Dieser Artikel beschreibt die von Java implementierte WeChat -Image -Verarbeitungs -Toolklasse. Teilen Sie es für Ihre Referenz wie folgt weiter:
Jetzt gibt es draußen weniger Bilder und Artikel. Ich habe den Kopiercode gelesen, kann aber nicht verwenden. Ich benutze das entsprechende JAR -Paket, um es zu verarbeiten, und viele sind proportional skaliert, was die gewünschten Erwartungen nicht erfüllen kann: Diese Werkzeugklasse ist eine Klasse, die auf Java basiert, die auf RGB basiert, die zuvor in WeChat -Druckern geschrieben wurden.
Paket com.zjpz.util; Import Java.awt.Color; Import Java.awt.graphics; Import Java.awt.graphics2d; Import Java.awt.RenderingHints; java.awt.image.writableraster; import Java.io.file; import Java.io.ioException; Import Javax.imageo.imageo; org.slf4j.logger; Logger logger = loggerfactory.getLogger (picturetool.class); public static void main (String [] args) löst IOException {FileOne = new Datei ("c: //1.jpg") aus; Bufferedimage imageFirst = imageio.read (fileOne); int border = 0; ImageFirst = Crop (ImageFirst, 0,10,297,300); Datei outfile = new Datei ("d: //2.jpg"); Imageio.Write (ImageFirst, "JPG", Outfile); // Bild schreiben}/ *** x Koordinatenpixel des vertikalen kombinierten Bildes*/ privat endgültiges statisches statisches int y_width = 645; / *** Das y -Koordinatenpixel des Standardbildes, 920, ist ein allgemeines Foto, 1099 ist ein Briefmarkenfoto*/ private endgültige statische int y_height = 920; / *** Crop X -Koordinaten eingedrungene Pixel*/ Private endgültige statische int x_retract = 50; / *** Crop y -Koordinaten Eingeklagte Pixel*/ Private endgültige statische statische int y_retract = 50; / ** * Der System Standard Image Grenze ist 20 */ öffentliches endgültiges statisches int Grenze = 20; / *** Horizontales synthetisches Bild*/ public static void XPIC (String zuerst, String zweitens, String out) {try {/* 1 Lesen Sie das erste Bild*/ fileone = new Datei (zuerst); 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 tun dasselbe für das zweite Bild*/ Datei Filetwo = neue Datei (zweite); BufferedImage mictageSecond = imageio.read (Filetwo); int widthtwo = mictuationSecond.getWidth (); // Bildbreite inthohdtwo = imagebotscond.getheight (); // Bildhöhe int [] imagearraySecond = new int [widthtwo * heighttwo]; ImagearraySecond = ImagebotiumSecond.getRGB (0, 0, Widthtwo, Hightwo, ImagearraySecond, 0, Widthtwo); int H = Höhe; if (Höhe <heighttwo) {h = heighttwo; } // generieren Sie ein neues Bild BufferedImage Imageresult = new bufferedImage (width + widthtwo, h, bufferedimage.type_int_rgb); ImageSult.setRGB (0, 0, Breite, Höhe, ImageArrayFirst, 0, Breite); // RGB ImageSult.setRGB (Breite, 0, Widthtwo, Heighttwo, ImagearraySecond, 0, Widthtwo); // RGB -Datei aus der rechten Halbzahlung in der richtigen Halbzahlung (Out). ImageIo.Write (ImageSult, "JPG", Outfile); // ein Bild schreiben} catch (Ausnahme E) {logger.Error ("Das Bild des horizontalen Synthese war Fehler ...", e); }} / *** vertikales Synthesebild** @param zuerst* den Bildpfad über* @param zweitens* Setzen Sie den Bildpfad unten. Versuchen Sie { /* 1 Lesen Sie das erste Bild* / fileOne = new Datei (zuerst); Bufferedimage imageFirst = imageio.read (fileOne); int width = imageFirst.getWidth (); // Bildbreite inthohe = imageFirst.getheight (); // Bildhöhe/* 2 DIESE SEITE FÜR DAS SCHWENDE AUSBILDUNG*/ Datei Filetwo = neue Datei (Sekunde); BufferedImage mictageSecond = imageio.read (Filetwo); int widthtwo = mictuationSecond.getWidth (); // Bildbreite inthohdtwo = imagebotscond.getheight (); // Bildhöhe/ * 1 Lesen Sie das erste Bild begin */ int t_Height = y_Height - Hightwo; // Das Bild ist ein horizontales Bild, drehen Sie 90 Grad gegen den Uhrzeigersinn und skalieren gleich, wenn (Breite> Höhe) {ImageFirst = rotateImageleft90 (ImageFirst); } // gleich scaling imageFirst = resize (ImageFirst, y_width, t_height); // Größe des Bildes nach Skalierungsbreite = ImageFirst.getWidth (); // Bildbreite Höhe = ImageFirst.getheight (); // Bildhöhe // Nach gleichradikaler Skalierung ist das Bild noch zu groß. Ernte das Bild boolean a_w, a_h = false; if ((a_w = (width> y_width)) || (a_h = (Höhe> T_Height)) {// Startposition x, y Koordinate int s_w = 0, s_h = 0; // Beim Anschneiden von X -Koordinaten das Indent -Attribut 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; } // Beim Anschneiden von Y -Koordinaten werden das Attribut y_retract if (a_h) {int temp = Höhe - 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 (); Höhe = ImageFirst.Getheight (); } int [] imageArrayfirst = new int [(width - border) * Höhe]; // RGB ImageArrayFirst = ImageFirst.getRGB (Rand, 0, (Breite - Grenze), Höhe, ImageRrayFirst, 0, (Breite - Rand)); / * 2do denselben Prozess für das zweite Bild begin */ int [] imagearraySecond = new int [widthtwo * heighttwo]; ImagearraySecond = ImagebotiumSecond.getRGB (0, 0, Widthtwo, Hightwo, ImagearraySecond, 0, Widthtwo); int w = width; if (Breite <widthtwo) {w = widthtwo; } // Bildhöhe int H = Höhe + hohes Heighttwo; // generieren Sie ein neues Bild BufferedImage ImageSult = new bufferedImage (W, H, bufferedimage.type_int_rgb); // Lösen Sie den schwarzen Hintergrund, die Standard -Typ_int_RGB sind alle 0 und sie sind alle schwarze graphics2d g = (Graphics2d) ImagMesult.CreateGraphics (); G.SetColor (color.white); G.FillRect (0, 0, W, H); // den gesamten Bildschirm füllen g.disponse (); // Lasse die Grenze imageresult.setrgb (Grenze, 0, (Breite - Grenze * 2), Höhe, ImageArrayFirst, 0, (Breite - Grenze); // RGB in der linken Halbzeit imageresult.setrgb (0, Höhe, Widthtwo, Heighttwo, Imagarraysult.Setrgb); Datei (out); ImageIo.Write (ImageSult, "JPG", Outfile); // ein Bild schreiben} catch (Ausnahme E) {logger.Error ("Perpendikuläres Synthese -Bild fehlgeschlagen ...", e); isok = false; } return isok; } / *** Vollständiger Bilddruck, Bildskalierung und Rotationsverarbeitung** @param Source* Bild anhängige Verarbeitung* @param out* Dateiausgabeverzeichnis nach der Verarbeitung* @param Border* Image Reserved Border* / public static boolaopic (String Quelle, String Out, Int Border) {boolean isok = true; Versuchen Sie { /* 1 Lesen Sie das erste Bild* / fileOne = new Datei (Quelle); Bufferedimage imageFirst = imageio.read (fileOne); int width = imageFirst.getWidth (); // Bildbreite inthohe = imageFirst.getheight (); // Bildhöhe // Das Bild ist ein horizontales Bild, drehen Sie 90 Grad gegen den Uhrzeigersinn und skalieren gleich, wenn (Breite> Höhe) {ImageFirst = rotateImageleft90 (Bildfirst); } // Gleichheitskalierung imageFirst = Größe (ImageFirst, y_width, y_height); // Größe des Bildes nach Skalierungsbreite = ImageFirst.getWidth (); // Bildbreite Höhe = ImageFirst.getheight (); // Bildhöhe // Nach der Skalierung der Gleichstellung ist das Bild noch zu groß. Ernte das Bild boolean a_w, a_h = false; if ((a_w = (width> y_width)) || (a_h = (Höhe> y_Height))) {// Startposition x, y Koordinate int s_w = 0, s_h = 0; // Beim Anschneiden von X -Koordinaten das Indent -Attribut 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; } // Beim Anschneiden von Y -Koordinaten werden Sie das Attribut y_retract if (a_h) {int temp = Höhe - 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 (); Höhe = ImageFirst.Getheight (); } int [] imageArrayfirst = new int [(width - border) * Höhe]; // Lesen Sie RGB aus dem Bild imageArrayfirst = ImageFirst.getRGB (Rand, 0, (Breite - Grenze), Höhe, ImageRrayFirst, 0, (Breite - Grenze); // generieren Sie ein neues Bild BufferedImage ImageSult = new bufferedImage (Breite, Höhe, bufferedimage.type_int_rgb); // Lösen Sie den schwarzen Hintergrund, die Standard -Typ_int_RGB sind alle 0 und sie sind alle schwarze graphics2d g = (Graphics2d) ImagMesult.CreateGraphics (); G.SetColor (color.white); G.FillRect (0, 0, Breite, Höhe); // den gesamten Bildschirm füllen g.disponse (); // Lasse die Grenze imageresult.setrgb (Grenze, 0, (Breite - Grenze * 2), Höhe, ImageRrayFirst, 0, (Breite - Grenze); // Setzen Sie das RGB -Datei -Outfile in der linken Hälfte = neuer Datei (OUT); Imageio.Write (Imageresult, "JPG", Outfile); // ein Bild schreiben} catch (ioException e) {logger.Error ("Vollständiger Bilddruck, Bildskalierung und Rotationsverarbeitung fehlgeschlagen ...", E); isok = false; } return isok; }/ ** * Implementieren Sie den gleichen Radio-Zoom des Bildes * * @param Source * Image Stream, der verarbeitet werden soll Zominimage (Quelle, TargetW, Tareth); // Bildbreite und Höhe sind zu klein, erzwingen Sie das Bild vergrößern/* if (width <targetW && Höhe <tareth) {return zoominimage (Quelle, targetW, tareth); } else if ((width <targetW && width == Höhe) || (Höhe <tareth && width == Höhe)) {return zoominimage (Quelle, targetW, tareth); } return null; */}/ ** * Ernte das Bild zu skalieren * * @param Quelle * anstehender Bildstream * @param startx * start x coordinate * @param starty * start y coordinate * @param endx * end x coordinate * @param endy * end y coordinate * @return */ public pufferedimage crop (bufpuledimage cropedimage 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 = Höhe - 1; } Bufferedimage result = new bufferedImage (endx, endy, source.gettype ()); für (int y = starty; y <endy+starty; y ++) {für (int x = startx; x <endx+startx; x ++) {int rgb = source.getRGB (x, y); result.setRGB (x - startx, y - starty, rgb); }} Rückgabeergebnis; } / ** * Drehen Sie das Bild in den angegebenen Winkel * * @param bufferedImage * Zielbild * @param Grad * Drehen Sie den Winkel * @return * / public static bufferedImage rotateImage (endgültig bufferedimage bufferedImage, endgültiger int grad) {int w = bufferedimage.getwidth (); int H = bufferedImage.getheight (); int type = bufferedImage.getColormodel (). GetTransparenz (); Bufferedimage img; Graphics2d Graphics2d; (Graphics2d = (img = new bufferedImage (h, w, type)). creategraphics ()). setRenderingHint (renderingHints.key_interpolation, renderingHints.value_interpolation_bilinear); Graphics2d.Rotate (Math.Toradians (Grad), w / 2, H / 2 + (W> H? (W - H) / 2: (h - w) / 2)); Graphics2d.Drawimage (BufferedImage, 0, 0, NULL); Graphics2D.DiSonS (); zurück img; } / ** * Bild dreht links 90 Grad * * @param bufferedImage * @return * / public static bufferedImage rotateImageleft90 (bufferedimage bufferedImage) {int w = bufferedimage.getWidth (); int H = bufferedImage.getheight (); int type = bufferedImage.getColormodel (). GetTransparenz (); 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.DiSonS (); zurück img; } / ** * Image dreht nach rechts 90 Grad * * @param bufferedImage * @return * / public static bufferedImage rotateImageright90 (bufferedImage bufferedImage) {int w = bufferedimage.getWidth (); int H = bufferedImage.getheight (); int type = bufferedImage.getColormodel (). GetTransparenz (); 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.DiSonS (); zurück img; } // Die öffentliche Datei rotateImageoppo (Dateidatei) leitet die Ausnahme {bufferedImage bufferedimage = imageio.read (Datei); int w = bufferedImage.getWidth (); int H = bufferedImage.getheight (); int type = bufferedImage.getColormodel (). GetTransparenz (); Bufferedimage img; Graphics2d Graphics2d; (Graphics2D = (IMG = New BufferedImage (W, H, Typ)). createGraphics ()). Graphics2d.Rotate (Math.Toradians (180), w / 2, h / 2); Graphics2d.Drawimage (BufferedImage, 0, 0, NULL); Graphics2D.DiSonS (); Imageio.write (IMG, "JPG", Datei); Rückgabedatei; } / *** * Bildspiegelungsprozess * * @param -Datei * @param fx * 0 ist auf und ab Inversion 1 ist links und rechts Inversion * @return * / public void Imagemisro (Dateidatei, int fx) {try {bufferedimage bufferedimage = imageio.read (Datei); int w = bufferedImage.getWidth (); int H = bufferedImage.getheight (); int [] [] datas = new int [w] [h]; für (int i = 0; i <h; i ++) {für (int j = 0; j <w; j ++) {datas [j] [i] = bufferedImage.getRGB (j, i); }} int [] [] tmps = new int [w] [h]; if (fx == 0) {für (int i = 0, a = h-1; i <h; i ++, a--) {für (int j = 0; j <w; j ++) {tmps [j] [a] = Daten [j] [i]; }}} else if (fx == 1) {für (int i = 0; i <h; i ++) {für (int j = 0, b = w-1; j <w; j ++, b-) {tmps [b] [i] = datas [j] [i]; }}} für (int i = 0; i <h; i ++) {für (int j = 0; j <w; j ++) {bufferedimage.setrgb (j, i, tmps [j] [i]); }} Imageio.write (bufferedImage, "jpg", Datei); } catch (Ausnahme e) {e.printstacktrace (); }} / ** * Erzwingen Sie Zoom in oder aus dem Bild * * @param originalImage * originalImage * @return * / public static bufferedImage zoomInimage (bufferedimage) originalImage, int width, int height) {bufferedimage newImage = new bufferedimage (widthth, originalImage.gtype.ttype.);); Grafik g = newImage.getGraphics (); G.Drawimage (OriginalImage, 0, 0, Breite, Höhe, Null); g.disponse (); NeueImage zurückgeben; } / *** Einfaches Bilderkennungsprinzip** @param img* Bildpfad* / public static void Discernimg (String img) {try {FileOne = new Datei (IMG); Bufferedimage bi = imageio.read (fileOne); // Die Breite und Höhe des Bildes int width = bi.getwidth () erhalten; int Höhe = bi.getheight (); // scannen Sie das Bild für (int i = 0; i <Höhe; i ++) {für (int j = 0; j <width; j ++) {// row scan int dip = bi.getrgb (j, i); if (dip == -1) system.out.print (""); sonst system.out.print ("♦"); } System.out.println (); // line break}} catch (exception e) {logger.Error ("Bilderkennungsfehler", e); }}}Für mehr Java-bezogene Inhalte können Leser, die an dieser Website interessiert sind, die Themen "Zusammenfassung von Java Image Operation Skills", "Zusammenfassung von Java-Datum und Zeit-Betriebsfähigkeiten", "Zusammenfassung der Java-Operation DOM-Knotenkenntnisse", "Zusammenfassung der Java-Datei und des Verzeichnisbetriebs" und "Tutorials of Java Data Struktur und Algorithm", Zusammenfassung von Java-Datum und Zeit ".
Ich hoffe, dieser Artikel wird für Java -Programme aller hilfreich sein.