Artikel ini menjelaskan kelas alat pemrosesan gambar WeChat yang diimplementasikan oleh Java. Bagikan untuk referensi Anda, sebagai berikut:
Sekarang, ada lebih sedikit gambar dan artikel di luar. Saya membaca kode salin tetapi tidak dapat menggunakannya. Saya menggunakan paket JAR yang sesuai untuk memprosesnya, dan banyak yang diskalakan dalam proporsi, yang tidak dapat memenuhi harapan yang saya inginkan: Kelas alat ini adalah kelas yang didasarkan pada Java berdasarkan RGB yang ditulis dalam printer WeChat sebelumnya.
Paket com.zjpz.util; impor java.awt.color; impor java.awt.graphics; impor java.awt.graphics2d; impor java.awt.renderinghints; impor java.awt.geom.affinetransform; impor java.awt.image.buffered. java.awt.image.writableraster; import java.io.file; impor java.io.ioException; import javax.imageio.imageio; pucat * pucat * pucat * pucat * pucat * {loggerfactory;/** * wechat pemrosesan gambar @slf4j.loggerFactory;/** * wechat citra pemrosesan gambar * Static Logger Logger = LoggerFactory.GetLogger (pictureTool.class); public static void main (string [] args) melempar ioException {fileOne cheare = file baru ("c: //1.jpg"); BufferedImage ImageFirst = ImageIo.read (FileOne); Int Border = 0; imagefirst = crop (imagefirst, 0,10,297.300); File outfile = file baru ("d: //2.jpg"); Imageo.write (imagefirst, "jpg", outfile); // tulis gambar}/ *** x koordinat piksel gambar gabungan vertikal*/ private final static int y_width = 645; / *** Pixel koordinat Y dari gambar standar, 920, adalah foto umum, 1099 adalah foto perangko*/ private final static int y_height = 920; / *** CROP X Koordinat piksel indentasi*/ private final static int x_retract = 50; / *** CROP y Koordinat piksel indentasi*/ private final static int y_retract = 50; / ** * Perbatasan gambar default sistem adalah 20 */ Perbatasan int statis akhir publik = 20; / *** Gambar sintetis horizontal*/ public static void xpic (string first, string kedua, string out) {coba {/* 1 Baca gambar pertama*/ fileOneFer fileOne = File baru (pertama); BufferedImage ImageFirst = ImageIo.read (FileOne); int width = imageFirst.getWidth();// Image width int height = imageFirst.getHeight();// Image height int[] imageArrayFirst = new int[width * height];// Read RGB from the image imageArrayFirst = imageFirst.getRGB(0, 0, width, height, imageArrayFirst, 0, width); /* 1 Lakukan hal yang sama untuk gambar kedua*/ file filetwo = file baru (kedua); BufferedImage ImageCond = ImageIo.read (Filetwo); int widthtwo = firficsEcond.getWidth (); // width gambar int heighttwo = firficsecond.getHeight (); // gambar tinggi int [] imageArraysecond = int baru [widthtwo * heighttwo]; ImageArraysecond = ImageSecond.getRgb (0, 0, lebar, heighttwo, imagearraysecond, 0, widthtwo); int h = tinggi; if (height <heighttwo) {h = heighttwo; } // menghasilkan gambar baru bufferedImage imageresult = baru buferedImage (lebar + lebar, h, bufferedimage.type_int_rgb); imageResult.setRGB(0, 0, width, height, imageArrayFirst, 0, width);// Set RGB imageResult.setRGB(width, 0, widthTwo, heightTwo, imageArraySecond, 0, widthTwo);// Set RGB File outFile in the right half = new File(out); Imageo.write (imageresult, "jpg", outfile); // tulis gambar} (pengecualian e) {logger.error ("Gambar sintesis horizontal adalah kesalahan ...", e); }} / *** Gambar sintesis vertikal** @param First* Letakkan jalur gambar di atas* @param kedua* Letakkan jalur gambar di bawah* @param out* Direktori output file* @param Border* Border cadangan gambar* / public static boolean ypic (string pertama, string kedua, string out, border int) {boolean isok = true; coba { /* 1 Baca gambar pertama* / file fileOne = file baru (pertama); BufferedImage ImageFirst = ImageIo.read (FileOne); Int width = imagefirst.getWidth (); // width gambar int tinggi = imagefirst.getHeight (); // tinggi gambar/* 2 melakukan hal yang sama untuk gambar kedua*/ file filetwo = file baru (kedua); BufferedImage ImageCond = ImageIo.read (Filetwo); int widthtwo = firficsEcond.getWidth (); // width gambar int heighttwo = figuresecond.getheight (); // tinggi gambar/ * 1 Baca gambar pertama mulai */ int t_height = y_height - heighttwo; // Gambar adalah gambar horizontal, putar 90 derajat berlawanan arah jarum jam dan skala sama jika (lebar> tinggi) {imagefirst = rotateImageleft90 (imagefirst); } // Equal Scaling ImageFirst = UKURAN (ImageFirst, y_width, t_height); // Ukuran gambar setelah penskalaan lebar = imagefirst.getWidth (); // Lebar gambar tinggi = imagefirst.getHeight (); // tinggi gambar // Setelah penskalaan yang sama-radikal, gambar masih terlalu besar. Pangkas gambar boolean a_w, a_h = false; if ((a_w = (lebar> y_width)) || (a_h = (tinggi> t_height))) {// Mulai posisi x, y koordinat int s_w = 0, s_h = 0; // Saat memangkas koordinat x, indent atribut x_retract if (a_w) {int temp = lebar - y_width; if (temp> x_retract) {temp = x_retract; } else {temp = 0; } s_w = s_w + temp; } // Saat memangkas koordinat y, indentasi atribut y_retract if (a_h) {int temp = tinggi - 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 (); tinggi = imagefirst.getHeight (); } int [] imageArrayFirst = int int [(lebar - border) * tinggi]; // baca rgb imagearrayfirst = imagefirst.getrgb (perbatasan, 0, (lebar - perbatasan), tinggi, imagearrayfirst, 0, (lebar - perbatasan)); / * 2do proses yang sama untuk gambar kedua mulai */ int [] imageArraysecond = int baru [widthtwo * heighttwo]; ImageArraysecond = ImageSecond.getRgb (0, 0, lebar, heighttwo, imagearraysecond, 0, widthtwo); int w = lebar; if (width <widthtwo) {w = widthtwo; } // gambar tinggi int h = tinggi + HeightTwo; // menghasilkan gambar baru bufferedImage imageresult = baru buferedImage (w, h, bufferedimage.type_int_rgb); // Selesaikan latar belakang hitam, type_int_rgb default semuanya 0, dan semuanya adalah grafik hitam2d g = (graphics2d) imageresult.creategraphics (); G.SetColor (Color.White); g.fillrect (0, 0, w, h); // Isi seluruh layar G.Dispose (); // tinggalkan perbatasan imageresult.setrgb (perbatasan, 0, (lebar - perbatasan * 2), tinggi, imagearrayfirst, 0, (lebar - perbatasan)); // atur rgb di setengah kiri imageresult. File (out); Imageo.write (imageresult, "jpg", outfile); // tulis gambar} catch (pengecualian e) {logger.error ("gambar sintesis tegak lurus gagal ...", e); isok = false; } return isok; } / *** Pencetakan gambar penuh, penskalaan gambar dan pemrosesan rotasi** @param Sumber* Gambar yang tertunda pemrosesan* @param out* Direktori output file setelah pemrosesan* @param Border* Gambar yang disediakan Border* / public static boolean maigaopic (sumber string, string out, border int) {boolean isok = true; coba { /* 1 Baca gambar pertama* / file fileOne = file baru (sumber); BufferedImage ImageFirst = ImageIo.read (FileOne); int width = imageFirst.getWidth();// Image width int height = imageFirst.getHeight();// Image height// The picture is a horizontal picture, rotate 90 degrees counterclockwise and scale equally if (width > height) { imageFirst = rotateImageLeft90(imageFirst); } // Equality Scaling ImageFirst = Ubah (imagefirst, y_width, y_height); // Ukuran gambar setelah penskalaan lebar = imagefirst.getWidth (); // width width tinggi = imagefirst.getHeight (); // tinggi gambar // Setelah penskalaan kesetaraan, gambar masih terlalu besar. Pangkas gambar boolean a_w, a_h = false; if ((a_w = (lebar> y_width)) || (a_h = (tinggi> y_height))) {// Mulai posisi x, y koordinat int s_w = 0, s_h = 0; // Saat memangkas koordinat x, indent atribut x_retract if (a_w) {int temp = lebar - y_width; if (temp> x_retract) {temp = x_retract; } else {temp = 0; } s_w = s_w + temp; } // Saat memangkas koordinat y, indentasi atribut y_retract if (a_h) {int temp = tinggi - 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 (); tinggi = imagefirst.getHeight (); } int [] imageArrayFirst = int int [(lebar - border) * tinggi]; // baca rgb dari gambar imagearrayfirst = imagefirst.getrgb (perbatasan, 0, (lebar - batas), tinggi, imagearrayfirst, 0, (lebar - perbatasan)); // menghasilkan gambar baru bufferedImage imageresult = baru buferedImage (lebar, tinggi, bufferedimage.type_int_rgb); // Selesaikan latar belakang hitam, type_int_rgb default semuanya 0, dan semuanya adalah grafik hitam2d g = (graphics2d) imageresult.creategraphics (); G.SetColor (Color.White); g.fillrect (0, 0, lebar, tinggi); // Isi seluruh layar G.Dispose (); // tinggalkan perbatasan imageresult.setrgb (perbatasan, 0, (lebar - perbatasan * 2), tinggi, imagearrayfirst, 0, (lebar - border)); // atur outfile file rgb di setengah kiri = file baru (keluar); Imageo.write (Imageresult, "jpg", outfile); // Tulis gambar}} (ioException e) {logger.error ("Pencetakan gambar penuh, penskalaan gambar dan pemrosesan rotasi gagal ...", e); isok = false; } return isok; }/ ** * Menerapkan zoom-radio yang sama dari gambar * * @param sumber * Stream gambar yang akan diproses * @param targetw * lebar * @param targeth * tinggi * @return */ public static bufferedImage ubah ukuran (levelsImage source, int targetw, int targeth) {int width = source.getwidth ();/ cadangan width, interth) {int width = source.getwidth ();/ gambar widther/ interth) {int width = source.getwidth ();/ gambar widther/ interth) {int width = source.getwidth ();/ Image widthth) {int width = source.getwidth ();/ Image widththing zoominimage (sumber, targetw, targeth); // Lebar dan tinggi gambar terlalu kecil, paksa memperbesar gambar/* if (lebar <targetw && tinggi <targeth) {return zoominimage (sumber, targetw, targeth); } else if ((width <targetw && width == height) || (tinggi <targeth && width == tinggi)) {return zoominimage (sumber, targetw, targeth); } return null; */ } /** * Crop the image to scale* * @param source * Pending image stream* @param startX * Start x coordinate* @param startY * Start y coordinate* @param endX * End x coordinate* @param endY * End y coordinate* @return */ public static BufferedImage crop(BufferedImage source, int startX, int startY, int endX, int endY) { int width = Source.getWidth (); int height = source.getHeight (); if (startx <= -1) {startx = 0; } if (starty <= -1) {starty = 0; } if (endx <= -1) {endx = lebar - 1; } if (endy <= -1) {endy = tinggi - 1; } BufferedImage hasil = BufferedImage baru (endx, endy, source.getType ()); untuk (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); }} hasil pengembalian; } / ** * Putar gambar ke sudut yang ditentukan * * @param bufferedImage * Gambar target * @param derajat * Putar sudut * @return * / public static bufferedImage rotateImage (final bufferedImage bufferedImage, tingkat inter final) {int w = bufferedimage.getwidth (); int h = bufferedImage.getHeight (); int tipe = bufferedImage.getColormodel (). getTransparency (); BufferedImage IMG; Graphics2d Graphics2d; (graphics2d = (img = BufferedImage baru (h, w, type)). CreateGraphics ()). SetRenderingHint (renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); Graphics2d.Rotate (Math.toradians (derajat), w / 2, h / 2 + (w> h? (W - h) / 2: (h - w) / 2)); graphics2d.drawimage (bufferedImage, 0, 0, null); graphics2d.dispose (); kembalikan img; } / ** * Gambar berbelok ke kiri 90 derajat * * @param buferedImage * @return * / public static BufferedImage rotateImageleft90 (bufferedImage bufferedImage) {int w = bufferedImage.getWidth (); int h = bufferedImage.getHeight (); int tipe = bufferedImage.getColormodel (). getTransparency (); BufferedImage IMG; Graphics2d Graphics2d; (graphics2d = (img = BufferedImage baru (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 (); kembalikan img; } / ** * Gambar belok kanan 90 derajat * * @param buferedImage * @return * / public static BufferedImage rotateImagaright90 (bufferedImage bufferedImage) {int w = bufferedImage.getWidth (); int h = bufferedImage.getHeight (); int tipe = bufferedImage.getColormodel (). getTransparency (); BufferedImage IMG; Graphics2d Graphics2d; (graphics2d = (img = BufferedImage baru (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 (); kembalikan img; } // Meneruskan file publik RotateImageOppo (File File) melempar Exception {bufferedImage bufferedImage = ImageIO.read (file); int w = bufferedImage.getWidth (); int h = bufferedImage.getHeight (); int tipe = bufferedImage.getColormodel (). getTransparency (); BufferedImage IMG; Graphics2d Graphics2d; (graphics2d = (img = BufferedImage baru (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 (); Imageo.write (img, "jpg", file); pengembalian file; } / *** * Pemrosesan mirroring gambar * * file @param * @param fx * 0 adalah inversi naik dan turun 1 adalah inversi kiri dan kanan * @return * / public void imagemisro (file file, int fx) {coba {bufferedImage bufferedImage = imageo.read (file); int w = bufferedImage.getWidth (); int h = bufferedImage.getHeight (); int [] [] data = int baru [w] [h]; untuk (int i = 0; i <h; i ++) {for (int j = 0; j <w; j ++) {dataS [j] [i] = bufferedImage.getrgb (j, i); }} int [] [] tmps = int baru [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] = data [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] = data [j] [i]; }}} untuk (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 (); }} / ** * Kekuatan zoom masuk atau keluar dari gambar * * @param originalImage * originalImage * @return * / public static BufferedImage zoominimage (bufferedImage) originalImage, int lebar, int tinggi) {bufferedImage newImage = bufferedImage baru (lebar, tinggi, originalsimage.gettype ();););););););););););); Grafik g = newImage.getGraphics (); G.DrawImage (originalImage, 0, 0, lebar, tinggi, null); g.dispose (); mengembalikan newImage; } / *** Prinsip pengenalan gambar sederhana** @param img* jalur gambar* / public static void disnimg (string img) {coba {file fileOne = file baru (img); BufferedImage Bi = ImageIO.read (FileOne); // Dapatkan lebar dan tinggi gambar int int lebar = bi.getWidth (); int tinggi = bi.getheight (); // Pindai gambar untuk (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 ("kesalahan pengenalan gambar", e); }}}Untuk lebih banyak konten terkait Java, pembaca yang tertarik dengan situs ini dapat melihat topik: "Ringkasan Keterampilan Operasi Gambar Java", "Ringkasan Keterampilan Operasi Tanggal dan Waktu Java", "Ringkasan Keterampilan Operasi Java Dom Node", "Ringkasan File Java dan Keterampilan Operasi Direktori" dan "Tutorial Struktur Data Java dan Algorith.
Saya harap artikel ini akan membantu pemrograman Java semua orang.