From 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a Mon Sep 17 00:00:00 2001 From: xiejun <xiejun@vci-tech.com> Date: 星期五, 01 十一月 2024 15:11:19 +0800 Subject: [PATCH] Revert "集成获取mdm分发通用数据格式接口集成" --- Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ImageUtil.java | 489 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 489 insertions(+), 0 deletions(-) diff --git a/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ImageUtil.java b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ImageUtil.java new file mode 100644 index 0000000..4dd7183 --- /dev/null +++ b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ImageUtil.java @@ -0,0 +1,489 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 搴勯獮 (smallchill@163.com) + */ +package org.springblade.core.tool.utils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springblade.core.tool.support.IMultiOutputStream; +import org.springblade.core.tool.support.ImagePosition; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.color.ColorSpace; +import java.awt.geom.AffineTransform; +import java.awt.image.*; +import java.io.*; +import java.net.URL; + +/** + * 鍥剧墖宸ュ叿绫� + * + * @author Chill + */ +public final class ImageUtil { + + /** + * Logger for this class + */ + private static Logger LOGGER = LoggerFactory.getLogger(ImageUtil.class); + + /** + * 榛樿杈撳嚭鍥剧墖绫诲瀷 + */ + public static final String DEFAULT_IMG_TYPE = "JPEG"; + + private ImageUtil() { + + } + + /** + * 杞崲杈撳叆娴佸埌byte + * + * @param src 婧� + * @param type 绫诲瀷 + * @return byte[] + * @throws IOException 寮傚父 + */ + public static byte[] toByteArray(BufferedImage src, String type) throws IOException { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + ImageIO.write(src, defaultString(type, DEFAULT_IMG_TYPE), os); + return os.toByteArray(); + } + + /** + * 鑾峰彇鍥惧儚鍐呭 + * + * @param srcImageFile 鏂囦欢璺緞 + * @return BufferedImage + */ + public static BufferedImage readImage(String srcImageFile) { + try { + return ImageIO.read(new File(srcImageFile)); + } catch (IOException e) { + LOGGER.error("Error readImage", e); + } + return null; + } + + /** + * 鑾峰彇鍥惧儚鍐呭 + * + * @param srcImageFile 鏂囦欢 + * @return BufferedImage + */ + public static BufferedImage readImage(File srcImageFile) { + try { + return ImageIO.read(srcImageFile); + } catch (IOException e) { + LOGGER.error("Error readImage", e); + } + return null; + } + + /** + * 鑾峰彇鍥惧儚鍐呭 + * + * @param srcInputStream 杈撳叆娴� + * @return BufferedImage + */ + public static BufferedImage readImage(InputStream srcInputStream) { + try { + return ImageIO.read(srcInputStream); + } catch (IOException e) { + LOGGER.error("Error readImage", e); + } + return null; + } + + /** + * 鑾峰彇鍥惧儚鍐呭 + * + * @param url URL鍦板潃 + * @return BufferedImage + */ + public static BufferedImage readImage(URL url) { + try { + return ImageIO.read(url); + } catch (IOException e) { + LOGGER.error("Error readImage", e); + } + return null; + } + + + /** + * 缂╂斁鍥惧儚锛堟寜姣斾緥缂╂斁锛� + * + * @param src 婧愬浘鍍� + * @param output 杈撳嚭娴� + * @param type 绫诲瀷 + * @param scale 缂╂斁姣斾緥 + * @param flag 缂╂斁閫夋嫨:true 鏀惧ぇ; false 缂╁皬; + */ + public final static void zoomScale(BufferedImage src, OutputStream output, String type, double scale, boolean flag) { + try { + // 寰楀埌婧愬浘瀹� + int width = src.getWidth(); + // 寰楀埌婧愬浘闀� + int height = src.getHeight(); + if (flag) { + // 鏀惧ぇ + width = Long.valueOf(Math.round(width * scale)).intValue(); + height = Long.valueOf(Math.round(height * scale)).intValue(); + } else { + // 缂╁皬 + width = Long.valueOf(Math.round(width / scale)).intValue(); + height = Long.valueOf(Math.round(height / scale)).intValue(); + } + Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT); + BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics g = tag.getGraphics(); + + g.drawImage(image, 0, 0, null); + g.dispose(); + + ImageIO.write(tag, defaultString(type, DEFAULT_IMG_TYPE), output); + + output.close(); + } catch (IOException e) { + LOGGER.error("Error in zoom image", e); + } + } + + /** + * 缂╂斁鍥惧儚锛堟寜楂樺害鍜屽搴︾缉鏀撅級 + * + * @param src 婧愬浘鍍� + * @param output 杈撳嚭娴� + * @param type 绫诲瀷 + * @param height 缂╂斁鍚庣殑楂樺害 + * @param width 缂╂斁鍚庣殑瀹藉害 + * @param bb 姣斾緥涓嶅鏃舵槸鍚﹂渶瑕佽ˉ鐧斤細true涓鸿ˉ鐧�; false涓轰笉琛ョ櫧; + * @param fillColor 濉厖鑹诧紝null鏃朵负Color.WHITE + */ + public final static void zoomFixed(BufferedImage src, OutputStream output, String type, int height, int width, boolean bb, Color fillColor) { + try { + double ratio = 0.0; + Image itemp = src.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH); + // 璁$畻姣斾緥 + if (src.getHeight() > src.getWidth()) { + ratio = Integer.valueOf(height).doubleValue() / src.getHeight(); + } else { + ratio = Integer.valueOf(width).doubleValue() / src.getWidth(); + } + AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null); + itemp = op.filter(src, null); + + if (bb) { + //琛ョ櫧 + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics2D g = image.createGraphics(); + Color fill = fillColor == null ? Color.white : fillColor; + g.setColor(fill); + g.fillRect(0, 0, width, height); + if (width == itemp.getWidth(null)) { + g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), fill, null); + } else { + g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null), fill, null); + } + g.dispose(); + itemp = image; + } + // 杈撳嚭涓烘枃浠� + ImageIO.write((BufferedImage) itemp, defaultString(type, DEFAULT_IMG_TYPE), output); + // 鍏抽棴娴� + output.close(); + } catch (IOException e) { + LOGGER.error("Error in zoom image", e); + } + } + + /** + * 鍥惧儚瑁佸壀(鎸夋寚瀹氳捣鐐瑰潗鏍囧拰瀹介珮鍒囧壊) + * + * @param src 婧愬浘鍍� + * @param output 鍒囩墖鍚庣殑鍥惧儚鍦板潃 + * @param type 绫诲瀷 + * @param x 鐩爣鍒囩墖璧风偣鍧愭爣X + * @param y 鐩爣鍒囩墖璧风偣鍧愭爣Y + * @param width 鐩爣鍒囩墖瀹藉害 + * @param height 鐩爣鍒囩墖楂樺害 + */ + public final static void crop(BufferedImage src, OutputStream output, String type, int x, int y, int width, int height) { + try { + // 婧愬浘瀹藉害 + int srcWidth = src.getWidth(); + // 婧愬浘楂樺害 + int srcHeight = src.getHeight(); + if (srcWidth > 0 && srcHeight > 0) { + Image image = src.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT); + // 鍥涗釜鍙傛暟鍒嗗埆涓哄浘鍍忚捣鐐瑰潗鏍囧拰瀹介珮 + ImageFilter cropFilter = new CropImageFilter(x, y, width, height); + Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter)); + BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics g = tag.getGraphics(); + g.drawImage(img, 0, 0, width, height, null); + g.dispose(); + // 杈撳嚭涓烘枃浠� + ImageIO.write(tag, defaultString(type, DEFAULT_IMG_TYPE), output); + // 鍏抽棴娴� + output.close(); + } + } catch (Exception e) { + LOGGER.error("Error in cut image", e); + } + } + + /** + * 鍥惧儚鍒囧壊锛堟寚瀹氬垏鐗囩殑琛屾暟鍜屽垪鏁帮級 + * + * @param src 婧愬浘鍍忓湴鍧� + * @param mos 鍒囩墖鐩爣鏂囦欢澶� + * @param type 绫诲瀷 + * @param prows 鐩爣鍒囩墖琛屾暟銆傞粯璁�2锛屽繀椤绘槸鑼冨洿 [1, 20] 涔嬪唴 + * @param pcols 鐩爣鍒囩墖鍒楁暟銆傞粯璁�2锛屽繀椤绘槸鑼冨洿 [1, 20] 涔嬪唴 + */ + public final static void sliceWithNumber(BufferedImage src, IMultiOutputStream mos, String type, int prows, int pcols) { + try { + int rows = prows <= 0 || prows > 20 ? 2 : prows; + int cols = pcols <= 0 || pcols > 20 ? 2 : pcols; + // 婧愬浘瀹藉害 + int srcWidth = src.getWidth(); + // 婧愬浘楂樺害 + int srcHeight = src.getHeight(); + if (srcWidth > 0 && srcHeight > 0) { + Image img; + ImageFilter cropFilter; + Image image = src.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT); + // 姣忓紶鍒囩墖鐨勫搴� + int destWidth = (srcWidth % cols == 0) ? (srcWidth / cols) : (srcWidth / cols + 1); + // 姣忓紶鍒囩墖鐨勯珮搴� + int destHeight = (srcHeight % rows == 0) ? (srcHeight / rows) : (srcHeight / rows + 1); + // 寰幆寤虹珛鍒囩墖 + // 鏀硅繘鐨勬兂娉�:鏄惁鍙敤澶氱嚎绋嬪姞蹇垏鍓查�熷害 + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + // 鍥涗釜鍙傛暟鍒嗗埆涓哄浘鍍忚捣鐐瑰潗鏍囧拰瀹介珮 + cropFilter = new CropImageFilter(j * destWidth, i * destHeight, destWidth, destHeight); + img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter)); + BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); + Graphics g = tag.getGraphics(); + // 缁樺埗缂╁皬鍚庣殑鍥� + g.drawImage(img, 0, 0, null); + g.dispose(); + // 杈撳嚭涓烘枃浠� + ImageIO.write(tag, defaultString(type, DEFAULT_IMG_TYPE), mos.buildOutputStream(i, j)); + } + } + } + } catch (Exception e) { + LOGGER.error("Error in slice image", e); + } + } + + /** + * 鍥惧儚鍒囧壊锛堟寚瀹氬垏鐗囩殑瀹藉害鍜岄珮搴︼級 + * + * @param src 婧愬浘鍍忓湴鍧� + * @param mos 鍒囩墖鐩爣鏂囦欢澶� + * @param type 绫诲瀷 + * @param pdestWidth 鐩爣鍒囩墖瀹藉害銆傞粯璁�200 + * @param pdestHeight 鐩爣鍒囩墖楂樺害銆傞粯璁�150 + */ + public final static void sliceWithSize(BufferedImage src, IMultiOutputStream mos, String type, int pdestWidth, int pdestHeight) { + try { + int destWidth = pdestWidth <= 0 ? 200 : pdestWidth; + int destHeight = pdestHeight <= 0 ? 150 : pdestHeight; + // 婧愬浘瀹藉害 + int srcWidth = src.getWidth(); + // 婧愬浘楂樺害 + int srcHeight = src.getHeight(); + if (srcWidth > destWidth && srcHeight > destHeight) { + Image img; + ImageFilter cropFilter; + Image image = src.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT); + // 鍒囩墖妯悜鏁伴噺 + int cols = (srcWidth % destWidth == 0) ? (srcWidth / destWidth) : (srcWidth / destWidth + 1); + // 鍒囩墖绾靛悜鏁伴噺 + int rows = (srcHeight % destHeight == 0) ? (srcHeight / destHeight) : (srcHeight / destHeight + 1); + // 寰幆寤虹珛鍒囩墖 + // 鏀硅繘鐨勬兂娉�:鏄惁鍙敤澶氱嚎绋嬪姞蹇垏鍓查�熷害 + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + // 鍥涗釜鍙傛暟鍒嗗埆涓哄浘鍍忚捣鐐瑰潗鏍囧拰瀹介珮 + cropFilter = new CropImageFilter(j * destWidth, i * destHeight, destWidth, destHeight); + img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter)); + BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); + Graphics g = tag.getGraphics(); + // 缁樺埗缂╁皬鍚庣殑鍥� + g.drawImage(img, 0, 0, null); + g.dispose(); + // 杈撳嚭涓烘枃浠� + ImageIO.write(tag, defaultString(type, DEFAULT_IMG_TYPE), mos.buildOutputStream(i, j)); + } + } + } + } catch (Exception e) { + LOGGER.error("Error in slice image", e); + } + } + + /** + * 鍥惧儚绫诲瀷杞崲锛欸IF-JPG銆丟IF-PNG銆丳NG-JPG銆丳NG-GIF(X)銆丅MP-PNG + * + * @param src 婧愬浘鍍忓湴鍧� + * @param formatName 鍖呭惈鏍煎紡闈炴寮忓悕绉扮殑 String锛氬JPG銆丣PEG銆丟IF绛� + * @param output 鐩爣鍥惧儚鍦板潃 + */ + public final static void convert(BufferedImage src, OutputStream output, String formatName) { + try { + // 杈撳嚭涓烘枃浠� + ImageIO.write(src, formatName, output); + // 鍏抽棴娴� + output.close(); + } catch (Exception e) { + LOGGER.error("Error in convert image", e); + } + } + + /** + * 褰╄壊杞负榛戠櫧 + * + * @param src 婧愬浘鍍忓湴鍧� + * @param output 鐩爣鍥惧儚鍦板潃 + * @param type 绫诲瀷 + */ + public final static void gray(BufferedImage src, OutputStream output, String type) { + try { + ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); + ColorConvertOp op = new ColorConvertOp(cs, null); + src = op.filter(src, null); + // 杈撳嚭涓烘枃浠� + ImageIO.write(src, defaultString(type, DEFAULT_IMG_TYPE), output); + // 鍏抽棴娴� + output.close(); + } catch (IOException e) { + LOGGER.error("Error in gray image", e); + } + } + + /** + * 缁欏浘鐗囨坊鍔犳枃瀛楁按鍗� + * + * @param src 婧愬浘鍍� + * @param output 杈撳嚭娴� + * @param type 绫诲瀷 + * @param text 姘村嵃鏂囧瓧 + * @param font 姘村嵃鐨勫瓧浣� + * @param color 姘村嵃鐨勫瓧浣撻鑹� + * @param position 姘村嵃浣嶇疆 {@link ImagePosition} + * @param x 淇鍊� + * @param y 淇鍊� + * @param alpha 閫忔槑搴︼細alpha 蹇呴』鏄寖鍥� [0.0, 1.0] 涔嬪唴锛堝寘鍚竟鐣屽�硷級鐨勪竴涓诞鐐规暟瀛� + */ + public final static void textStamp(BufferedImage src, OutputStream output, String type, String text, Font font, Color color + , int position, int x, int y, float alpha) { + try { + int width = src.getWidth(null); + int height = src.getHeight(null); + BufferedImage image = new BufferedImage(width, height, + BufferedImage.TYPE_INT_RGB); + Graphics2D g = image.createGraphics(); + g.drawImage(src, 0, 0, width, height, null); + g.setColor(color); + g.setFont(font); + g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); + // 鍦ㄦ寚瀹氬潗鏍囩粯鍒舵按鍗版枃瀛� + ImagePosition boxPos = new ImagePosition(width, height, calcTextWidth(text) * font.getSize(), font.getSize(), position); + g.drawString(text, boxPos.getX(x), boxPos.getY(y)); + g.dispose(); + // 杈撳嚭涓烘枃浠� + ImageIO.write((BufferedImage) image, defaultString(type, DEFAULT_IMG_TYPE), output); + // 鍏抽棴娴� + output.close(); + } catch (Exception e) { + LOGGER.error("Error in textStamp image", e); + } + } + + /** + * 缁欏浘鐗囨坊鍔犲浘鐗囨按鍗� + * + * @param src 婧愬浘鍍� + * @param output 杈撳嚭娴� + * @param type 绫诲瀷 + * @param stamp 姘村嵃鍥剧墖 + * @param position 姘村嵃浣嶇疆 {@link ImagePosition} + * @param x 淇鍊� + * @param y 淇鍊� + * @param alpha 閫忔槑搴︼細alpha 蹇呴』鏄寖鍥� [0.0, 1.0] 涔嬪唴锛堝寘鍚竟鐣屽�硷級鐨勪竴涓诞鐐规暟瀛� + */ + public final static void imageStamp(BufferedImage src, OutputStream output, String type, BufferedImage stamp + , int position, int x, int y, float alpha) { + try { + int width = src.getWidth(); + int height = src.getHeight(); + BufferedImage image = new BufferedImage(width, height, + BufferedImage.TYPE_INT_RGB); + Graphics2D g = image.createGraphics(); + g.drawImage(src, 0, 0, width, height, null); + // 姘村嵃鏂囦欢 + int stampWidth = stamp.getWidth(); + int stampHeight = stamp.getHeight(); + g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); + ImagePosition boxPos = new ImagePosition(width, height, stampWidth, stampHeight, position); + g.drawImage(stamp, boxPos.getX(x), boxPos.getY(y), stampWidth, stampHeight, null); + // 姘村嵃鏂囦欢缁撴潫 + g.dispose(); + // 杈撳嚭涓烘枃浠� + ImageIO.write((BufferedImage) image, defaultString(type, DEFAULT_IMG_TYPE), output); + // 鍏抽棴娴� + output.close(); + } catch (Exception e) { + LOGGER.error("Error imageStamp", e); + } + } + + /** + * 璁$畻text鐨勯暱搴︼紙涓�涓腑鏂囩畻涓や釜瀛楃锛� + * + * @param text text + * @return int + */ + public final static int calcTextWidth(String text) { + int length = 0; + for (int i = 0; i < text.length(); i++) { + if (new String(text.charAt(i) + "").getBytes().length > 1) { + length += 2; + } else { + length += 1; + } + } + return length / 2; + } + + /** + * 榛樿瀛楃涓� + * @param str 瀛楃涓� + * @param defaultStr 榛樿鍊� + * @return + */ + public static String defaultString(String str, String defaultStr) { + return ((str == null) ? defaultStr : str); + } + +} -- Gitblit v1.9.3