บทความนี้อธิบายคลาสเครื่องมือประมวลผลภาพ WeChat ที่ใช้โดย Java แบ่งปันสำหรับการอ้างอิงของคุณดังนี้:
ตอนนี้มีรูปภาพและบทความอยู่ข้างนอกน้อยลง ฉันอ่านรหัสคัดลอก แต่ไม่สามารถใช้งานได้ ฉันใช้แพ็คเกจ JAR ที่สอดคล้องกันเพื่อประมวลผลและจำนวนมากถูกปรับขนาดตามสัดส่วนซึ่งไม่สามารถตอบสนองความคาดหวังที่ฉันต้องการได้: คลาสเครื่องมือนี้เป็นคลาสที่ใช้ Java ตาม RGB ที่เขียนในเครื่องพิมพ์ WeChat มาก่อน
แพ็คเกจ com.zjpz.util; นำเข้า java.awt.color; นำเข้า java.awt.graphics; นำเข้า java.awt.graphics2d นำเข้า java.awt.renderinghints นำเข้า java.awt.geom.affinetransform; java.awt.image.writableraster; นำเข้า java.io.file; นำเข้า java.io.ioexception; นำเข้า javax.imageio.imageio; นำเข้า org.slf4j.logger; นำเข้า org.slf4j.loggerfactory;/** * logger logger = loggerFactory.getLogger (picturetool.class); โมฆะคงที่สาธารณะหลัก (สตริง [] args) พ่น IOException {ไฟล์ไฟล์ = ไฟล์ใหม่ ("c: //1.jpg"); bufferedImage imageFirst = imageio.read (fileone); int border = 0; ImageFirst = Crop (ImageFirst, 0,10,297,300); ไฟล์ outfile = ไฟล์ใหม่ ("d: //2.jpg"); Imageio.write (ImageFirst, "jpg", outfile); // เขียนรูปภาพ}/ *** x พิกัดพิกัดพิกเซลของรูปภาพรวมแนวตั้ง*/ ส่วนตัวสุดท้ายคงที่ int y_width = 645; / *** พิกเซลพิกัด y ของรูปภาพมาตรฐาน, 920, เป็นภาพถ่ายทั่วไป, 1099 เป็นภาพถ่ายแสตมป์*/ ส่วนตัวสุดท้ายคงที่ int y_height = 920; / *** crop x พิกัดพิกเซลเยื้อง*/ ส่วนตัวสุดท้ายคงที่ int x_retract = 50; / *** crop y พิกัดพิกเซลที่เยื้อง*/ ส่วนตัวสุดท้ายคงที่ int y_retract = 50; / ** * เส้นขอบภาพเริ่มต้นของระบบคือ 20 */ สาธารณะสุดท้ายคงที่ int int เส้นขอบ = 20; / *** ภาพสังเคราะห์แนวนอน*/ โมฆะแบบคงที่สาธารณะ XPIC (สตริงแรก, สตริงที่สอง, สตริงออก) {ลอง {/* 1 อ่านภาพแรก*/ ไฟล์ไฟล์ = ไฟล์ใหม่ (แรก); bufferedImage imageFirst = imageio.read (fileone); int width = imagefirst.getWidth (); // ความกว้างของภาพความสูง int = imageFirst.getheight (); // ความสูงของภาพ int [] imageArrayfirst = ใหม่ int [ความกว้าง * ความสูง]; // อ่าน rgb จากภาพ imagearrayfirst = imagefirst.getrgb (0, 0, ความสูง /* 1 ทำเช่นเดียวกันสำหรับภาพที่สอง*/ ไฟล์ filetwo = ไฟล์ใหม่ (ที่สอง); BufferedImage imagesecond = imageio.read (filetwo); int widthtwo = imagesecond.getWidth (); // ภาพความกว้างของภาพ int heighttwo = imagesecond.getheight (); // ความสูงของภาพ int [] imagearraysecond = ใหม่ int [widthtwo * ความสูง]; imageArraysecond = imagesecond.getRgb (0, 0, Widthtwo, Highttwo, ImageArraysecond, 0, WidthTwo); int h = ความสูง; ถ้า (ความสูง <ความสูง) {h = ความสูง; } // สร้างภาพใหม่ bufferedImage imageResult = ใหม่ bufferedImage (ความกว้าง + widthtwo, h, bufferedimage.type_int_rgb); imageResult.setRgb (0, 0, ความกว้าง, ความสูง, imageArrayfirst, 0, ความกว้าง); // ตั้งค่า rgb imageeresult.setrgb (ความกว้าง, 0, widthtwo, ighttwo, imagearraysecond, 0, widthtwo); imageio.write (imageResult, "jpg", outfile); // เขียนภาพ} catch (Exception e) {logger.error ("ภาพการสังเคราะห์แนวนอนเป็นข้อผิดพลาด ... ", e); }} / *** ภาพการสังเคราะห์แนวตั้ง** @param ก่อน* ใส่เส้นทางภาพด้านบน* @param วินาที* วางเส้นทางภาพด้านล่าง* @param out* ไดเรกทอรีไฟล์เอาท์พุทไฟล์* @param เส้นขอบ* ภาพที่สงวนไว้* ลอง { /* 1 อ่านภาพแรก* / ไฟล์ไฟล์ = ไฟล์ใหม่ (แรก); bufferedImage imageFirst = imageio.read (fileone); int width = imagefirst.getWidth (); // ความกว้างของภาพความสูง int ความสูง = imagefirst.getheight (); // ความสูงของภาพ/* 2 ทำเช่นเดียวกันสำหรับภาพที่สอง*/ ไฟล์ filetwo = ไฟล์ใหม่ (ที่สอง); BufferedImage imagesecond = imageio.read (filetwo); int widthtwo = imagesecond.getWidth (); // ภาพความกว้างของภาพ int heighttwo = imagesecond.getheight (); // ความสูงของภาพ/ * 1 อ่านภาพแรกเริ่มต้น */ int t_height = y_height - ความสูง // ภาพเป็นภาพแนวนอนหมุน 90 องศาทวนเข็มนาฬิกาและสเกลเท่า ๆ กันถ้า (ความกว้าง> ความสูง) {ImageFirst = RotateMageleft90 (ImageFirst); } // การปรับสเกลที่เท่ากัน ImageFirst = Resize (ImageFirst, y_width, t_height); // ขนาดของภาพหลังจากความกว้างการปรับขนาด = imageFirst.getWidth (); // ความกว้างความกว้างของภาพ = imagefirst.getheight (); // ความสูงของภาพ // หลังจากการปรับขนาดเท่ากันภาพยังมีขนาดใหญ่เกินไป ครอบตัดบูลีนรูปภาพ A_W, A_H = FALSE; if ((a_w = (ความกว้าง> y_width)) || (a_h = (ความสูง> t_height))) {// เริ่มตำแหน่ง x, พิกัด int s_w = 0, s_h = 0; // เมื่อการครอบตัด x พิกัดแอตทริบิวต์เยื้อง x_retract ถ้า (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 ถ้า (a_h) {int temp = ความสูง - 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); ความกว้าง = imageFirst.getWidth (); ความสูง = imagefirst.getheight (); } int [] imageArrayfirst = new int [(ความกว้าง - เส้นขอบ) * ความสูง]; // อ่าน rgb imagearrayfirst = imagefirst.getRgb (เส้นขอบ, 0, (ความกว้าง - เส้นขอบ), ความสูง, imageArrayfirst, 0, (ความกว้าง - ชายแดน) / * 2DO กระบวนการเดียวกันสำหรับภาพที่สองเริ่มต้น */ int [] imageArraySecond = new int [widthtwo * heighttwo]; imageArraysecond = imagesecond.getRgb (0, 0, Widthtwo, Highttwo, ImageArraysecond, 0, WidthTwo); int w = ความกว้าง; if (width <widthtwo) {w = widthtwo; } // ความสูงของภาพ int h = ความสูง + ความสูง two; // สร้างภาพใหม่บัฟเฟอร์เมจ imageResult = bufferedImage ใหม่ (w, h, bufferedimage.type_int_rgb); // แก้พื้นหลังสีดำ Type_Int_RGB เริ่มต้นทั้งหมด 0 และพวกเขาทั้งหมดเป็นสีดำกราฟิก 2D G = (graphics2d) imageResult.createGraphics (); G.SetColor (color.white); G.FillRect (0, 0, W, H); // เติมทั้งหน้าจอ G.Dispose (); // ปล่อยให้ชายแดน imageResult.setrgb (ชายแดน, 0, (ความกว้าง - ชายแดน * 2), ความสูง, imageArrayfirst, 0, (ความกว้าง - เส้นขอบ)); // ตั้งค่า rgb ในครึ่งซ้าย imageResult.setrgb (0, ความสูง, ความกว้าง ไฟล์ (ออก); imageio.write (imageResult, "jpg", outfile); // เขียนภาพ} catch (Exception e) {logger.error ("ภาพการสังเคราะห์ตั้งฉากล้มเหลว ... ", e); isok = false; } return isok; } / *** การพิมพ์ภาพเต็มรูปแบบการปรับขนาดภาพและการประมวลผลการหมุน** @param แหล่งที่มา* การประมวลผลภาพที่รอการประมวลผล* @param out* ไดเรกทอรีเอาต์พุตไฟล์หลังจากการประมวลผล* @param border* ภาพที่สงวนไว้* / public boolean maigaopic (สตริงแหล่งที่มา ลอง { /* 1 อ่านภาพแรก* / ไฟล์ไฟล์ = ไฟล์ใหม่ (แหล่งที่มา); bufferedImage imageFirst = imageio.read (fileone); ความกว้าง int = imageFirst.getWidth (); // ความกว้างของภาพความสูง int = imageFirst.getheight (); // ความสูงของภาพ // ภาพเป็นภาพแนวนอนหมุน 90 องศาทวนเข็มนาฬิกาและสเกลเท่า ๆ กันถ้า (ความกว้าง> ความสูง) } // การปรับสเกลที่เท่าเทียมกัน ImageFirst = Resize (ImageFirst, y_width, y_height); // ขนาดของภาพหลังจากความกว้างการปรับขนาด = imageFirst.getWidth (); // ความกว้างความกว้างของภาพ = imagefirst.getheight (); // ความสูงของภาพ // หลังจากการปรับขนาดเท่าที่เท่าเทียมกันภาพยังมีขนาดใหญ่เกินไป ครอบตัดบูลีนรูปภาพ A_W, A_H = FALSE; if ((a_w = (width> y_width)) || (a_h = (ความสูง> y_height))) {// เริ่มตำแหน่ง x, พิกัด int s_w = 0, s_h = 0; // เมื่อการครอบตัด x พิกัดแอตทริบิวต์เยื้อง x_retract ถ้า (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 ถ้า (a_h) {int temp = ความสูง - 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); ความกว้าง = imageFirst.getWidth (); ความสูง = imagefirst.getheight (); } int [] imageArrayfirst = new int [(ความกว้าง - เส้นขอบ) * ความสูง]; // อ่าน rgb จาก imagearrayfirst = imagefirst.getrgb (เส้นขอบ, 0, (ความกว้าง - เส้นขอบ), ความสูง, imagearrayfirst, 0, (ความกว้าง - ชายแดน); // สร้างภาพบัฟเฟอร์ภาพใหม่ imageResult = bufferedImage ใหม่ (ความกว้าง, ความสูง, bufferedImage.type_int_rgb); // แก้พื้นหลังสีดำ Type_Int_RGB เริ่มต้นทั้งหมด 0 และพวกเขาทั้งหมดเป็นสีดำกราฟิก 2D G = (graphics2d) imageResult.createGraphics (); G.SetColor (color.white); G.FillRect (0, 0, ความกว้าง, ความสูง); // เติมทั้งหน้าจอ G.Dispose (); // ปล่อยให้ชายแดน imageResult.setrgb (ชายแดน, 0, (ความกว้าง - ชายแดน * 2), ความสูง, imagearrayfirst, 0, (ความกว้าง - เส้นขอบ)); // ตั้งค่าไฟล์ RGB outfile ในครึ่งซ้าย = ไฟล์ใหม่ (ออก); imageio.write (imageeresult, "jpg", outfile); // เขียนภาพ} catch (ioexception e) {logger.error ("การพิมพ์ภาพเต็มรูปแบบการปรับขนาดภาพและการประมวลผลการหมุนล้มเหลว ... ", e); isok = false; } return isok; }/ ** * ใช้การซูมที่เท่าเทียมกันของภาพ * * @param แหล่งที่มา * สตรีมรูปภาพที่จะประมวลผล * @param targetw * width * @param targeth * ความสูง * @return */ public bufferedimage risize (bufferedimage source, int targetw, int targeth) ZOOMINIMAGE (แหล่งที่มา, TargetW, Targeth); // ความกว้างของภาพและความสูงมีขนาดเล็กเกินไปแรงจะขยายภาพ/* ถ้า (ความกว้าง <targetW && ความสูง <targeth) {return zoominimage (แหล่งที่มา, TargetW, Targeth); } อื่นถ้า ((width <targetW && width == ความสูง) || (ความสูง <targeth && width == ความสูง)) {return zoominimage (แหล่งที่มา, targetW, targeth); } return null; */}/ ** * ครอบตัดภาพไปยังสเกล * * @param แหล่งที่มา * สตรีมรูปภาพที่รอดำเนินการ * @param startx * เริ่ม x พิกัด * @param starty * start y พิกัด * @param endx * end x ประสานงาน * @param Endy * สิ้นสุด y source.getWidth (); ความสูง int = source.getheight (); if (startx <= -1) {startx = 0; } if (starty <= -1) {starty = 0; } if (endx <= -1) {endx = width - 1; } if (endy <= -1) {endy = ความสูง - 1; } bufferedImage result = new bufferedImage (endx, endy, source.getType ()); สำหรับ (int y = starty; y <endy+starty; y ++) {สำหรับ (int x = startx; x <endx+startx; x ++) {int rgb = source.getRgb (x, y); result.setrgb (x - startx, y - starty, rgb); }} ผลการส่งคืน; } / ** * หมุนภาพไปยังมุมที่ระบุ * * @param bufferedImage * ภาพเป้าหมาย * @param องศา * หมุนมุม * @return * / public bufferedImage rotateimage (บัฟเฟอร์เมจบัฟเฟอร์ขั้นสุดท้าย, ระดับ int สุดท้าย) {int w = bufferedimage int h = bufferedImage.getheight (); 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 (องศา), w / 2, h / 2 + (w> h? (w - h) / 2: (h - w) / 2)); graphics2d.drawimage (bufferedimage, 0, 0, null); Graphics2d.Dispose (); return img; } / ** * ภาพเลี้ยวซ้าย 90 องศา * * @param bufferedImage * @return * / public bufferedimage rotateimageleft90 (bufferedimage bufferedimage) {int w = bufferedimage.getWidth (); int h = bufferedImage.getheight (); 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 (); return img; } / ** * ภาพเลี้ยวขวา 90 องศา * * @param bufferedImage * @return * / public bufferedimage rotateimageright90 (bufferedImage bufferedImage) {int w = bufferedimage.getWidth (); int h = bufferedImage.getheight (); 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 (); return img; } // ส่งต่อไฟล์สาธารณะ rotateimageoppo (ไฟล์ไฟล์) พ่นข้อยกเว้น {bufferedImage bufferedImage = imageio.read (ไฟล์); int w = bufferedImage.getWidth (); int h = bufferedImage.getheight (); 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", ไฟล์); ส่งคืนไฟล์; } / *** * การประมวลผลภาพมิเรอร์ * * * @param ไฟล์ * @param fx * 0 ขึ้นและลงการผกผัน 1 คือซ้ายและขวาผกผัน * @return * / โมฆะสาธารณะ imagemisro (ไฟล์ไฟล์, int fx) {ลอง {bufferedimage bufferedimage = imageio.read (ไฟล์); int w = bufferedImage.getWidth (); int h = bufferedImage.getheight (); int [] [] datas = new int [w] [h]; สำหรับ (int i = 0; i <h; i ++) {สำหรับ (int j = 0; j <w; j ++) {data [j] [i] = bufferedimage.getRgb (j, i); }} int [] [] tmps = new int [w] [h]; if (fx == 0) {สำหรับ (int i = 0, a = h-1; i <h; i ++, a--) {สำหรับ (int j = 0; j <w; j ++) {tmps [j] [a] = datas [j] [i]; }}} อื่นถ้า (fx == 1) {สำหรับ (int i = 0; i <h; i ++) {สำหรับ (int j = 0, b = w-1; j <w; j ++, b--) {tmps [b] [i] = data [j] [i]; }}} สำหรับ (int i = 0; i <h; i ++) {สำหรับ (int j = 0; j <w; j ++) {bufferedimage.setrgb (j, i, tmps [j] [i]); }} imageio.write (bufferedImage, "jpg", ไฟล์); } catch (exception e) {e.printstacktrace (); }} / ** * บังคับซูมเข้าหรือออกจากภาพ * * @param OriginalImage * OriginalImage * @return * / Public Static BufferedImage Zoominimage (bufferedImage) OriginalImage, ความกว้าง int, ความสูง int) กราฟิก g = newImage.getGraphics (); G.Drawimage (OriginalImage, 0, 0, ความกว้าง, ความสูง, null); G.Dispose (); คืนนิวเมจ; } / *** หลักการจดจำรูปภาพง่าย ๆ** @param img* เส้นทางภาพ* / โมฆะสาธารณะคงที่ discernimg (สตริง img) {ลอง {ไฟล์ไฟล์ = ไฟล์ใหม่ (img); bufferedImage bi = imageio.read (fileone); // รับความกว้างและความสูงของภาพความกว้าง int = bi.getWidth (); ความสูง int = bi.getheight (); // สแกนภาพสำหรับ (int i = 0; i <ความสูง; i ++) {สำหรับ (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 Date และ Time Operation Skills", "บทสรุปของทักษะการดำเนินงาน Java Dom Node", "สรุปของไฟล์ Java และทักษะการดำเนินการไดเรกทอรี"
ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม Java ของทุกคน