Dieser Artikel teilt den entsprechenden Code für Java zur Implementierung der Bildkomprimierung für Ihre Referenz. Der spezifische Inhalt ist wie folgt
Import Java.awt.image; Bild java.awt.image.buffeedImage; Import Java.io.BytearrayoutputStream; Import Java.io.ioException; Import Java.io.inputStream; import Javax.imageo.imageo; public class ImageProcess { / *** Bild* / privates Bild img; / *** Breite*/ private int width; / *** Höhe*/ private int Höhe; / *** Dateiformat*/ private String imageFormat; /*** Konstruktor* @throws Exception*/public imageProcess (inputStream in, String -Dateiname) löst eine Ausnahme aus {// Konstruieren Sie das Bildobjekt img = imageio.read (in); // Die Quellbildbreite breite = img.getwidth (null); // Die Quellbildlänge Höhe abrufen = img.Getheight (null); // Dateiformat imageFormat = Dateiname.substring (Dateiname.lastIndexof (".")+1); } / *** Kompress nach Breite oder Höhe* @param w int maximal width* @param h int maximale Höhe* / public byte [] resizeFix (int w, int h) löst IoException aus {if (width / height> w / h) {retrizensize -bywidth (w); } else {return resizeByHeight (h); }} / *** das Bild proportional mit width* @param w int neue width* / public byte [] resizeBywidth (int w) löscht ioException {int h = (int) (Höhe* w / w / width) aus; Return -Größen (w, h); } / *** das Bild proportional mit der Höhe skalieren. Return -Größen (w, h); }/*** Erzwingen Sie das Bild in einer festen Größe. Die Qualität ist besser, aber langsamer als die Geschwindigkeit. Bufferedimage image = new bufferedImage (w, h, bufferedimage.type_int_rgb); Image.getGraphics (). DrawImage (IMG, 0, 0, W, H, Null); // Zeichnen Sie die reduzierte Figur BytearrayoutputStream baos = new bytearrayoutputStream (); Imageio.Write (Bild, ImageFormat, Baos); return baos.tobytearray (); }}Das obige dreht sich alles um diesen Artikel. Ich hoffe, dass es für das Lernen aller hilfreich sein wird und die Bildkomprimierungsvorgänge einfach implementieren wird.