이 기사에서는 Java가 구현 한 WeChat 이미지 처리 도구 클래스에 대해 설명합니다. 다음과 같이 참조에 대해 공유하십시오.
이제 외부에는 사진과 기사가 적습니다. 사본 코드를 읽었지만 사용할 수는 없습니다. 해당 JAR 패키지를 사용하여 처리하고 많은 사람들이 비례 적으로 확장됩니다.이 도구 클래스는 이전에 WeChat 프린터로 작성된 RGB를 기반으로 한 Java를 기반으로하는 클래스입니다.
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.bermeredimage; 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 이미지 처리 도구 * * @author zhuang. logger logger = loggerfactory.getLogger (picturetool.class); public static void main (String [] args)은 ioexception {file fileOne = new File ( "c : //1.jpg"); bufferedImage imageFirst = imageIO.Read (fileOne); int border = 0; ImageFirst = Crop (ImageFirst, 0,10,297,300); 파일 Outfile = 새 파일 ( "D : //2.jpg"); imageio.write (ImageFirst, "JPG", Outfile); // 그림 쓰기}/ *** x 수직 복합 이미지의 좌표 픽셀*/ 개인 최종 정적 int y_width = 645; / *** 표준 이미지의 y 좌표 픽셀, 920은 일반 사진입니다. 1099는 스탬프 사진*/ 개인 최종 정적 int y_height = 920; / *** Crop X 좌표 압축 된 픽셀*/ 개인 최종 정적 int x_retract = 50; / *** 작물 y 조정 된 픽셀*/ 개인 최종 정적 int y_retract = 50; / ** * 시스템 기본 이미지 테두리는 20 */ public final static int border = 20입니다. / *** 수평 합성 이미지*/ public static void xpic (문자열 첫 번째, 문자열 두 번째, 문자열 출력) {try {/* 1 첫 번째 이미지를 읽습니다*/ file fileOne = 새 파일 (첫 번째); bufferedImage imageFirst = imageIO.Read (fileOne); int width = imagefirst.getWidth (); // 이미지 너비 int height = imageFirst.getheight (); // 이미지 높이 int [] imageAreArrayFirst = new int [width * height]; // imageArrayfirst = imageFirst.getrgb (0, 0, width, imageArrayfirst, 0, 0, width); /* 1 두 번째 이미지에 대해 동일하게 수행*/ file filetwo = 새 파일 (두 번째); BufferedImage imagesecond = imageio.read (filetwo); int widthtwo = imagesecond.getWidth (); // 이미지 너비 int heightwo = imagesecond.getheight (); // image height int [] imageAreraysecond = new int [widthtwo * remattwo]; imageArraysecond = imagesecond.getRGB (0, 0, widthtwo, heighttwo, imageArraysecond, 0, widthtwo); int h = 높이; if (height <heighttwo) {h = heighttwo; } // 새 이미지 생성 BufferedImage imageresult = new bufferedImage (width + widthtwo, h, bufferedImage.type_int_rgb); imageResult.setrgb (0, 0, 너비, 높이, imageArrayfirst, 0, 너비); // rgb imageresult.setrgb (width, 0, widthtwo, heighttwo, imagearraysecond, 0, widthtwo); // 오른쪽 하부에서 RGB 파일을 설정하는 경우 (out); imageio.write (imageresult, "jpg", attfile); // 이미지 작성} catch (예외 e) {logger.error ( "수평 합성 이미지는 오류 ...", e); }} / *** 수직 합성 이미지** @param first* 이미지 경로를 위에 올려 놓으십시오* @param sec { /* 1 첫 번째 이미지를 읽으십시오* / file fileOne = 새 파일 (첫 번째); bufferedImage imageFirst = imageIO.Read (fileOne); int width = imageFirst.getWidth (); // 이미지 너비 int height = im BufferedImage imagesecond = imageio.read (filetwo); int widthtwo = imagesecond.getWidth (); // 이미지 너비 int heightwo = imagesecond.getheight (); // 이미지 높이/ * 1 첫 번째 이미지 읽기 시작 */ int t_height = y_height -heighttwo; // 그림은 수평 이미지이며, 시계 반대 방향으로 90도 회전하고 (너비> 높이) {imageFirst = rotateImageleft90 (imageFirst); } // 동일한 스케일링 imageFirst = resize (imageFirst, y_width, t_height); // 스케일링 이후 이미지의 크기 = imageFirst.getWidth (); // 이미지 너비 높이 = im 이미지 부울 A_W, A_H = FALSE; if ((a_w = (width> y_width)) || (a_h = (높이> t_height)) {// start position x, y coordinate int s_w = 0, s_h = 0; // X 좌표를 자르면 속성 속성 x_retract if (a_w) {int temp = width -y_width; if (temp> x_retract) {temp = x_retract; } else {temp = 0; } s_w = s_w + 온도; } // y 좌표를 자르면 속성 속성 y_retract if (a_h) {int temp = height -t_height; if (temp> y_retract) {temp = y_retract; } else {temp = 0; } S_H = S_H + 온도; } imageFirst = Crop (ImageFirst, S_W, S_H, Y_WIDTH, T_HEIGHT); 너비 = imageFirst.getWidth (); height = imagefirst.getheight (); } int [] imageAreArrayFirst = new int [(width -border) * height]; // rgb imageArrayfirst = imageFirst.getRGB (Porder, 0, 0, (폭 - 테두리), 높이, imageArrayfirst, 0, (width -Border); / * 2. 두 번째 이미지에 대해 동일한 프로세스를 시작합니다. imageArraysecond = imagesecond.getRGB (0, 0, widthtwo, heighttwo, imageArraysecond, 0, widthtwo); int w = 너비; if (width <widthtwo) {w = widthtwo; } // 이미지 높이 INT H = 높이 + 높이; // 새 이미지 생성 BufferedImage ImageSult = New BufferedImage (w, h, bufferedimage.type_int_rgb); // 검은 색 배경을 풀고, 기본 유형 _int_rgb는 모두 0이며, 모두 검은 색 그래픽 2d g = (Graphics2d) imageresult.creategraphics (); G. 세트 컬러 (Color.white); G.FillRect (0, 0, W, H); // 전체 화면을 채우십시오 g.dispose (); // 국경 imageSult.setrgb (테두리, 0, (폭 - 경계 * 2), 높이, imageArrayfirst, 0, 0, (너비 - 경계); // 왼쪽 반 ImageSult.SetRGB에서 RGB를 설정합니다 파일 (out); imageio.write (imageresult, "jpg", atfile); // image} catch (예외 e) {logger.error ( "수직 합성 이미지 실패 ...", e); isok = false; } return isok; } / *** 전체 이미지 인쇄, 이미지 스케일링 및 회전 처리** @param 소스* 이미지 보류 처리* @param out* 파일 출력 디렉토리* @param border* 이미지 예약 된 테두리* / public static boolean maigaopic (문자열 소스, 문자열 out) {boolean isok = true; { /* 1 첫 번째 이미지를 읽으십시오* / file fileOne = 새 파일 (소스); bufferedImage imageFirst = imageIO.Read (fileOne); int width = imagefirst.getWidth (); // 이미지 너비 int height = imageFirst.getheight (); // 이미지 높이 // 그림은 수평 그림이며 90도 회전하고 동일하게 (width> height) {imageFirst = rotateImageleft90 (imageFirst); } // 평등 스케일링 imageFirst = resize (imageFirst, y_width, y_height); // 스케일링 이후 이미지의 크기 = imageFirst.getWidth (); // 이미지 너비 높이 = im 이미지 부울 A_W, A_H = FALSE; if ((a_w = (width> y_width)) || (a_h = (높이> y_height))) {// start position x, y coordinate int s_w = 0, s_h = 0; // X 좌표를 자르면 속성 속성 x_retract if (a_w) {int temp = width -y_width; if (temp> x_retract) {temp = x_retract; } else {temp = 0; } s_w = s_w + 온도; } // y 좌표를 자르면 속성 속성 y_retract if (a_h) {int temp = height -y_height; if (temp> y_retract) {temp = y_retract; } else {temp = 0; } S_H = S_H + 온도; } imageFirst = Crop (ImageFirst, S_W, S_H, Y_WIDTH, Y_HEIGHT); 너비 = imageFirst.getWidth (); height = imagefirst.getheight (); } int [] imageAreArrayFirst = new int [(width -border) * height]; // 이미지에서 rgb 읽기 imageArrayFirst = imageFirst.getRGB (테두리, 0, (폭 - 경계), 높이, imageAreArayfirst, 0, (폭 - 테두리); // 새 이미지 생성 BufferedImage ImageSult = New BufferedImage (너비, 높이, BufferedImage.type_int_rgb); // 검은 색 배경을 풀고, 기본 유형 _int_rgb는 모두 0이며, 모두 검은 색 그래픽 2d g = (Graphics2d) imageresult.creategraphics (); G. 세트 컬러 (Color.white); G.FillRect (0, 0, 너비, 높이); // 전체 화면을 채우십시오 g.dispose (); // 국경을 남기고 imageSult.setrgb (테두리, 0, (너비 - 국경 * 2), 높이, imageAreArrayFirst, 0, (너비 -Portle)); // 왼쪽 반에 RGB 파일 출시를 설정합니다. imageio.write (imageresult, "jpg", attfile); // 이미지 작성} catch (ioexception e) {logger.error ( "전체 이미지 인쇄, 이미지 스케일링 및 회전 처리 실패 ...", e); isok = false; } return isok; }/ ** * 이미지의 평등 라디 디오 줌을 구현 * @param 소스 * 이미지 스트림 처리 * @param targetw * width * @param targeth * @return */ public static bufferedimage resize (bufferedimage source, int targeth, int targeth) {int width = source.getwidth (); Zoominimage (소스, Targetw, Targeth); // 이미지 너비와 높이가 너무 작고 힘은 이미지를 확대합니다/* if (width <targetw && height <targeth) {return Zoominimage (소스, targetw, targeth); } else if ((width <targetw && width == 높이) || (높이 <targeth && width == 높이)) {return Zoominimage (소스, targetw, targeth); } return null; */}/ ** * 이미지를 크기로 자르십시오 * * @param 소스 * 보류 이미지 스트림 * @param startx * start x coderinate * @param starty * start y coordinate * @param endx * end x coderinate * @param endy * end y coordinate */ public static bufferedimage crup (bufferedimage source, int startx, int startx, inty, inty) source.getWidth (); int height = source.getheight (); if (startx <= -1) {startx = 0; } if (starty <= -1) {Starty = 0; } if (endx <= -1) {endx = 너비 -1; } if (endy <= -1) {endy = 높이 -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); }} 반환 결과; } / ** * 이미지를 지정된 각도로 회전시킵니다 * @param bufferedImage * target image * @param dec 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 (학위), w / 2, H / 2 + (w> h? (w -h) / 2 : (h -w) / 2)); Graphics2d.DrawImage (bufferedImage, 0, 0, null); Graphics2d.dispose (); 반환 IMG; } / ** * 이미지가 좌회전 90도 회전 * * @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 (); 반환 IMG; } / ** * 이미지가 오른쪽으로 90도 회전 * * @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 (); 반환 IMG; } // 공개 파일 전달 rotateImageOppo (File File)는 예외를 {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", 파일); 반환 파일; } / *** * 이미지 미러링 처리 * * @param 파일 * @param fx * 0은 위아래로 반전이 왼쪽 및 오른쪽 반전입니다 * @return * / public void imagemisro (파일 파일, int fx) {try {bufferedimage bufferedimage = imageio.read (파일); 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; }}} 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 (예외 e) {e.printstacktrace (); }} / ** * 이미지를 강제로 축소하거나 축소하십시오 * @param originalImage * originalImage * @return * / public static bufferedimage Zoominimage (bufferedImage) OriginalImage, int width, int height) {bufferedImage newImage = new bufferedImage (너비, 높이, 원본 .getType (); 그래픽 g = newImage.getGraphics (); G.DrawImage (OriginalImage, 0, 0, 너비, 높이, 널); g.dispose (); Newimage를 반환하십시오. } / *** 간단한 그림 인식 원칙** @param img* 이미지 경로* / public static void williMg (String img) {try {file fileOne = new File (IMG); BufferedImage bi = imageio.Read (FileOne); // 이미지의 너비와 높이를 가져옵니다 int width = bi.getWidth (); int height = bi.getheight (); // (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 (예외 e) {logger.error ( "이미지 인식 오류", e); }}}더 많은 Java 관련 컨텐츠를 위해이 사이트에 관심이있는 독자는 "Java 이미지 운영 기술 요약", "Java 날짜 및 시간 운영 기술 요약", "Java Operation Dom Node Skills 요약", "Java 파일 및 디렉토리 운영 기술 요약"및 "Java 데이터 구조 및 알고리즘의 자습서"를 볼 수 있습니다.
이 기사가 모든 사람의 Java 프로그래밍에 도움이되기를 바랍니다.