この記事では、Javaの関連コードを共有して、参照用の画像圧縮を実装しています。特定のコンテンツは次のとおりです
java.awt.image; image java.awt.image.bufferedimage; Import java.io.bytearrayoutputStream; Import java.io.ioException; Import java.io.inputStream; javax.imageio.imageioをインポートします。 public class imageProcess { / *** image* / private image img; / *** width*/ private int width; / *** height*/ private int height; / ***ファイルフォーマット*/ private string imageformat; /** constructor* @throws例外*/public imageProcess(inputstream in、string filename)shrows exception {//画像オブジェクトimg = imageio.read(in); //ソース画像幅width = img.getWidth(null)を取得します。 //ソース画像の長さの高さを取得= img.getheigh(null); //ファイル形式ImageFormat = filename.substring(filename.lastindexof( "。")+1); } / ***幅または高さに応じて圧縮* @param w int最大幅* @param h int最大高さ* / public byte [] seizefix(int w、int h)throws ioexception {if(width / height> w / h){return resizebywidth(w); } else {return resizebyheight(h); }} / *** widthに比例して画像をスケーリングする* @param w int new width* / public byte [] seizeByWidth(int w)throws ioException {int h =(int)(height* w / width); returnsize(w、h); } / ***高さに比例して画像をスケーリングする* @param h int new height* / public byte [] seizebyheight(int h)throws ioexception {int w =(int)(width* h / height); returnsize(w、h); }/***強制/*画像を固定サイズに強制/拡大* @param w int new width* @param h int new height*/public byte [] sezize(int w、int h)throws ioexception {// scale_smoothのサムネイルアルゴリズムはサムネイル画像の平滑化を優先します。品質は優れていますが、速度よりも遅くなります。 bufferedimage image = new BufferedImage(W、H、BufferedImage.type_int_rgb); image.getGraphics()。drawimage(img、0、0、w、h、null); //削減された図を描画bytearrayoutputStream baos = new bytearrayoutputStream(); imageio.write(image、mageformat、baos); return baos.tobytearray(); }}上記はこの記事に関するすべてです。すべての人の学習に役立ち、画像圧縮操作を簡単に実装することを願っています。