この記事では、Javaが実装したWeChat画像処理ツールクラスについて説明します。次のように、参照のために共有してください。
今、外に写真や記事が少なくなっています。コピーコードを読みましたが、使用できません。対応するJARパッケージを使用して処理しますが、多くは比例してスケーリングされています。これは、私が望む期待を満たすことができません。このツールクラスは、以前にWeChatプリンターで書かれたRGBに基づくJavaに基づくクラスです。
パッケージcom.zjpz.util; import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; Import java.awt.renderingss; Import java.awt.geom.affinetransform; Import java.awt.awt.image.image.bufferedimage.aw. java.awt.image.writableraster; Import java.io.file; Import java.io.ioexception; Import javax.imageio.imageio; Import org.slf4j.logger; Import org.slf4j.loggeractory; logger logger = loggerfactory.getLogger(picturetool.class); public static void main(string [] args)throws ioexception {file fileone = new file( "c://1.jpg"); bufferedimage imagefirst = imageio.read(fileone); int border = 0; ImageFirst = Crop(ImageFirst、0,10,297,300); file outfile = new file( "d://2.jpg"); Imageio.Write(ImageFirst、 "jpg"、outfile); // write picture}/ *** x垂直像を組み合わせた画像の座標ピクセル*/ private final static int y_width = 645; / ***標準画像のy座標ピクセル、920は一般的な写真であり、1099はスタンプ写真です*/ private final static int y_height = 920; / ***トリッキングX調整インデントピクセル*/プライベート最終的な静的int x_retract = 50; / ***クロップY調整インデントピクセル*/プライベート最終的な静的int y_retract = 50; / ** *システムデフォルトの画像境界は20 */ public final static int border = 20です。 / ***水平合成画像*/ public static void xpic(string first、string second、string out){/* 1最初の画像を読み取り*/ file fileone = new file(first); bufferedimage imagefirst = imageio.read(fileone); int width = imagefirst.getWidth(); // image width int height = imagefirst.getheight(); // image height int [] imagearrayfirst = new int [width * height]; // imagearrayfirst = imagefirst.getrgb(0、0、width、emagearfirst、0、fid、fid、fid、ead、emagefirst.getrgb(0、height); /* 1 2番目の画像で同じことを行います*/ file filetwo = new file(2番目); BufferedImage Imagesecond = imageio.read(filetwo); int widthtwo = imagesecond.getWidth(); // image width int heighttwo = imagesecond.getheight(); // image height int [] imagearraysecond = new int [widthtwo * heighttwo]; imagearraysecond = imageecond.getRgb(0、0、widthtwo、heighttwo、imagearraysecond、0、widthtwo); int h = height; if(height <heighttwo){h = heighttwo; } //新しい画像を生成するbufferedimage imageResult = new BufferedImage(width + widthtwo、h、bufferedimage.type_int_rgb); ImageResult.setRgb(0、0、width、height、imagearrayfirst、0、width); // set rgb imageresult.setrgb(width、0、widthtwo、heightwo、imagearraysecond、0、widthtwo); Imageio.Write(ImageResult、 "jpg"、outfile); // image} catch(例外e){logger.error( "水平合成画像はエラー..."、e); }} / ***垂直合成画像** @param first*画像パスの上に画像パスを置く* @param secondの上に画像パスを置く* @param out* @param border* image reserved border* / public static boolean ypic(string first、string second、string out、int border){boolean isok = true; { /* 1最初の画像を読み取ります* / file fileone = new file(first); bufferedimage imagefirst = imageio.read(fileone); int width = imagefirst.getWidth(); // image width int height = imagefirst.getheight(); // image height/* 2 2番目の画像に対して同じことをします*/ file filetwo = newファイル(2番目); BufferedImage Imagesecond = imageio.read(filetwo); int widthtwo = imagesecond.getWidth(); // image width int heighttwo = imagesecond.getheight(); // image height/ * 1最初の画像を読む//画像は水平画像であり、反時計回りに90度回転し、(width> height){imagefirst = rotateimagelefeft90(imagefirst); } // equalスケーリングImageFirst = resize(ImageFirst、y_width、t_height); //スケーリング後の画像のサイズ= ImageFirst.getWidth(); //画像幅height = imageFirst.getheight(); //イメージの高さ//等しいラジカルスケーリングの後、画像はまだ大きすぎます。画像ブールA_W、a_h = falseをトリミングします。 if((a_w =(width> y_width))||(a_h =(height> t_height)))){//位置X、y座標int s_w = 0、s_h = 0; // x座標をトリミングする場合、インデント属性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; } // y座標のトリミングの場合、インデント属性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 = crop(imagefirst、s_w、s_h、y_width、t_height); width = imagefirst.getWidth(); height = imagefirst.getheight(); } int [] ImagearrayFirst = new int [(width -border) * height]; // read rgb imagearrayfirst = imagefirst.getrgb(border、0、(width -border)、height、imagerrayfirst、0、(width -border)); / * 2DO 2番目の画像の同じプロセスが始まります */ int [] imagearraysecond = new int [widthtwo * heighttwo]; imagearraysecond = imageecond.getRgb(0、0、widthtwo、heighttwo、imagearraysecond、0、widthtwo); int w = width; if(width <widthtwo){w = widthtwo; } //画像の高さint h = height + heighttwo; //新しい画像を生成するbufferedimage imageResult = new BuffereDimage(W、H、BufferedImage.type_int_rgb); //黒の背景を解くと、デフォルトのtype_int_rgbはすべて0であり、それらはすべて黒いgraphics2d g =(graphics2d)imageresult.creategraphics()です。 g.setcolor(color.white); G.FillRect(0、0、W、H); //画面全体を入力しますg.dispose(); // border imageresult.setrgb(border、0、(width -border * 2)、height、imagearrayfirst、0、(width -border)); //左半分のimageResult.setrgb(0、height、widtthtwo、heighttwo、imagearraysecond、0、widthtwo); widttwo); widttwo);ファイル(out); Imageio.Write(ImageResult、 "jpg"、outfile); // image} catch(例外e){logger.error( "垂直合成画像が失敗しました..."、e); ISOK = false; } isokを返します。 } / ***フル画像印刷、画像スケーリングと回転処理** @paramソース*画像保留処理*処理後のファイル出力ディレクトリ* @param border* image reserved border* / public static boolean maigaopic(string out、int border){boolean isok = true; { /* 1最初の画像を読み取ります* / file fileone = new file(source); bufferedimage imagefirst = imageio.read(fileone); int width = imagefirst.getWidth(); // image width int height = imagefirst.getheight(); //画像の高さ//画像は水平画像であり、90度を反時計回りに回転し、(wid> height){imagefirst = rotateimageleft90(imagefirst); } // equality scaling imagefirst = resize(imagefirst、y_width、y_height); //スケーリング後の画像のサイズ= imageFirst.getWidth(); //画像幅height = imageFirst.getheigh(); // equality scalingの後、画像がまだ大きすぎます。画像ブールA_W、a_h = falseをトリミングします。 if((a_w =(width> y_width))||(a_h =(height> y_height)))){//位置xを開始します。 // x座標をトリミングする場合、インデント属性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; } // y座標のトリミングの場合、インデント属性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 = crop(imagefirst、s_w、s_h、y_width、y_height); width = imagefirst.getWidth(); height = imagefirst.getheight(); } int [] imagearrayfirst = new int [(width -border) * height]; //画像imagearrayfirst = imagefirst.getrgb(border、0、(width -border)、height、yimaerrayfirst、0(width -border)); //新しい画像を生成するbufferedimage imageResult = new BuffereDimage(幅、高さ、bufferedimage.type_int_rgb); //黒の背景を解くと、デフォルトのtype_int_rgbはすべて0であり、それらはすべて黒いgraphics2d g =(graphics2d)imageresult.creategraphics()です。 g.setcolor(color.white); G.FillRect(0、0、幅、高さ); //画面全体を入力しますg.dispose(); // border imageresult.setrgb(border、0、(width -border * 2)、height、imagearrayfirst、0、(width -border)); //左半分にrgbファイルのoutfileを設定します= newファイル(out); Imageio.Write(ImageResult、 "jpg"、outfile); // image} catch} catch(ioexception e){logger.error( "フル画像印刷、画像スケーリング、回転処理が失敗しました..."、e); ISOK = false; } isokを返します。 }/ ** *画像のequal-radioズーム * * @paramソース *処理される画像ストリーム * @param targetw * width * @param targeth * height */ public static bufferedimage sezize(bufferedimage source、int targetw、int targeth){int width = source = source = source(); Zoominimage(Source、TargetW、Targeth); //画像の幅と高さが小さすぎる、力を拡大する/* if(width <targetw && height <targeth){return zoominimage(source、targetw、targeth); } else if((width <targetw && width == height)||(height <targeth && width == height)){return zoominimage(source、targetw、targeth); } nullを返します。 */}/ ** *スケーリングする画像のトリミング * * @param Source *保留画像ストリーム * @param startx * Start X Coordinate * @Param Starty * @Param Endx * End X Coordinate * @Param Endy * End Y Coordinate * @return */ public Static BufferedImage Crog(int int intx、int static source source、int starty、int int intx、int source.getWidth(); int height = source.getheight(); if(startx <= -1){startx = 0; } if(starty <= -1){starty = 0; } if(endx <= -1){endx = width -1; } if(endy <= -1){endy = height -1; } bufferedImage result = new BuffereDimage(Endx、Endy、source.getType()); for(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); }} return result; } / ** *指定された角度に画像を回転 * * * @param bufferedimage *ターゲット画像 * @param degute *角度を回転 * @return * / public static bufferedimage rotateimage(最終的な緩衝液バッファレディメージ、最終段階){int w = bufferedimage.getwidth(); int h = bufferedimage.getheigh(); int type = bufferedimage.getColormodel()。getTransparency(); BufferedImage IMG; Graphics2d Graphics2d; (Graphics2d =(img = new BuffereDimage(H、W、Type))。CreateGraphics())。SetRenderingHint(renderingHints.key_interpolation、renderinghints.value_interpolation_bilinear); graphics2d.rotate(math.toradians(degree)、w / 2、h / 2 +(w> h?(w -h) / 2:(h -w) / 2)); Graphics2d.drawimage(bufferedimage、0、0、null); graphics2d.dispose(); IMGを返します。 } / ** *画像が左に90度回転 * * @param bufferedimage * @return * / public static bufferedimage rotateimagelefeft90(bufferedimage bufferedimage){int w = bufferedimage.getWidth(); int h = bufferedimage.getheigh(); int type = bufferedimage.getColormodel()。getTransparency(); BufferedImage IMG; Graphics2d Graphics2d; (Graphics2d =(img = new BuffereDimage(H、W、Type))。CreateGraphics())。SetRenderingHint(renderingHints.key_interpolation、renderinghints.value_interpolation_bilinear); graphics2d.rotate(math.toradians(270)、w / 2、h / 2 +(w -h) / 2); Graphics2d.drawimage(bufferedimage、0、0、null); graphics2d.dispose(); IMGを返します。 } / ** *画像が右折90度 * * @param bufferedimage * @return * / public static bufferedimage rotateimageright90(bufferedimage bufferedimage){int w = bufferedimage.getWidth(); int h = bufferedimage.getheigh(); int type = bufferedimage.getColormodel()。getTransparency(); BufferedImage IMG; Graphics2d Graphics2d; (Graphics2d =(img = new BuffereDimage(H、W、Type))。CreateGraphics())。SetRenderingHint(renderingHints.key_interpolation、renderinghints.value_interpolation_bilinear); graphics2d.rotate(math.toradians(90)、w / 2-(w -h) / 2、h / 2); Graphics2d.drawimage(bufferedimage、0、0、null); graphics2d.dispose(); IMGを返します。 } //フォワードパブリックファイルrotateimageoppo(ファイルファイル)スロー例外{bufferedimage bufferedimage = imageio.read(file); int w = bufferedimage.getWidth(); int h = bufferedimage.getheigh(); int type = bufferedimage.getColormodel()。getTransparency(); BufferedImage IMG; Graphics2d Graphics2d; (Graphics2d =(img = new BuffereDimage(W、H、Type))。CreateGraphics())。SetRenderingHint(renderingHints.key_interpolation、renderinghints.value_interpolation_bilinear); Graphics2d.Rotate(Math.Toradians(180)、W / 2、H / 2); Graphics2d.drawimage(bufferedimage、0、0、null); graphics2d.dispose(); Imageio.write(img、 "jpg"、file);ファイルを返します。 } / *** *画像ミラーリング処理 * * @param file * @param fx * 0は上下に反転1です。 int w = bufferedimage.getWidth(); int h = bufferedimage.getheigh(); 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 ++){tmps [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]; }}} for(int i = 0; i <h; i ++){for(int j = 0; j <w; j ++){bufferedimage.setrgb(j、i、tmps [j] [i]); }} imageio.write(bufferedimage、 "jpg"、file); } catch(Exception e){e.printstacktrace(); }} / ** *画像からズームインまたはズームを押し出します * * @param originalimage * orterimage * @return * / public static bufferedimage zoominimage(bufferedimage)originalimage、int width、int height){befferedimage newimage = new bufferedimage(width、height、heighttytepe();グラフィックg = newimage.getGraphics(); g.drawimage(originalimage、0、0、width、height、null); g.dispose(); NewImageを返します。 } / ***単純な画像認識原理** @param img*画像パス* / public static void siscernimg(string img){try {file fileone = new file(img); bufferedimage bi = imageio.read(fileone); //画像の幅と高さを取得しますint width = bi.getWidth(); int height = bi.getheight(); //(int i = 0; i <height; i ++){for(int j = 0; j <width; j ++){// row scan int dip = bi.getrgb(j、i); if(dip == -1)system.out.print( ""); else system.out.print( "♦"); } system.out.println(); // line break}} catch(Exception e){logger.error( "画像認識エラー"、e); }}}Java関連のコンテンツについては、このサイトに興味のある読者は、「Java画像操作スキルの要約」、「Javaの日付と時刻の操作スキルの概要」、「Java操作DOMノードスキルの概要」、Javaファイルの概要とディレクトリ操作スキルの概要」、「Javaデータ構造の概要」、およびAlgorithmm」を見ることができます。
この記事がみんなのJavaプログラミングに役立つことを願っています。