ZXing을 사용하여 QR 코드 생성기의 임베디드 그림을 구현하는 데는 다음과 같이 특정 참조 값이 있습니다.
기본 아이디어는 먼저 zxing에 의해 생성 된 QR 코드 이미지를 사용한 다음 이미지를 읽고 아이콘을 삽입 한 다음 전체 이미지를 출력하는 것입니다.
최근 프로젝트에서는 QR 코드를 생성해야합니다. 몇 가지 예를 찾은 후 최종 효과를 얻었습니다. QR 코드는 이미지 형식 (JPG 등)을 생성하거나 웹 페이지에 표시 될 수 있습니다. 이 기사는 기록되어 있으며 많은 유사점이 있으므로 잊고 싶습니다. . . .
참고 : Zxing 포장이 필요한 도구의 경우 일반적인 프로세스는 내장 된 그림을 읽고 컨텐츠를 QR 코드로 변환하고 사진을 QR 코드에 포함시키고 그림을 제작하는 것입니다.
다음은 전체 코드입니다.
import java.awt.basicstroke; import java.awt.color; import java.awt.graphics2d; import java.awt.image; image java.awt.shape; import java.awt.geom.affinetransform; import java.awt.roundrectangle 2d; import java.affinetransformange; java.awt.image.bufferedImage; import java.io.file; import java.io.ioexception; import java.util.hashmap; import java.util.map; import javava.util.map; import com.google.zxing.barcodeformat; import com.zxing.comodehogle com.google.zxing.MultiFormatWriter;import com.google.zxing.WriterException;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;public class Zxing {private static final int BLACK = 0xFF000000;private static final int WHITE = 0xFFFFFFFF;// General 개인 정적 최종 int image_width = 80; 비공개 정적 최종 int image_half_width = image_width / 2; 비공개 정적 최종 int frame_width = 2; // QR 코드 쓰기 개인 정적 multiformatwriter mutiwriter = new multiformatwriter (); public static void main (string [] args) {try {// bitmatrix bitmatrix = multiformatwriter.encode (content, barcodeformat.qr_code, 400, 400, hints); String content = "13400000000"; // qr 코드 내용 버퍼링 이미지 = genbarcode (400, 400, 400,”; if (! imageio.write (image, "jpg", new File ( "f : //2122.jpg"))) {Throw New IoException ( "형식의 이미지를 쓸 수 없음"); } /** // 위의 코드를 여기에서 바꾸고 스트림을 사용하여 페이지에서 읽습니다. outputStream os = response.getOutputStream (); if (! imageio.write (image, "jpg", os)) {wrach new ioexception ( "형식의 이미지를 쓸 수 없음"); } **/} catch (WriteRexception e) {e.printstacktrace ();} catch (ioException e) {// todo auto-auto-auto-auto-auto-auto-auto-auto-auto-auto-auto-auto-auto-autoedimage private bufferedimage tobufferedimage (bitmatrix magrix) {int width = matrix.get int height =; matrix.getheight (); bufferedImage image = new bufferedImage (너비, 높이, bufferedImage.type_int_rgb); for (int x = 0; x <width; x ++) {(int y = 0; y <height; y ++) {y ++) {y ++) {y image.setrgb (y, matrix.gg (x, y)); image;} private static bufferedimage genbarcode (문자열 내용, int 너비, int 높이, 문자열 srcimagepath) writerexception, ioexception {// 소스 이미지 bufferedimage scaleimage = scale (srcimagepath, image_width, image_height, true); int [] [] srcpixels = new int [image_width] [image_height]; for (int i = 0; i <scaleimage.getWidth (); i ++) {for (int j = 0; }} map <EncodeHintType, Object> hint = new Hashmap <EncodeHintType, Object> (); hint.put (encodehinttype.character_set, "utf-8"); // 컨텐츠 인코딩 hint.put (encodeHintType.error_correction, ErrorCorrectionLevel.h); // Error Level Hint.put (EncodeHintType.Margin, 1); // QR 코드의 외부 테두리의 빈 영역의 너비를 설정 // QR 코드 생성 비트 스마트 릭스 매트릭스 = mutiwriter.encode (content, barcodeformat.qr_code, 너비, 높이, 힌트); // 2D 행렬을 1 차원 픽셀 배열로 변환 int halfw = matrix.getWidth () / 2; int halfh = matrix.getheight () / 2; int [] pixels = new int [너비 * 높이]; for (int y = 0; y <matrix.getheight (); y ++) {for (int x = 0; x <matrix.getwidth (); x ++) {// if (x> halfw -image_half_width && x <Halfw+image_half_width && almal _ <hallh+image _ _ h) {픽셀 [y * width + x] = srcpixels [x -halfw + image_half_width] [y -halfh + image_half_width]; } // if ((x> halfw- im frame_width && x <Halfw + image_half_width + frame_width && y> halfh- image_half_width -frame_width && y <halfh + image_half_width + frame_width) || -Image_half_width -frame_width && y <Halfh- image_half_width + frame_width) || frame_width)) {픽셀 [y * width + x] = 0xffffffff; } else {// 여기에서 QR 코드의 색상을 수정할 수 있으며 각각 QR 코드의 색상과 배경을 공식화 할 수 있습니다. 픽셀 [y * width + x] = matrix.get (x, y)? 0xff000000 : 0xffffffff; }}} bufferedImage image = new bufferedImage (너비, 높이, bufferedImage.type_int_rgb); image.getRaster (). setDataelements (0, 0, 너비, 높이, 픽셀); 리턴 이미지; } /** * 요구 사항을 충족하는 아이콘 * @param srcimagefile * 소스 파일 주소 * @param height * target height * @param width * target width * @param hasfiller * 스케일이 부정확 할 때 채우는 것이 필요합니다. 거짓은 채우지 않습니다. * @throws ioException */ private static bufferedImage scale (String srcimagefile, int 높이, int 너비, 부울 해제 릴러)는 ioexception {double ratio = 0.0; // scale file file = 새 파일 (srcimageFile); BufferedImage srcimage = imageio.Read (파일); image destimage = srcimage.getScaledInstance (너비, 높이, bufferedImage.scale_smooth); // if ((srcimage.getheight ()> height) || (srcimage.getwidth ()> width)) {if (srcimage.getheight ()> srcimage.getWidth ()) {ratio = (new Inter (Height)). } else {ratio = (새 정수 (width)). doubleValue () / srcimage.getWidth (); } affinetransformop op = new affinetransformop (affinetransform.getScaleInstance (비율, 비율), null); destimage = op.filter (srcimage, null); } if (hasfiller) {// bufferedImage image = new bufferedImage (너비, 높이, bufferedImage.type_int_rgb); Graphics2d Graphic = image.creategraphics (); Graphic.setColor (color.pink); graphic.fillRect (10, 10, 너비, 높이); Graphic.DrawRect (100, 360, 너비, 높이); if (width == destimage.getWidth (null)) {graphic.DrawImage (destimage, 0, (높이 -Destimage.getheight (null)) / 2, destimage.getwidth (null), destimage.getheight (null), color.white, null); 모양 구성 = New RoundRectangle2d.float (0, (높이 -STEMAGE.GETHEIGH (NULL)) / 2, 너비, 너비, 20, 20); Graphic.SetStroke (새로운 기본 스트로크 (5f)); Graphic.Draw (모양); } else {graphic.drawImage (destimage, (width -destimage.getWidth (null)) / 2, 0, destimage.getWidth (null), destimage.getheight (null), color.white, null); 모양 구성 = new RoundRectangle2d.float ((너비 - destimage.getWidth (null)) / 2, 0, 너비, 너비, 20, 20); Graphic.SetStroke (새로운 기본 스트로크 (5f)); Graphic.Draw (모양); } graphic.dispose (); destimage = 이미지; } return (bufferedImage) destimage; }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.