Dieser Artikel ist eine Java -Bildverarbeitungsklasse, die vom Autor in Kombination mit einigen Informationen im Internet eingekapselt ist und die Skalierung, Rotation und Mosaisierung von Bildern unterstützt.
Ohne weiteres, der Code:
Paketvertrag; Import Java.awt.Color; Import Java.awt.Graphics; Import Java.awt.graphics2d; Import Java.awt.image; Import Java.awt.Geom.Affinetransform; Import Java.image.afransform; javax.imageo.imageo;/*** Bildverarbeitungsklasse. * * @author nagsh * */public class Imagedal {String OpenUrl; // das Originalbild Öffnen Sie Pfad String SaveURL; // das neue Bild speichern Pfad String Savename; // das neue Bildname -String -Suffix; // Der neue Bildtyp unterstützt nur GIF, JPG, PNG Public Imagedal (String OpenUrl, String SaveUrl, String Savename, String -Suffix) {this.openurl = openUrl; this.savename = Savename; this.saveurl = SaveURL; this.suffix = suffix; } /*** Bildskalierung. * * @param width * Erforderliche Breite * @param Höhe * Erforderliche Höhe * @Throws Exception */ public void Zoom (int Breite, inthohe) löst Ausnahme aus {double sx = 0,0; Doppel Sy = 0,0; Datei Datei = neue Datei (OpenURL); if (! file.isfile ()) {Neue Ausnahme werfen ("Imagedal >>>" + Datei + "keine Bilddatei!"); } BufferedImage bi = imageio.read (Datei); // Lesen Sie das Bild // Berechnen Sie die x-achse y-axis-Skalierungsverhältnis-Wenn die gleich skalaale Skalierung erforderlich ist, ist die Veränderung der Parameter und der Höhe gleich) Änderungen vor dem Aufruf von = (doppelte) Breite/ bi.getwidth (); Sy = (doppelte) Höhe / bi.Getheight (); Affinetransformop op = new Affinetransformop (affinetransform.getScaleinstance (SX, SY), NULL); Datei sf = neue Datei (SaveURL, Savename + "." + Suffix); Bild Zoomimage = op.filter (bi, null); try {imageio.write ((bufferedimage) zoomimage, suffix, sf); // das Bild speichern} catch (Ausnahme e) {e.printstacktrace (); }} / ** * rotieren * * @param Grad * Drehwinkel * @Throws Exception * / public void spin (int Grad) löst eine Ausnahme aus {int swidth = 0; // Breite nach der Rotation int SHEIGHT = 0; // Höhe nach Rotation int x; // Herkunft horizontale Koordinate int y; // Ursprung vertikaler Koordinatendatei = neue Datei (OpenURL); if (! file.isfile ()) {Neue Ausnahme werfen ("Imagedal >>>" + Datei + "keine Bilddatei!"); } BufferedImage bi = imageio.read (Datei); // Lesen Sie das Bild // Prozesswinkel-Determin-Rotationsgrad = Grad % 360; if (Grad <0) Grad = 360 + Grad; // den Winkel auf zwischen 0 und 360 Grad doppelte Theta = math. SHEIGHT = Bi.Getheight (); } else if (Grad == 90 || Grad == 270) {shight = bi.getWidth (); swidth = bi.getheight (); } else {swidth = (int) (math.sqrt (bi.getwidth () * bi.getwidth () + bi.getheight () * bi.getheight ()); SHEIGHT = (int) (math.sqrt (bi.getWidth () * bi.getwidth () + bi.getheight ())); } x = (swidth / 2) - (bi.getWidth () / 2); // Bestimmen Sie die Ursprungskoordinate y = (Shight / 2) - (bi.getheight () / 2); BufferedImage Spinimage = new bufferedImage (swidth, sheight, bi.gettype ()); // Setzen Sie die Hintergrundfarbe der Bildgrafik2d gs = (Graphics2d) spinimage.getGraphics (); gs.setColor (color.white); Gs.FillRect (0, 0, Swidth, Shight); // Zeichnen Sie den Hintergrund des gedrehten Bildes in einer bestimmten Farbaffinetransform bei = new Affinetransform (); at.Rotate (theta, swidth / 2, shight / 2); // Bild drehen at.translate (x, y); Affinetransformop op = neuer affinetransformop (at, affinetransformop.type_bicubic); Spinimage = op.filter (bi, Spinimage); Datei sf = neue Datei (SaveURL, Savename + "." + Suffix); Imageio.Write (Spinimage, Suffix, SF); // das Bild speichern} /*** Mosaisierung. * @Param Größe Mosaikgröße, dh die Länge und Breite jedes Rechtecks * @return * @throws Exception */ public boolean mosaic (intgröße) löst eine Ausnahme aus {Datei file = new Datei (openUrl); if (! file.isfile ()) {Neue Ausnahme werfen ("Imagedal >>>" + Datei + "keine Bilddatei!"); } BufferedImage bi = imageio.read (Datei); // Lesen Sie das Bild BufferedImage Spinimage = new bufferedImage (bi.getwidth (), bi.getheight (), bi.type_int_rgb); if (bi.getWidth () <Größe || bi.getheight () <Größe || Größe <= 0) {// Die Größe des Mosaik -Gitters ist zu groß oder zu klein retektiert falsch; } int xcount = 0; // Die Anzahl der Richtung wird gezogen, int yCount = 0; // Die Anzahl der Richtung wird in yCount if (bi.getWidth () % size == 0) {xcount = bi.getWidth () / size; } else {xcount = bi.getWidth () / size + 1; } if (bi.getheight () % size == 0) {yCount = bi.getheight () / size; } else {yCount = bi.getheight () / size + 1; } int x = 0; // koordiniert int y = 0; // Mosaiken zeichnen (Rechtecke zeichnen und Farben füllen) Grafik gs = spinimage.getGraphics (); für (int i = 0; i <xcount; i ++) {für (int j = 0; j <ycount; j ++) {// mosaische Rechteckgröße int mwidth = Größe; int mHeight = Größe; if (i == xcount-1) {// der letzte in horizontaler Richtung ist besonders und reicht möglicherweise nicht aus für eine Größe mwidth = bi.getwidth ()-x; } if (j == yCount-1) {// ähnlich mHeight = bi.getheight ()-y; } // Der RGB -Wert der Rechteckfarbe nimmt das mittlere Pixel int Centerx = x; int centery = y; if (mwidth % 2 == 0) {CenterX += MWIDTH / 2; } else {Centrex += (mwidth - 1) / 2; } if (mHeight % 2 == 0) {centery += mHeight / 2; } else {centery += (mHeight - 1) / 2; } Color color = new color (bi.getRGB (CenterX, Centery)); gs.setColor (Farbe); Gs.FillRect (X, Y, Mwidth, MHEIGHT); y = y + Größe; // Berechnen Sie die y -Koordinaten des nächsten Rechtecks} y = 0; // Die y -Koordinaten x = x + Größe wiederherstellen; // Berechnen Sie die X -Koordinaten} gs.Dispose (); Datei sf = neue Datei (SaveURL, Savename + "." + Suffix); Imageio.Write (Spinimage, Suffix, SF); // das Bild speichern. Return True; } public static void main (String [] args) löst eine Ausnahme aus {Imagedal imagedal = new Imagedal ("e: //1.jpg", "e: //", "2", "jpg"); // skalieren testen/* imagedal.zoom (200, 300); */ // Rotation testen/* Imagedal.spin (90); */ // mosaic/*imagedal.mosaic (4);*/}} testen Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.