Cet article décrit la classe d'outils de traitement d'image WeChat implémentée par Java. Partagez-le pour votre référence, comme suit:
Maintenant, il y a moins de photos et d'articles à l'extérieur. J'ai lu le code de copie mais je ne peux pas l'utiliser. J'utilise le package JAR correspondant pour le traiter, et beaucoup sont mis à l'échelle en proportion, qui ne peuvent pas répondre aux attentes que je veux: cette classe d'outils est une classe basée sur Java basée sur RVB écrite dans des imprimantes WeChat auparavant.
package com.zjpz.util; import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.rederinghints; import java.awt.geom.affinetransform; import java.awt.image.bufferedImage; import java.awt.image.coldEl; java.awt.image.writableteraster; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import org.slf4j.logger; import org.slf4j.loggerfactory; / ** * wechat wech image toul Logger Logger = LoggerFactory.getLogger (Picturetool.class); public static void main (String [] args) lève ioException {file fileOne = new File ("c: //1.jpg"); BufferedImage ImageFirst = imageo.read (fileOne); int border = 0; ImageFirst = CRAP (ImageFirst, 0,10,297,300); File outfile = nouveau fichier ("d: //2.jpg"); Imageo.write (imageFirst, "jpg", outfile); // write image} / ** * x coordonnée pixel de l'image combinée verticale * / private final statique int y_width = 645; / ** * Le pixel de coordonnées y de l'image standard, 920, est une photo générale, 1099 est une photo de tampon * / private final statique int y_height = 920; / ** * Crop x Coordonnées Pixels en retrait * / Private Final Static int x_retract = 50; / ** * Crop Y Coordonnées Pixels en retrait * / Private Final Static int y_retract = 50; / ** * La bordure d'image par défaut du système est de 20 * / / public final static int border = 20; / ** * Image synthétique horizontale * / public static void xpic (String First, String Second, String Out) {try {/ * 1 Lire la première image * / file fileOne = new File (First); BufferedImage ImageFirst = imageo.read (fileOne); int width = imageFirst.getWidth (); // Image Width int height = ImageFirst.GetHeight (); // Lire RGB Int [] ImageArrayFirst = ImageFirst.getrgb (0, 0, largeur, hauteur, hauteur, ImageArrayFirst, 0, width); / * 1 Faites de même pour la deuxième image * / fichier filetwo = new File (deuxième); BufferedImage ImagesConDond = imageo.read (filetwo); int widthtwo = imagesConD.getWidth (); // Width Image int heightTwo = imagesConD.GetHeight (); // Image Height int [] imageArAiSECOND = new int [widthtwo * highttwo]; ImagearRaySecond = imagesConD.getrgb (0, 0, widthtwo, heightTwo, ImageArraysecond, 0, widthtwo); int h = hauteur; if (height <heighttwo) {h = heighttwo; } // générer une nouvelle image BufferedImage ImageReResult = new BufferedImage (Width + WidthTwo, H, BufferedImage.Type_int_rgb); ImageResult.setrgb (0, 0, largeur, hauteur, imagearrayfirst, 0, largeur); // définir RGB ImageResult.Setrgb (Width, 0, widthTwo, heighttwo, imagearraysecond, 0, widthtwo); // set rgb File Outfile in the droite = nouveau fichier (out); Imageo.write (ImageResult, "jpg", outfile); // écrire une image} catch (exception e) {logger.error ("L'image de synthèse horizontale était une erreur ...", e); }} / ** * Image de synthèse verticale * * @param d'abord * Mettez le chemin d'image ci-dessus * @param deuxième * Mettez le chemin d'image ci-dessous * @param out * Répertoire de sortie de fichier * @param bordure * Image Reserved Border * / public static boolean ypic (String First, String second, String Out, int border) {boolean isok = true; essayez {/ * 1 lire la première image * / file fileOne = new File (premier); BufferedImage ImageFirst = imageo.read (fileOne); int width = imageFirst.getWidth (); // Width Image int height = ImageFirst.GetHeight (); // Image Height / * 2 faire de même pour la deuxième image * / fichier filetwo = new File (second); BufferedImage ImagesConDond = imageo.read (filetwo); int widthtwo = imagesConD.getWidth (); // width d'image int heightTwo = imagesConD.GetHeight (); // Image Height / * 1 Lire la première image Begin * / int t_height = y_height - hightwo; // L'image est une image horizontale, tourne à 90 degrés dans le sens antihoraire et à l'échelle également si (largeur> hauteur) {imageFirst = rotateImageleft90 (imageFirst); } // Equal Scaling imageFirst = redimensit (imageFirst, y_width, t_height); // Taille de l'image après la largeur de mise à l'échelle = ImageFirst.getWidth (); // Largeur d'image Hauteur = ImageFirst.GetHeight (); // Hauteur de l'image // Après une mise à l'échelle du radical égal, l'image est encore trop grande. Recadrer l'image booléen a_w, a_h = false; if ((a_w = (largeur> y_width)) || (a_h = (hauteur> t_height))) {// position de démarrage x, y coordonnée int s_w = 0, s_h = 0; // Lorsque les coordonnées de Cramping x, l'attribut de retrait 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; } // Lorsque les coordonnées de recadrage, attribut 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 = CRAP (ImageFirst, S_W, S_H, Y_Width, T_Height); width = imageFirst.getWidth (); height = imageFirst.getheight (); } int [] ImageArrayFirst = new int [(largeur - bordure) * hauteur]; // Read RGB ImageArrayFirst = ImageFirst.getrgb (Border, 0, (largeur - bordure), hauteur, imagearrayfirst, 0, (largeur - bordure)); / * 2do Le même processus pour la deuxième image commence * / int [] ImageArraySecond = new int [widthtwo * heighttwo]; ImagearRaySecond = imagesConD.getrgb (0, 0, widthtwo, heightTwo, ImageArraysecond, 0, widthtwo); int w = largeur; if (width <widthtwo) {w = widthtwo; } // Hauteur d'image int h = hauteur + heightTwo; // Générez une nouvelle image BufferedImage ImageResult = new BufferedImage (W, H, BufferedImage.Type_int_rgb); // Résoudre l'arrière-plan noir, le type par défaut_int_rgb est tous 0, et ils sont tous des graphiques noirs 2d g = (graphics2d) ImageReResult.Creategraphics (); g.setColor (Color.white); G.Fillrect (0, 0, W, H); // remplit tout l'écran g.dispose (); // Laissez la bordure ImageReResult.Setrgb (bordure, 0, (largeur - bordure * 2), hauteur, imagearrayfirst, 0, (largeur - bordure)); // réglé rgb dans la demi-image de gauche, ImageResult.Setrgb (0, hauteur Fichier (out); Imageo.write (ImageResult, "jpg", outfile); // écrivez une image} catch (exception e) {logger.error ("L'image de synthèse perpendiculaire a échoué ...", e); ISOK = false; } return isok; } / ** * Impression d'image complète, échelle d'image et traitement de rotation * * @param source * Traitement en attente d'image * @param out * Répertoire de sortie de fichier après traitement * @param bordure * Border réservé d'image * / booléen statique publique Maigaopic (chaîne source, chaîne out, border) {booléen isok = true; essayez {/ * 1 lire la première image * / file fileOne = new File (source); BufferedImage ImageFirst = imageo.read (fileOne); int width = imageFirst.getWidth (); // Image Width int height = ImageFirst.Getheight (); // Image Height // L'image est une image horizontale, tourne à 90 degrés dans le sens antihoraire et à l'échelle de l'échelle (ImageFirst); } // Equality Scaling imageFirst = redimensit (imageFirst, y_width, y_height); // Taille de l'image après la largeur de mise à l'échelle = ImageFirst.getWidth (); // Image Width Height = ImageFirst.GetHeight (); // Image Height // Après l'échelle de l'égalité, l'image est encore trop grande. Recadrer l'image booléen a_w, a_h = false; if ((a_w = (largeur> y_width)) || (a_h = (hauteur> y_height))) {// position de démarrage x, y coordonnée int s_w = 0, s_h = 0; // Lorsque les coordonnées de Cramping x, l'attribut de retrait 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; } // Lorsque les coordonnées de recadrage, attribut 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 = CRAP (ImageFirst, S_W, S_H, Y_Width, Y_Height); width = imageFirst.getWidth (); height = imageFirst.getheight (); } int [] ImageArrayFirst = new int [(largeur - bordure) * hauteur]; // lire RGB de l'image ImageArrayFirst = ImageFirst.getrgb (bordure, 0, (largeur - bordure), hauteur, imagearrayfirst, 0, (largeur - bordure)); // Générez une nouvelle image BufferedImage ImageResult = new BufferedImage (largeur, hauteur, bufferedImage.type_int_rgb); // Résoudre l'arrière-plan noir, le type par défaut_int_rgb est tous 0, et ils sont tous des graphiques noirs 2d g = (graphics2d) ImageReResult.Creategraphics (); g.setColor (Color.white); G.Fillrect (0, 0, largeur, hauteur); // remplit tout l'écran g.dispose (); // Laissez la bordure ImageReResult.Setrgb (bordure, 0, (largeur - bordure * 2), hauteur, imagearrayfirst, 0, (largeur - bordure)); // Définissez le fichier RVB Outfile dans la moitié gauche = nouveau fichier (out); ImageIo.Write (ImageResult, "JPG", outfile); // Écrivez une image} Catch (ioException e) {Logger.Error ("L'impression d'image complète, l'échelle d'image et le traitement de rotation ont échoué ...", e); ISOK = false; } return isok; } / ** * Implémentez le zoom égal à radio de l'image * * @param source * Stream d'image à traiter * @param Targetw * width * @param Targeth * height * @return * / public static butteredImage remedize (BufferedImage Source, INTR Targetw, int targeth) {int width = source.getWidth (); // Image width et height. ZoomImage (Source, TargetW, Targeth); // La largeur et la hauteur de l'image sont trop petites, forcent l'image de l'image / * if (width <TargetW && height <Targeth) {return zoomimage (source, targetw, targeth); } else if ((width <TargetW && width == height) || (height <Targeth && width == height)) {return zoomimage (source, targetw, targeth); } return null; * /} / ** * Cramez l'image à l'échelle * * @param source * Stream d'image en attente * @param startx * Démarrer x coordonnée * @param starty * Démarrer la coordonnée * @return * / public static buffedimage Crop. source.getWidth (); int hauteur = source.getheight (); if (startx <= -1) {startx = 0; } if (starty <= -1) {starty = 0; } if (endx <= -1) {endx = largeur - 1; } if (endy <= -1) {endy = height - 1; } BufferedImage Result = new BufferedImage (EndX, Endy, Source.getType ()); pour (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); }} Retour Résultat; } / ** * Tournez l'image à l'angle spécifié * * @param BufferedImage * Image cible * @param degré * Rotation de l'angle * @return * / public static BufferedImage RotateImage (final BufferedImage BufferedImage, final int) {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 (RenderingHint.key_interpolation, rederingHint.value_interpolation_bilinear); graphics2d.rotate (math.toradians (degré), w / 2, h / 2 + (w> h? (w - h) / 2: (h - w) / 2)); Graphics2d.DrawImage (BufferedImage, 0, 0, null); graphics2d.dispose (); retour IMG; } / ** * L'image tourne à gauche à 90 degrés * * @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 (RenderingHint.key_interpolation, rederingHint.value_interpolation_bilinear); graphics2d.rotate (math.toradians (270), w / 2, h / 2 + (w - h) / 2); Graphics2d.DrawImage (BufferedImage, 0, 0, null); graphics2d.dispose (); retour IMG; } / ** * L'image tourne à droite de 90 degrés * * @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 (RenderingHint.key_interpolation, rederingHint.value_interpolation_bilinear); graphics2d.rotate (math.toradians (90), w / 2 - (w - h) / 2, h / 2); Graphics2d.DrawImage (BufferedImage, 0, 0, null); graphics2d.dispose (); retour IMG; } // Faire un fichier public rotateImageOPPO (fichier de fichier) lève une exception {bufferedImage BufferedImage = ImageIo.Read (fichier); 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 (RenderingHint.key_interpolation, rederingHint.value_interpolation_bilinear); Graphics2d.rotate (math.toradians (180), w / 2, h / 2); Graphics2d.DrawImage (BufferedImage, 0, 0, null); graphics2d.dispose (); Imageo.write (img, "jpg", fichier); return fichier; } / *** * Traitement de miroir d'image * * @param fichier * @param fx * 0 est de haut en bas, l'inversion 1 est la gauche et l'inversion droite * @return * / public void ImageMisro (fichier de fichier, int fx) {try {bufferedImage BufferedImage = imageo.read (fichier); 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 ++) {tmpps [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]; }}} pour (int i = 0; i <h; i ++) {for (int j = 0; j <w; j ++) {bufferedImage.setrgb (j, i, tmps [j] [i]); }} Imageo.write (bufferedImage, "jpg", fichier); } catch (exception e) {e.printStackTrace (); }} / ** * Forcer le zoom dans ou hors de l'image * * @param originalMage * OriginalMage * @return * / public static butteredImage ZoomImage (BufferedImage) OriginalMage, int larget, int height) {BufferedImage NewImage = New BufferedImage (Width, Height, OriginalMage.getType ()); Graphiques g = newImage.getGraphics (); G.DrawImage (originalMage, 0, 0, largeur, hauteur, null); g.dispose (); retourner NewImage; } / ** * principe de reconnaissance d'image simple * * @param img * chemin d'image * / public static void discernimg (string img) {try {file fileOne = new File (img); BufferedImage BI = ImageIo.Read (FileOne); // Obtenez la largeur et la hauteur de l'image int largeur = bi.getWidth (); int hauteur = bi.getheight (); // scanne l'image pour (int i = 0; i <hauteur; i ++) {pour (int j = 0; j <width; j ++) {// Row scan int dir = bi.getrgb (j, i); if (dip == -1) System.out.print (""); else System.out.print ("♦"); } System.out.println (); // line Break}} catch (exception e) {Logger.Error ("Erreur de reconnaissance d'image", e); }}}Pour plus de contenu lié à Java, les lecteurs qui sont intéressés par ce site peuvent afficher les sujets: "Résumé des compétences de l'opération d'image Java", "Résumé des compétences de date et d'opération de temps Java", "Résumé des compétences de nœuds de Dom Java", "Résumé du fichier Java et des compétences de fonctionnement des répertoires" et "Tutorials de la structure de données et de l'algorithme Java".
J'espère que cet article sera utile à la programmation Java de tous.