|
@@ -1,735 +0,0 @@
|
|
|
-package com.xingxi.api.common;
|
|
|
|
|
-
|
|
|
|
|
-import com.jhlabs.image.*;
|
|
|
|
|
-import net.coobird.thumbnailator.Thumbnails;
|
|
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
-import sun.font.FontDesignMetrics;
|
|
|
|
|
-
|
|
|
|
|
-import javax.imageio.ImageIO;
|
|
|
|
|
-import java.awt.*;
|
|
|
|
|
-import java.awt.geom.AffineTransform;
|
|
|
|
|
-import java.awt.image.AffineTransformOp;
|
|
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
|
|
-import java.io.FileOutputStream;
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-import java.util.*;
|
|
|
|
|
-
|
|
|
|
|
-/**
|
|
|
|
|
- * ImageUtil 图像处理工具类
|
|
|
|
|
- *
|
|
|
|
|
- * @author : jinqiwen
|
|
|
|
|
- * @Date : 2014-1-2 下午9:13:21
|
|
|
|
|
- * @Description : 图像处理工具类
|
|
|
|
|
- * @Version : 1.0.0
|
|
|
|
|
- */
|
|
|
|
|
-public final class ImageUtils {
|
|
|
|
|
- public static final String IMAGE_SUFFIX_PNG = ".png";
|
|
|
|
|
- public static final String SEPARATRIX_COMMA = ",";
|
|
|
|
|
- public static final String COLOR_STRING_REPLACE_TEXT = "rgb()";
|
|
|
|
|
-
|
|
|
|
|
- private ImageUtils() {
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage base64StringToBufferedImage(String base64String) {
|
|
|
|
|
-
|
|
|
|
|
- int index = base64String.indexOf(",");
|
|
|
|
|
- // have Data URI Scheme
|
|
|
|
|
- if (index >= 0) {
|
|
|
|
|
- base64String = base64String.substring(index + 1);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- return ImageIO.read(new ByteArrayInputStream(base64String.getBytes()));
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage createBufferedImage(int width, int height) {
|
|
|
|
|
- return new BufferedImage(width, height, 1);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage createTransparencyBufferedImage(int width, int height) {
|
|
|
|
|
- BufferedImage bufferedImage = createBufferedImage(width, height);
|
|
|
|
|
- Graphics2D graphics2D = bufferedImage.createGraphics();
|
|
|
|
|
- bufferedImage = graphics2D.getDeviceConfiguration().createCompatibleImage(width, height, 3);
|
|
|
|
|
- graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
|
|
- graphics2D.dispose();
|
|
|
|
|
- return bufferedImage;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage createBufferedImage(int width, int height, Color color) {
|
|
|
|
|
- BufferedImage bufferedImage = createBufferedImage(width, height);
|
|
|
|
|
- Graphics2D graphics2D = bufferedImage.createGraphics();
|
|
|
|
|
- graphics2D.setColor(color);
|
|
|
|
|
- graphics2D.fillRect(0, 0, width, height);
|
|
|
|
|
- graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
|
|
- graphics2D.dispose();
|
|
|
|
|
- return bufferedImage;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static void makeImageFile(BufferedImage inputImage, String outputImage) {
|
|
|
|
|
- try {
|
|
|
|
|
- Thumbnails.of(inputImage)
|
|
|
|
|
- .size(inputImage.getWidth(), inputImage.getHeight())
|
|
|
|
|
- .outputQuality(1f)
|
|
|
|
|
- .toFile(outputImage);
|
|
|
|
|
- } catch (IOException ex) {
|
|
|
|
|
- throw new RuntimeException(ex);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static void makeImageFile(BufferedImage inputImage, BufferedImage waterImage, String outputImage) {
|
|
|
|
|
- try {
|
|
|
|
|
- Thumbnails.of(inputImage).watermark(waterImage, 0.8f).size(1000, 1000).outputQuality(1f).toFile(outputImage);
|
|
|
|
|
- } catch (IOException ex) {
|
|
|
|
|
- throw new RuntimeException(ex);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static int getWordWidth(Font font, String content){
|
|
|
|
|
- FontMetrics fontMetrics = FontDesignMetrics.getMetrics(font);
|
|
|
|
|
- int w = 0;
|
|
|
|
|
- for (int i = 0; i < content.length(); i++) {
|
|
|
|
|
- w += fontMetrics.charWidth(content.charAt(i));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return w;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage drawString(
|
|
|
|
|
- List<String> contents, Color color,
|
|
|
|
|
- Font font, int width, int height, int gap) {
|
|
|
|
|
-
|
|
|
|
|
-// BufferedImage bufferedImage = createTransparencyBufferedImage(width, height);
|
|
|
|
|
- BufferedImage bufferedImage = createBufferedImage(width, height, Color.BLUE);
|
|
|
|
|
- GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
|
|
|
|
- localGraphicsEnvironment.registerFont(font);
|
|
|
|
|
- Graphics2D graphics = localGraphicsEnvironment.createGraphics(bufferedImage);
|
|
|
|
|
- graphics.setColor(color);
|
|
|
|
|
- graphics.setFont(font);
|
|
|
|
|
- int y = graphics.getFontMetrics().getAscent();
|
|
|
|
|
- int height1 = graphics.getFontMetrics().getHeight();
|
|
|
|
|
- int descent = graphics.getFontMetrics().getDescent();
|
|
|
|
|
-// int y = (height / 2) + graphics.getFontMetrics().getAscent();
|
|
|
|
|
-// y = y;
|
|
|
|
|
- int size = contents.size();
|
|
|
|
|
- for (int i = 0; i < size; i++) {
|
|
|
|
|
- String content = contents.get(i);
|
|
|
|
|
- int wordWidth = getWordWidth(font, content);
|
|
|
|
|
- if (i == 0) {
|
|
|
|
|
- graphics.drawString(content, (width-wordWidth) / 2, y + descent);
|
|
|
|
|
- } else {
|
|
|
|
|
- graphics.drawString(content, (width-wordWidth) / 2, (y + descent)*i + height1 + gap);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- graphics.dispose();
|
|
|
|
|
-
|
|
|
|
|
- return bufferedImage;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage waterMark2(String content, Color foreColor, Font font) {
|
|
|
|
|
-
|
|
|
|
|
- FontMetrics fontMetrics = FontDesignMetrics.getMetrics(font);
|
|
|
|
|
- int width = fontMetrics.stringWidth(content);
|
|
|
|
|
- int height = fontMetrics.getHeight();
|
|
|
|
|
-
|
|
|
|
|
- BufferedImage bufferedImage = createTransparencyBufferedImage(width, height);
|
|
|
|
|
- GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
|
|
|
|
- localGraphicsEnvironment.registerFont(font);
|
|
|
|
|
- Graphics2D graphics = localGraphicsEnvironment.createGraphics(bufferedImage);
|
|
|
|
|
- graphics.setColor(foreColor);
|
|
|
|
|
- graphics.setFont(font);
|
|
|
|
|
- graphics.drawString(content, 0, fontMetrics.getAscent());
|
|
|
|
|
- graphics.dispose();
|
|
|
|
|
-
|
|
|
|
|
- return bufferedImage;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage waterMark(String content, Color foreColor, Font font) {
|
|
|
|
|
-
|
|
|
|
|
- FontMetrics fm = FontDesignMetrics.getMetrics(font);
|
|
|
|
|
- int width = fm.stringWidth(content);
|
|
|
|
|
- int height = fm.getHeight();
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- BufferedImage outputImage = createTransparencyBufferedImage(width, height);
|
|
|
|
|
- Graphics2D g = outputImage.createGraphics();
|
|
|
|
|
- g.setColor(foreColor);
|
|
|
|
|
- g.setFont(font);
|
|
|
|
|
- g.drawString(content, 0, fm.getAscent());
|
|
|
|
|
- g.dispose();
|
|
|
|
|
-
|
|
|
|
|
- return outputImage;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- //生成分享图片
|
|
|
|
|
-// public static void makeShareImg(String bgImagePath,
|
|
|
|
|
-// String qrCodePath,
|
|
|
|
|
-// String outPath,
|
|
|
|
|
-// String name,
|
|
|
|
|
-// String rmb, String shopname, String merchantLogoPath, String shareDesc, Color shopColor, String backShare) {
|
|
|
|
|
-// //底图
|
|
|
|
|
-// BufferedImage bi = read(backShare);
|
|
|
|
|
-//
|
|
|
|
|
-// //效果图
|
|
|
|
|
-// BufferedImage backBi = read(bgImagePath);
|
|
|
|
|
-// backBi = resize(backBi, 375, 375);
|
|
|
|
|
-// bi = addImage(bi, backBi, 0, 67);
|
|
|
|
|
-//
|
|
|
|
|
-// //LOGO图
|
|
|
|
|
-// if (org.apache.commons.lang3.StringUtils.isNotBlank(merchantLogoPath)) {
|
|
|
|
|
-// BufferedImage logoBi = read(merchantLogoPath);
|
|
|
|
|
-// try {
|
|
|
|
|
-//// logoBi = Thumbnails.of(logoBi).size(100,40).asBufferedImage();
|
|
|
|
|
-// logoBi = Thumbnails.of(logoBi).width(55).height(55).asBufferedImage();
|
|
|
|
|
-// } catch (IOException e) {
|
|
|
|
|
-// logoBi = resize(logoBi, 55, 55);
|
|
|
|
|
-// }
|
|
|
|
|
-// bi = addImage(bi, logoBi, 10, 10);
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// //二维码
|
|
|
|
|
-// BufferedImage qrCodeBi = read(qrCodePath);
|
|
|
|
|
-// qrCodeBi = resize(qrCodeBi, 100, 100);
|
|
|
|
|
-// bi = addImage(bi, qrCodeBi, 265, 490);
|
|
|
|
|
-//
|
|
|
|
|
-//// Font font = new Font("宋体", Font.PLAIN, 20);
|
|
|
|
|
-// //商铺名称
|
|
|
|
|
-// BufferedImage shopnameBi = waterMark(shopname, Color.BLACK, new Font("宋体", Font.BOLD, 22));
|
|
|
|
|
-// bi = addImage(bi, shopnameBi, 90, 15);
|
|
|
|
|
-//
|
|
|
|
|
-// //商铺分享口号
|
|
|
|
|
-// if (org.apache.commons.lang3.StringUtils.isNotBlank(shareDesc)) {
|
|
|
|
|
-// BufferedImage shareDescBi = waterMark(shareDesc, Color.BLACK, new Font("宋体", Font.ITALIC, 14));
|
|
|
|
|
-// bi = addImage(bi, shareDescBi, 90, 42);
|
|
|
|
|
-// }
|
|
|
|
|
-// //商品名称
|
|
|
|
|
-// BufferedImage nameBi = waterMark(name, Color.BLACK, new Font("宋体", Font.PLAIN, 20));
|
|
|
|
|
-// bi = addImage(bi, nameBi, 30, 500);
|
|
|
|
|
-// //价格
|
|
|
|
|
-// BufferedImage rmbBi = waterMark("RMB: " + rmb, Color.BLACK, new Font("宋体", Font.PLAIN, 20));
|
|
|
|
|
-// bi = addImage(bi, rmbBi, 30, 530);
|
|
|
|
|
-//
|
|
|
|
|
-// //主题色块
|
|
|
|
|
-// BufferedImage biblock = create(20, 75, shopColor);
|
|
|
|
|
-// bi = addImage(bi, biblock, 0, 480);
|
|
|
|
|
-//
|
|
|
|
|
-// makeImageFile(bi, outPath);
|
|
|
|
|
-// }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static void base64Str2Image(String filePath, String str) {
|
|
|
|
|
- FileOutputStream fos = null;
|
|
|
|
|
- try {
|
|
|
|
|
- fos = new FileOutputStream(filePath);
|
|
|
|
|
- byte[] bytes = Base64.getDecoder().decode(str);
|
|
|
|
|
- fos.write(bytes);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
|
- } finally {
|
|
|
|
|
- if (fos != null) {
|
|
|
|
|
- try {
|
|
|
|
|
- fos.close();
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-// public static BufferedImage read(String sourceFile) {
|
|
|
|
|
-//
|
|
|
|
|
-// InputStream is = null;
|
|
|
|
|
-// BufferedImage bi;
|
|
|
|
|
-// try {
|
|
|
|
|
-// URL url = new URL(sourceFile);
|
|
|
|
|
-// bi = ImageIO.read(url);
|
|
|
|
|
-// } catch (Exception ex) {
|
|
|
|
|
-// if (ex instanceof MalformedURLException) {
|
|
|
|
|
-// try {
|
|
|
|
|
-// is = Files.newInputStream(Paths.get(sourceFile));
|
|
|
|
|
-// bi = ImageIO.read(is);
|
|
|
|
|
-// } catch (IOException e) {
|
|
|
|
|
-// throw new RuntimeException(e);
|
|
|
|
|
-// } finally {
|
|
|
|
|
-// if (is != null) {
|
|
|
|
|
-// try {
|
|
|
|
|
-// is.close();
|
|
|
|
|
-// } catch (IOException e) {
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-// } else {
|
|
|
|
|
-// throw new RuntimeException(ex);
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// return bi;
|
|
|
|
|
-// }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage gray(BufferedImage image) {
|
|
|
|
|
-
|
|
|
|
|
- int width = image.getWidth();
|
|
|
|
|
- int height = image.getHeight();
|
|
|
|
|
-
|
|
|
|
|
- BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
|
|
|
|
|
- for (int i = 0; i < width; i++) {
|
|
|
|
|
- for (int j = 0; j < height; j++) {
|
|
|
|
|
- int rgb = image.getRGB(i, j);
|
|
|
|
|
- grayImage.setRGB(i, j, rgb);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return grayImage;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage embossEdge(BufferedImage image) throws IOException {
|
|
|
|
|
-
|
|
|
|
|
- BufferedImage dst = null;
|
|
|
|
|
-
|
|
|
|
|
- //加亮
|
|
|
|
|
- AbstractBufferedImageOp exposure = new ExposureFilter();
|
|
|
|
|
- dst = exposure.filter(image, dst);
|
|
|
|
|
-
|
|
|
|
|
- //压花
|
|
|
|
|
- AbstractBufferedImageOp gray = new EmbossFilter();
|
|
|
|
|
- dst = gray.filter(image, dst);
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- return dst;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage print(BufferedImage source) {
|
|
|
|
|
- System.out.println("print");
|
|
|
|
|
- return source;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage embroid(BufferedImage source) {
|
|
|
|
|
- System.out.println("embroid");
|
|
|
|
|
- try {
|
|
|
|
|
- return embossEdge(source);
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage laser(BufferedImage source) {
|
|
|
|
|
- System.out.println("laser");
|
|
|
|
|
- try {
|
|
|
|
|
- return lineEdge(source);
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage lineEdge(BufferedImage image) throws IOException {
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- /*
|
|
|
|
|
- JHLABS
|
|
|
|
|
- */
|
|
|
|
|
- BufferedImage dst = null;
|
|
|
|
|
-// //加亮
|
|
|
|
|
-// AbstractBufferedImageOp exposure = new ExposureFilter();
|
|
|
|
|
-// dst = exposure.filter(image,dst);
|
|
|
|
|
-
|
|
|
|
|
- //灰度
|
|
|
|
|
- AbstractBufferedImageOp gray = new GrayscaleFilter();
|
|
|
|
|
- dst = gray.filter(image, dst);
|
|
|
|
|
-
|
|
|
|
|
- //中值
|
|
|
|
|
- AbstractBufferedImageOp median = new MedianFilter();
|
|
|
|
|
- dst = median.filter(dst, dst);
|
|
|
|
|
-
|
|
|
|
|
- //边缘
|
|
|
|
|
- AbstractBufferedImageOp edge = new EdgeFilter();
|
|
|
|
|
- dst = edge.filter(dst, dst);
|
|
|
|
|
-
|
|
|
|
|
- AbstractBufferedImageOp invert = new InvertFilter();
|
|
|
|
|
- dst = invert.filter(dst, dst);
|
|
|
|
|
-
|
|
|
|
|
- AbstractBufferedImageOp threshold = new ThresholdFilter(235);
|
|
|
|
|
- dst = threshold.filter(dst, dst);
|
|
|
|
|
-//
|
|
|
|
|
-// StampFilter sf = new StampFilter();
|
|
|
|
|
-// sf.setRadius(1f);
|
|
|
|
|
-// sf.setSoftness(0f);
|
|
|
|
|
-// AbstractBufferedImageOp bbf2 = sf;
|
|
|
|
|
-// dst = bbf2.filter(dst, dst);
|
|
|
|
|
- return dst;
|
|
|
|
|
-
|
|
|
|
|
-// //将Mat转BufferedImage
|
|
|
|
|
-// MatOfByte mob = new MatOfByte();
|
|
|
|
|
-// Imgcodecs.imencode(".jpg", dstImage, mob);
|
|
|
|
|
-// // convert the "matrix of bytes" into a byte array
|
|
|
|
|
-// byte[] byteArray = mob.toArray();
|
|
|
|
|
-// InputStream in = new ByteArrayInputStream(byteArray);
|
|
|
|
|
-// return ImageIO.read(in);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static String randomImgName() {
|
|
|
|
|
- return UUID.randomUUID() + IMAGE_SUFFIX_PNG;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static Color getColor(String colorStr) {
|
|
|
|
|
- Color color = null;
|
|
|
|
|
- if (colorStr != null && !colorStr.isEmpty()) {
|
|
|
|
|
- colorStr = StringUtils.strip(colorStr, COLOR_STRING_REPLACE_TEXT);
|
|
|
|
|
- String[] rgbs = colorStr.split(SEPARATRIX_COMMA);
|
|
|
|
|
- color = new Color(
|
|
|
|
|
- Integer.parseInt(rgbs[0]),
|
|
|
|
|
- Integer.parseInt(rgbs[1]),
|
|
|
|
|
- Integer.parseInt(rgbs[2])
|
|
|
|
|
- );
|
|
|
|
|
- }
|
|
|
|
|
- return color;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /*
|
|
|
|
|
- 6位数字字符串转颜色
|
|
|
|
|
- */
|
|
|
|
|
- public static Color strToColor(String colorStr) {
|
|
|
|
|
- colorStr = colorStr.replace("#", "");
|
|
|
|
|
- int i = Integer.parseInt(colorStr, 16);
|
|
|
|
|
- return new Color(i);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-// /**
|
|
|
|
|
-// * opencv 检测图片亮度
|
|
|
|
|
-// * brightnessException 计算并返回一幅图像的色偏度以及,色偏方向
|
|
|
|
|
-// * cast 计算出的偏差值,小于1表示比较正常,大于1表示存在亮度异常;当cast异常时,da大于0表示过亮,da小于0表示过暗
|
|
|
|
|
-// * 返回值通过cast、da两个引用返回,无显式返回值
|
|
|
|
|
-// */
|
|
|
|
|
-// public static Integer brightnessException (Mat srcImage) {
|
|
|
|
|
-// Mat dstImage = new Mat();
|
|
|
|
|
-// // 将RGB图转为灰度图
|
|
|
|
|
-// Imgproc.cvtColor(srcImage,dstImage, Imgproc.COLOR_BGR2GRAY);
|
|
|
|
|
-// float a=0;
|
|
|
|
|
-// int[] Hist = new int[256];
|
|
|
|
|
-// for(int i=0;i<256;i++) {
|
|
|
|
|
-// Hist[i] = 0;
|
|
|
|
|
-// }
|
|
|
|
|
-// for(int i=0;i<dstImage.rows();i++)
|
|
|
|
|
-// {
|
|
|
|
|
-// for(int j=0;j<dstImage.cols();j++)
|
|
|
|
|
-// {
|
|
|
|
|
-// //在计算过程中,考虑128为亮度均值点
|
|
|
|
|
-// a+=(float)(dstImage.get(i,j)[0]-128);
|
|
|
|
|
-// int x=(int)dstImage.get(i,j)[0];
|
|
|
|
|
-// Hist[x]++;
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-// float da = a/(float)(dstImage.rows()*dstImage.cols());
|
|
|
|
|
-// System.out.println(da);
|
|
|
|
|
-// float D =Math.abs(da);
|
|
|
|
|
-// float Ma=0;
|
|
|
|
|
-// for(int i=0;i<256;i++)
|
|
|
|
|
-// {
|
|
|
|
|
-// Ma+=Math.abs(i-128-da)*Hist[i];
|
|
|
|
|
-// }
|
|
|
|
|
-// Ma/=(float)((dstImage.rows()*dstImage.cols()));
|
|
|
|
|
-// float M=Math.abs(Ma);
|
|
|
|
|
-// float K=D/M;
|
|
|
|
|
-// float cast = K;
|
|
|
|
|
-// System.out.printf("亮度指数: %f\n",cast);
|
|
|
|
|
-// if(cast>=1) {
|
|
|
|
|
-// System.out.printf("亮度:"+da);
|
|
|
|
|
-// if(da > 0) {
|
|
|
|
|
-// System.out.printf("过亮\n");
|
|
|
|
|
-// return 2;
|
|
|
|
|
-// } else {
|
|
|
|
|
-// System.out.printf("过暗\n");
|
|
|
|
|
-// return 1;
|
|
|
|
|
-// }
|
|
|
|
|
-// } else {
|
|
|
|
|
-// System.out.printf("亮度:正常\n");
|
|
|
|
|
-// return 0;
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * @param inputImage 源图缓存
|
|
|
|
|
- * @param addImage 叠加的图片
|
|
|
|
|
- * @param x 叠加图片的位置-x
|
|
|
|
|
- * @param y 叠加图片的位置-y
|
|
|
|
|
- * @MethodName : processImageWithAddImage
|
|
|
|
|
- * @Description : 对缓存的图形进行缩放
|
|
|
|
|
- */
|
|
|
|
|
- public static BufferedImage addImage(BufferedImage inputImage, BufferedImage addImage, int x, int y) {
|
|
|
|
|
- /* 原始图像的宽度和高度 */
|
|
|
|
|
- int width = inputImage.getWidth();
|
|
|
|
|
- int height = inputImage.getHeight();
|
|
|
|
|
-
|
|
|
|
|
- //定义一个临时BufferedImage对象,用于保存缩放后的位图
|
|
|
|
|
- BufferedImage outputImage = createTransparencyBufferedImage(width, height);
|
|
|
|
|
- // 获取Graphics2D画布
|
|
|
|
|
- Graphics2D g2d = outputImage.createGraphics();
|
|
|
|
|
-
|
|
|
|
|
- // 添加原图到画布
|
|
|
|
|
- g2d.drawImage(inputImage, 0, 0, null);
|
|
|
|
|
- g2d = outputImage.createGraphics();
|
|
|
|
|
- // 添加原图到画布
|
|
|
|
|
- g2d.drawImage(addImage, x, y, null);
|
|
|
|
|
- g2d = outputImage.createGraphics();
|
|
|
|
|
-
|
|
|
|
|
- // 释放对象
|
|
|
|
|
- g2d.dispose();
|
|
|
|
|
-
|
|
|
|
|
- return outputImage;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage resized(BufferedImage inputImage) {
|
|
|
|
|
- return resized(inputImage, 300, 300);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage resized(BufferedImage inputImage, int width, int height) {
|
|
|
|
|
- try {
|
|
|
|
|
- inputImage = Thumbnails.of(inputImage).size(width, height).outputQuality(1f).asBufferedImage();
|
|
|
|
|
- } catch (IOException ex) {
|
|
|
|
|
- throw new RuntimeException(ex);
|
|
|
|
|
- }
|
|
|
|
|
- return inputImage;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage resize(BufferedImage inputImage, float scale) {
|
|
|
|
|
-
|
|
|
|
|
- int width = inputImage.getWidth();
|
|
|
|
|
- int height = inputImage.getHeight();
|
|
|
|
|
-
|
|
|
|
|
- int resizedWidth = (int) (width * scale);
|
|
|
|
|
- int resizedHeight = (int) (height * scale);
|
|
|
|
|
-
|
|
|
|
|
- return resize(inputImage, resizedWidth, resizedHeight);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage resize(BufferedImage inputImage, int width, int height) {
|
|
|
|
|
-
|
|
|
|
|
- Image resizedImage = inputImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
|
|
|
|
|
- BufferedImage newImage = createTransparencyBufferedImage(width, height);
|
|
|
|
|
- Graphics2D g = newImage.createGraphics();
|
|
|
|
|
- g.drawImage(resizedImage, 0, 0, width, height, null);
|
|
|
|
|
- g.dispose();
|
|
|
|
|
-
|
|
|
|
|
- return newImage;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * @param inputImage 源图文件
|
|
|
|
|
- * @param degree 旋转角度
|
|
|
|
|
- * @return 旋转后图像
|
|
|
|
|
- * @MethodName : processImageWithSpin
|
|
|
|
|
- * @Description : 对缓存的图形进行旋转
|
|
|
|
|
- */
|
|
|
|
|
- public static BufferedImage rotate(BufferedImage inputImage, double degree) {
|
|
|
|
|
- int swidth = 0; // 旋转后的宽度
|
|
|
|
|
- int sheight = 0; // 旋转后的高度
|
|
|
|
|
- int x; // 原点横坐标
|
|
|
|
|
- int y; // 原点纵坐标
|
|
|
|
|
-
|
|
|
|
|
- // 处理角度--确定旋转弧度
|
|
|
|
|
- degree = degree % 360;
|
|
|
|
|
- // 将角度转换到0-360度之间
|
|
|
|
|
- if (degree < 0) {
|
|
|
|
|
- degree = 360 + degree;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 将角度转为弧度
|
|
|
|
|
- double theta = Math.toRadians(degree);
|
|
|
|
|
-
|
|
|
|
|
- // 确定旋转后的宽和高
|
|
|
|
|
- if (degree == 180 || degree == 0 || degree == 360) {
|
|
|
|
|
- swidth = inputImage.getWidth();
|
|
|
|
|
- sheight = inputImage.getHeight();
|
|
|
|
|
- } else if (degree == 90 || degree == 270) {
|
|
|
|
|
- sheight = inputImage.getWidth();
|
|
|
|
|
- swidth = inputImage.getHeight();
|
|
|
|
|
- } else {
|
|
|
|
|
- swidth = (int) (Math.sqrt(inputImage.getWidth() * inputImage.getWidth()
|
|
|
|
|
- + inputImage.getHeight() * inputImage.getHeight()));
|
|
|
|
|
- sheight = (int) (Math.sqrt(inputImage.getWidth() * inputImage.getWidth()
|
|
|
|
|
- + inputImage.getHeight() * inputImage.getHeight()));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 确定原点坐标
|
|
|
|
|
- x = (swidth / 2) - (inputImage.getWidth() / 2);
|
|
|
|
|
- y = (sheight / 2) - (inputImage.getHeight() / 2);
|
|
|
|
|
-
|
|
|
|
|
- BufferedImage spinImage = new BufferedImage(swidth, sheight, inputImage.getType());
|
|
|
|
|
-
|
|
|
|
|
- AffineTransform at = new AffineTransform();
|
|
|
|
|
- // 旋转图象
|
|
|
|
|
- at.rotate(theta, swidth >> 1, sheight >> 1);
|
|
|
|
|
- at.translate(x, y);
|
|
|
|
|
- AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
|
|
|
|
|
- spinImage = op.filter(inputImage, spinImage);
|
|
|
|
|
-
|
|
|
|
|
- return spinImage;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * @param inputImage 源图缓存
|
|
|
|
|
- * @param x 移动后坐标-x
|
|
|
|
|
- * @param y 移动后坐标-y
|
|
|
|
|
- * @MethodName : processImageWithMove
|
|
|
|
|
- * @Description : 对缓存的图形进行移动
|
|
|
|
|
- */
|
|
|
|
|
- public static BufferedImage move(BufferedImage inputImage, int x, int y) {
|
|
|
|
|
- // 获取原始图像的宽度和高度
|
|
|
|
|
- int width = inputImage.getWidth();
|
|
|
|
|
- int height = inputImage.getHeight();
|
|
|
|
|
-
|
|
|
|
|
- //定义一个临时BufferedImage对象,用于保存移动后的位图
|
|
|
|
|
- BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
|
|
- // 获取Graphics2D画布
|
|
|
|
|
- Graphics2D g2d = outputImage.createGraphics();
|
|
|
|
|
- // 增加下面代码使得背景透明
|
|
|
|
|
- outputImage = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
|
|
|
|
|
- // 添加图像到画布
|
|
|
|
|
- g2d = outputImage.createGraphics();
|
|
|
|
|
- g2d.drawImage(inputImage, x, y, null);
|
|
|
|
|
-
|
|
|
|
|
- // 释放对象
|
|
|
|
|
- g2d.dispose();
|
|
|
|
|
-
|
|
|
|
|
- return outputImage;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static BufferedImage test(List<String> words, Font font, Color fontColor, int heightSpace){
|
|
|
|
|
-
|
|
|
|
|
- Optional<String> max = words.stream().max(Comparator.comparing(String::length));
|
|
|
|
|
- String maxString = max.get(); // 文字的最大长度
|
|
|
|
|
- int lineCount = words.size(); // 行数
|
|
|
|
|
-
|
|
|
|
|
- FontMetrics fm = FontDesignMetrics.getMetrics(font);
|
|
|
|
|
-
|
|
|
|
|
- int startX = 0; // x
|
|
|
|
|
- int startY = fm.getAscent(); // y
|
|
|
|
|
-// int heightSpace = 100; // 行间距
|
|
|
|
|
- int descent = fm.getDescent();
|
|
|
|
|
- int leading = fm.getLeading();
|
|
|
|
|
- int baseline = 30;
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- int maxWidth = fm.stringWidth(maxString); // 最大宽度
|
|
|
|
|
- int textHeight = fm.getHeight(); // 文字高度
|
|
|
|
|
-
|
|
|
|
|
- heightSpace = textHeight / 2 * heightSpace + descent;
|
|
|
|
|
- int maxHeight = lineCount * textHeight + (lineCount - 1 + heightSpace);
|
|
|
|
|
-
|
|
|
|
|
-// BufferedImage bufferedImage = createBufferedImage(maxWidth, maxHeight, Color.BLUE);
|
|
|
|
|
- BufferedImage bufferedImage = createTransparencyBufferedImage(maxWidth, maxHeight);
|
|
|
|
|
- GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
|
|
|
|
- localGraphicsEnvironment.registerFont(font);
|
|
|
|
|
- Graphics2D graphics = localGraphicsEnvironment.createGraphics(bufferedImage);
|
|
|
|
|
- graphics.setFont(font);
|
|
|
|
|
- graphics.setColor(fontColor);
|
|
|
|
|
-
|
|
|
|
|
- for (int i = 0; i < lineCount; i++) {
|
|
|
|
|
- String s = words.get(i);
|
|
|
|
|
- graphics.drawString(s, startX, startY + i * (textHeight / 2 + heightSpace));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- graphics.dispose();
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- return bufferedImage;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static void main(String[] args) throws Exception {
|
|
|
|
|
-
|
|
|
|
|
-//
|
|
|
|
|
-// // 设置测试用图片的名字
|
|
|
|
|
-// String fileName = "1.png";
|
|
|
|
|
-// // 设置测试用的目录
|
|
|
|
|
-// String inputPath = "D:/测试用图片/";
|
|
|
|
|
-// // 设置测试用的输出目录
|
|
|
|
|
-// String outputPath = "D:/测试用图片/convert/";
|
|
|
|
|
-// // 设置目标打印宽度,根据设置的宽度来计算缩放倍数
|
|
|
|
|
-// float printWidth = 1500;
|
|
|
|
|
-//
|
|
|
|
|
-//
|
|
|
|
|
-// read("https://test.njnet.vip/public/img/custom/20190408/result-front-NEM0q25Cp9c7.png");
|
|
|
|
|
-//
|
|
|
|
|
-////
|
|
|
|
|
-//// saveWebImg("https://test.njnet.vip/public/images/picUpload/platform/20190403/20190403111904010.png",
|
|
|
|
|
-//// "d:/test.png");
|
|
|
|
|
-//
|
|
|
|
|
-// // 设置输入文件
|
|
|
|
|
-// File inputImage = new File(inputPath + fileName);
|
|
|
|
|
-// // 设置输出文件
|
|
|
|
|
-//
|
|
|
|
|
-// String nweImagePath = outputPath + "new_" + fileName;
|
|
|
|
|
-// File outputImage = new File(outputPath + "new_" + fileName);
|
|
|
|
|
-// // 读取输入的图片文件
|
|
|
|
|
-// ImageIcon oImageIcon = new ImageIcon(inputPath + fileName);
|
|
|
|
|
-// // 获取原始图像的宽度和高度
|
|
|
|
|
-// int oWidth = oImageIcon.getIconWidth();
|
|
|
|
|
-// // 计算缩放比例
|
|
|
|
|
-// float scaleSize = printWidth / oWidth;
|
|
|
|
|
-// // 读取源图
|
|
|
|
|
-// BufferedImage tProcessImage = ImageIO.read(inputImage);
|
|
|
|
|
-// // 进行缩放
|
|
|
|
|
-// tProcessImage = resize(tProcessImage, scaleSize);
|
|
|
|
|
-// // 进行旋转
|
|
|
|
|
-// tProcessImage = rotate(tProcessImage, 0);
|
|
|
|
|
-// // 叠加图片
|
|
|
|
|
-// BufferedImage tAddImage = ImageIO.read(new File(inputPath + "transformer.jpg"));
|
|
|
|
|
-//
|
|
|
|
|
-// tAddImage = resize(tAddImage, 1);
|
|
|
|
|
-//
|
|
|
|
|
-// tProcessImage = addImage(tProcessImage, tAddImage, 100, 100);
|
|
|
|
|
-// // 添加文字
|
|
|
|
|
-// BufferedImage tStringImage = null;
|
|
|
|
|
-//// tProcessImage = processImageWithWaterMark(tProcessImage, 750, 750, "第一个大大的水印", Color.MAGENTA, new Font("微软雅黑", Font.PLAIN, 150), 90);
|
|
|
|
|
-//// tProcessImage = processImageWithWaterMark(tProcessImage, 0, 200, "第二个大大的水印", Color.MAGENTA, new Font("宋体", Font.PLAIN, 150), 0);
|
|
|
|
|
-//// tStringImage = processImageWithWaterMark(tProcessImage, 750, 250, "第一个大大的水印", Color.MAGENTA, new Font("微软雅黑", Font.PLAIN, 150), 0);
|
|
|
|
|
-// tStringImage = resize(tStringImage, 300, 400);
|
|
|
|
|
-// tProcessImage = addImage(tProcessImage, tStringImage, 100, 100);
|
|
|
|
|
-// // 进行移动
|
|
|
|
|
-// tProcessImage = move(tProcessImage, 0, 0);
|
|
|
|
|
-//
|
|
|
|
|
-// makeImageFile(tProcessImage, nweImagePath);
|
|
|
|
|
-
|
|
|
|
|
-// long start = System.currentTimeMillis();
|
|
|
|
|
-// ImageIO.read(new URL("https://test.njnet.vip/public/img/custom/20190408/result-front-NEM0q25Cp9c7.png"));
|
|
|
|
|
-// long end = System.currentTimeMillis();
|
|
|
|
|
-// System.out.println("convert finish:"+ (end - start)+"ms");
|
|
|
|
|
-
|
|
|
|
|
-// BufferedImage transparencyBufferedImage = createTransparencyBufferedImage(100, 100);
|
|
|
|
|
-// makeImageFile(transparencyBufferedImage, "c:/app/test.png");
|
|
|
|
|
-//
|
|
|
|
|
-
|
|
|
|
|
- List<String> list = new ArrayList<String>(){{
|
|
|
|
|
- add("你好");
|
|
|
|
|
- add("厉害");
|
|
|
|
|
- }};
|
|
|
|
|
- Font font = new Font("宋体", Font.PLAIN, 30);
|
|
|
|
|
-
|
|
|
|
|
- makeImageFile(test(list, font, Color.black, 20), "D:/test.png");
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|