最近、プロジェクトを行うには画像圧縮処理が必要です。オンラインで検索したメソッドのほとんどは、パッケージcom.sun.image.codec.jpeg。*のjpegimageencoderクラスを使用して使用します。このパッケージを導入した後、エラーを報告しました。さまざまなGoogle Baiduは、JREの手動でRt.Jarを含むさまざまな方法を試し、アクセス制限APIプロンプトをEclipseで警告するなどを試しました。しかし、これらは良くありません。後でcom.sun.image。このクラスはJava7で完全に削除されているようですが、少なくともバージョンはありません。次に、ImageIOクラスを処理するために使用する代替ソリューションを検索しました。コードは次のとおりです。
圧縮後の任意のサイズ、高解像度、変形なし(空白)に圧縮することができ、接尾辞名を変更し、圧縮解像度を変更できます。
たぶん、何人かの友人もこれを必要としています。それを参照してください。ご質問がある場合は、証拠を教えてください!
パッケージcn.com.images; java.awt.graphicsをインポートします。 java.awt.imageをインポートします。 java.awt.image.bufferedimageをインポートします。 java.io.fileをインポートします。 java.io.ioexceptionをインポートします。 java.math.bigdecimalをインポートします。 java.math.mathcontextをインポートします。 java.util.arraylistをインポートします。 javax.imageio.imageioをインポートします。 /*** *画像上の操作 * * @author chenzheng_java * @since 2011/7/29 * */public class imagehelper {private static imagehelper imagehelper = null; public static imagehelper getImageHelper(){if(imagehelper == null){imageHelper = new ImageHelper(); } imagehelperを返します。 } / *** *指定されたスケールで画像をスケーリングします * * @param sourceimagepath * sourceアドレス * @param destination * sizeのsizeのアドレス * @paramスケール * 1.2 * / public static void sacleimage(string destination、double sacer、string format){file file = new file(new file file = new file file = new file file(source); BufferedImage bufferedimage; try {bufferedimage = imageio.read(file); int width = bufferedimage.getWidth(); int height = bufferedimage.getheigh(); width = parsedoubletoint(width *スケール); height = parsedoubletoint(height *スケール);画像画像= bufferedimage.getScaledInstance(幅、height、image.scale_smooth); bufferedimage outputimage = new bufferedimage(幅、高さ、bufferedimage.type_int_rgb); Graphics Graphics = outputImage.getGraphics(); Graphics.drawimage(画像、0、0、null); graphics.dispose(); Imageio.write(outputimage、format、new file(destinationPath)); } catch(ioException e){system.out.println( "画像の圧縮中にエラーが発生しました"); e.printstacktrace(); }} / ****画像を指定された高さまたは幅にスケーリング* @param sourceimagepath画像ソースアドレス*画像を圧縮した後の画像の宛先のアドレス* @param widthスケール幅* @param width width* @param auto画像形式の元の高さと幅の比率を自動的に維持するかどうか* @param SourceImagePath、String DestinationPath、int width、int height、boolean auto、string format){try {file file = new file(sourceImagePath); BufferedImage bufferedimage = null; bufferedimage = imageio.read(file); if(auto){arraylist <integer> paramsArrayList = getAutowIdthandheight(bufferedimage、width、height); width = paramsarraylist.get(0); height = paramsArrayList.get(1); system.out.println( "auto-Adjustスケール、width ="+width+"画像を圧縮するときにエラーが発生しました"); e.printstacktrace(); }} / ** *ダブルタイプデータをintに変換し、丸め原則 * * @param sourcedouble * @return * / private static int int parsedoubletoint(double sourcedouble){int result = 0; result =(int)sourcedouble;返品結果; } / ***** @param bufferedimage画像オブジェクトはスケーリングされますheight_scale){arrayList <integer> arrayList = new ArrayList <Integer>(); int width = bufferedimage.getWidth(); int height = bufferedimage.getheigh(); double scale_w = getDot2Decimal(width_scale、width); system.out.println( "getAutowIdthandheightscale_w ="+scale_w); double scale_h = getDot2decimal(height_scale、height); if(scale_w <scale_h){arraylist.add(parsedoubletoint(scale_w*width)); arraylist.add(parsedoubletoint(scale_w*height)); } else {arraylist.add(parsedoubletoint(scale_h*width)); arraylist.add(parsedoubletoint(scale_h*height)); } return arrayList; }/*** * 2つの数値の3つの小数点A/b * @param a * @param b * @return */public static double getDot2decimal(int a、int b){bigdecimal bigdecimal_1 = new bigdecimal(a); Bigdecimal bigdecimal_2 = new bigdecimal(b); bigdecimal bigdecimal_result = bigdecimal_1.divide(bigdecimal_2、new MathContext(4)); double double1 = new double(bigdecimal_result.tostring()); system.out.println( "double after division is:"+double1); Double1を返します。 }}