This article describes the WeChat image processing tool class implemented by Java. Share it for your reference, as follows:
Now, there are fewer pictures and articles outside. I read the copy code but cannot use it. I use the corresponding jar package to process it, and many are scaled in proportion, which cannot meet the expectations I want: This tool class is a class based on Java based on RGB written in WeChat printers before.
package com.zjpz.util;import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.RenderingHints;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import java.awt.image.ColorModel;import java.awt.image.WritableRaster;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * WeChat image processing tool* * @author zhuang.y * */public class PictureTool { protected static Logger logger = LoggerFactory.getLogger(PictureTool.class); public static void main(String[] args) throws IOException { File fileOne = new File("c://1.jpg"); BufferedImage imageFirst = ImageIO.read(fileOne); int border = 0; imageFirst =crop(imageFirst,0,10,297,300); File outFile = new File("d://2.jpg"); ImageIO.write(imageFirst, "jpg", outFile);// Write picture} /** * x coordinate pixel of vertical composite image*/ private final static int y_width = 645; /** * The y coordinate pixel of standard image, 920, is a general photo, 1099 is a stamp photo*/ private final static int y_height = 920; /** * Crop x coordinates indented pixels*/ private final static int x_retract = 50; /** * Crop y coordinates indented pixels*/ private final static int y_retract = 50; /** * The system default image border is 20 */ public final static int BORDER = 20; /** * Horizontal synthetic image*/ public static void xPic(String first, String second, String out) { try { /* 1 Read the first image*/ File fileOne = new File(first); BufferedImage imageFirst = ImageIO.read(fileOne); int width = imageFirst.getWidth();// Image width int height = imageFirst.getHeight();// Image height int[] imageArrayFirst = new int[width * height];// Read RGB from the image imageArrayFirst = imageFirst.getRGB(0, 0, width, height, imageArrayFirst, 0, width); /* 1 Do the same for the second image*/ File fileTwo = new File(second); BufferedImage imageSecond = ImageIO.read(fileTwo); int widthTwo = imageSecond.getWidth();// Image width int heightTwo = imageSecond.getHeight();// Image height int[] imageArraySecond = new int[widthTwo * heightTwo]; imageArraySecond = imageSecond.getRGB(0, 0, widthTwo, heightTwo, imageArraySecond, 0, widthTwo); int h = height; if (height < heightTwo) { h = heightTwo; } // Generate a new image BufferedImage imageResult = new BufferedImage(width + widthTwo, h, BufferedImage.TYPE_INT_RGB); imageResult.setRGB(0, 0, width, height, imageArrayFirst, 0, width);// Set RGB imageResult.setRGB(width, 0, widthTwo, heightTwo, imageArraySecond, 0, widthTwo);// Set RGB File outFile in the right half = new File(out); ImageIO.write(imageResult, "jpg", outFile);// Write an image} catch (Exception e) { logger.error("The horizontal synthesis image was Error...", e); } } /** * Vertical synthesis image* * @param first * Put the image path above* @param second * Put the image path below* @param out * File output directory* @param border * Image reserved border*/ public static boolean yPic(String first, String second, String out, int border) { boolean isOk = true; try { /* 1 Read the first image*/ File fileOne = new File(first); BufferedImage imageFirst = ImageIO.read(fileOne); int width = imageFirst.getWidth();// Image width int height = imageFirst.getHeight();// Image height/* 2 Do the same for the second image*/ File fileTwo = new File(second); BufferedImage imageSecond = ImageIO.read(fileTwo); int widthTwo = imageSecond.getWidth();// Image width int heightTwo = imageSecond.getHeight();// Image height/* 1 Read the first image begin */ int t_height = y_height - heightTwo; // The picture is a horizontal image, rotate 90 degrees counterclockwise and scale equally if (width > height) { imageFirst = rotateImageLeft90(imageFirst); } // Equal scaling imageFirst = resize(imageFirst, y_width, t_height); // Size of the image after scaling width = imageFirst.getWidth();// Image width height = imageFirst.getHeight();// Image height// After equal-radical scaling, the image is still too large. Crop the image boolean a_w, a_h = false; if ((a_w = (width > y_width)) || (a_h = (height > t_height))) { // Start position x,y coordinate int s_w = 0, s_h = 0; // When cropping x coordinates, indent attribute x_retract if (a_w) { int temp = width - y_width; if (temp > x_retract) { temp = x_retract; } else { temp = 0; } s_w = s_w + temp; } // When cropping y coordinates, indent attribute y_retract if (a_h) { int temp = height - t_height; if (temp > y_retract) { temp = y_retract; } else { temp = 0; } s_h = s_h + temp; } imageFirst = crop(imageFirst, s_w, s_h, y_width, t_height); width = imageFirst.getWidth(); height = imageFirst.getHeight(); } int[] imageArrayFirst = new int[(width - border) * height];// Read RGB imageArrayFirst = imageFirst.getRGB(border, 0, (width - border), height, imageArrayFirst, 0, (width - border)); /* 2Do the same process for the second image begin */ int[] imageArraySecond = new int[widthTwo * heightTwo]; imageArraySecond = imageSecond.getRGB(0, 0, widthTwo, heightTwo, imageArraySecond, 0, widthTwo); int w = width; if (width < widthTwo) { w = widthTwo; } // Image height int h = height + heightTwo; // Generate a new image BufferedImage imageResult = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); // Solve the black background, the default TYPE_INT_RGB are all 0, and they are all black Graphics2D g = (Graphics2D) imageResult.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, w, h); // Fill the entire screen g.dispose(); // Leave the border imageResult.setRGB(border, 0, (width - border * 2), height, imageArrayFirst, 0, (width - border));// Set RGB in the left half imageResult.setRGB(0, height, widthTwo, heightTwo, imageArraySecond, 0, widthTwo);// Set RGB in the right half File outFile = new File(out); ImageIO.write(imageResult, "jpg", outFile);// Write an image} catch (Exception e) { logger.error("Perpendicular synthesis image failed...", e); isOk = false; } return isOk; } /** * Full image printing, image scaling and rotation processing* * @param source * Image pending processing* @param out * File output directory after processing* @param border * Image reserved border*/ public static boolean maigaoPic(String source, String out, int border) { boolean isOk = true; try { /* 1 Read the first image*/ File fileOne = new File(source); BufferedImage imageFirst = ImageIO.read(fileOne); int width = imageFirst.getWidth();// Image width int height = imageFirst.getHeight();// Image height// The picture is a horizontal picture, rotate 90 degrees counterclockwise and scale equally if (width > height) { imageFirst = rotateImageLeft90(imageFirst); } // Equality scaling imageFirst = resize(imageFirst, y_width, y_height); // Size of the image after scaling width = imageFirst.getWidth();// Image width height = imageFirst.getHeight();// Image height// After equality scaling, the picture is still too large. Crop the image boolean a_w, a_h = false; if ((a_w = (width > y_width)) || (a_h = (height > y_height))) { // Start position x,y coordinate int s_w = 0, s_h = 0; // When cropping x coordinates, indent attribute x_retract if (a_w) { int temp = width - y_width; if (temp > x_retract) { temp = x_retract; } else { temp = 0; } s_w = s_w + temp; } // When cropping y coordinates, indent attribute y_retract if (a_h) { int temp = height - y_height; if (temp > y_retract) { temp = y_retract; } else { temp = 0; } s_h = s_h + temp; } imageFirst = crop(imageFirst, s_w, s_h, y_width, y_height); width = imageFirst.getWidth(); height = imageFirst.getHeight(); } int[] imageArrayFirst = new int[(width - border) * height];// Read RGB from the image imageArrayFirst = imageFirst.getRGB(border, 0, (width - border), height, imageArrayFirst, 0, (width - border)); // Generate a new image BufferedImage imageResult = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Solve the black background, the default TYPE_INT_RGB are all 0, and they are all black Graphics2D g = (Graphics2D) imageResult.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); // Fill the entire screen g.dispose(); // Leave the border imageResult.setRGB(border, 0, (width - border * 2), height, imageArrayFirst, 0, (width - border));// Set the RGB File outFile in the left half = new File(out); ImageIO.write(imageResult, "jpg", outFile);// Write an image} catch (IOException e) { logger.error("Full image printing, image scaling and rotation processing failed...", e); isOk = false; } return isOk; } /** * Implement equal-radio zoom of the image* * @param source * Image stream to be processed* @param targetW * Width* @param targetH * Height* @return */ public static BufferedImage resize(BufferedImage source, int targetW, int targetH) { int width = source.getWidth();// Image width and height = source.getHeight();// Image height return zoomInImage(source, targetW, targetH); // Image width and height are too small, force enlarge the image/* if (width < targetW && height < targetH) { return zoomInImage(source, targetW, targetH); } else if ((width < targetW && width == height) || (height < targetH && width == height)) { return zoomInImage(source, targetW, targetH); } return null; */ } /** * Crop the image to scale* * @param source * Pending image stream* @param startX * Start x coordinate* @param startY * Start y coordinate* @param endX * End x coordinate* @param endY * End y coordinate* @return */ public static BufferedImage crop(BufferedImage source, int startX, int startY, int endX, int endY) { int width = source.getWidth(); int height = source.getHeight(); if (startX <= -1) { startX = 0; } if (startY <= -1) { startY = 0; } if (endX <= -1) { endX = width - 1; } if (endY <= -1) { endY = height - 1; } BufferedImage result = new BufferedImage(endX, endY , source.getType()); for (int y = startY; y < endY+startY; y++) { for (int x = startX; x < endX+startX; x++) { int rgb = source.getRGB(x, y); result.setRGB(x - startX, y - startY, rgb); } } return result; } /** * Rotate the image to the specified angle* * @param bufferedimage * Target image* @param degree * Rotate the angle* @return */ public static BufferedImage rotateImage(final BufferedImage bufferedimage, final int degree) { int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int type = bufferedimage.getColorModel().getTransparency(); BufferedImage img; Graphics2D graphics2d; (graphics2d = (img = new BufferedImage(h, w, type)).createGraphics()).setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2 + (w > h ? (w - h) / 2 : (h - w) / 2)); graphics2d.drawImage(bufferedimage, 0, 0, null); graphics2d.dispose(); return img; } /** * Image turns left 90 degrees* * @param bufferedimage * @return */ public static BufferedImage rotateImageLeft90(BufferedImage bufferedimage) { int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int type = bufferedimage.getColorModel().getTransparency(); BufferedImage img; Graphics2D graphics2d; (graphics2d = (img = new BufferedImage(h, w, type)).createGraphics()).setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(270), w / 2, h / 2 + (w - h) / 2); graphics2d.drawImage(bufferedimage, 0, 0, null); graphics2d.dispose(); return img; } /** * Image turns right 90 degrees* * @param bufferedimage * @return */ public static BufferedImage rotateImageRight90(BufferedImage bufferedimage) { int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int type = bufferedimage.getColorModel().getTransparency(); BufferedImage img; Graphics2D graphics2d; (graphics2d = (img = new BufferedImage(h, w, type)).createGraphics()).setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(90), w / 2 - (w - h) / 2, h / 2); graphics2d.drawImage(bufferedimage, 0, 0, null); graphics2d.dispose(); return img; } // Forward public File rotateImageOppo(File file) throws Exception { BufferedImage bufferedimage = ImageIO.read(file); int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int type = bufferedimage.getColorModel().getTransparency(); BufferedImage img; Graphics2D graphics2d; (graphics2d = (img = new BufferedImage(w, h, type)).createGraphics()).setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(180), w / 2, h / 2); graphics2d.drawImage(bufferedimage, 0, 0, null); graphics2d.dispose(); ImageIO.write(img, "jpg", file); return file; } /*** * Image mirroring processing* * @param file * @param FX * 0 is up and down inversion 1 is left and right inversion* @return */ public void imageMisro(File file, int FX) { try { BufferedImage bufferedimage = ImageIO.read(file); int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int[][] datas = new int[w][h]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { datas[j][i] = bufferedimage.getRGB(j, i); } } int[][] tmps = new int[w][h]; if (FX == 0) { for (int i = 0, a = h - 1; i < h; i++, a--) { for (int j = 0; j < w; j++) { tmps[j][a] = datas[j][i]; } } } else if (FX == 1) { for (int i = 0; i < h; i++) { for (int j = 0, b = w - 1; j < w; j++, b--) { tmps[b][i] = datas[j][i]; } } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { bufferedimage.setRGB(j, i, tmps[j][i]); } } ImageIO.write(bufferedimage, "jpg", file); } catch (Exception e) { e.printStackTrace(); } } /** * Force zoom in or out of the image* * @param originalImage * OriginalImage * @return */ public static BufferedImage zoomInImage(BufferedImage) originalImage, int width, int height) { BufferedImage newImage = new BufferedImage(width, height, originalImage.getType()); Graphics g = newImage.getGraphics(); g.drawImage(originalImage, 0, 0, width, height, null); g.dispose(); return newImage; } /** * Simple picture recognition principle* * @param img * Image path*/ public static void discernImg(String img) { try { File fileOne = new File(img); BufferedImage bi = ImageIO.read(fileOne); // Get the width and height of the image int width = bi.getWidth(); int height = bi.getHeight(); // Scan the image for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) {// Row scan int dip = bi.getRGB(j, i); if (dip == -1) System.out.print(" "); else System.out.print(" ♦"); } System.out.println();// Line break} } catch (Exception e) { logger.error("Image recognition error", e); } }}For more Java-related content, readers who are interested in this site can view the topics: "Summary of Java Image Operation Skills", "Summary of Java Date and Time Operation Skills", "Summary of Java Operation DOM Node Skills", "Summary of Java File and Directory Operation Skills" and "Tutorials of Java Data Structure and Algorithm".
I hope this article will be helpful to everyone's Java programming.