English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Using Zxing to implement the QR code generator with embedded images has certain reference value, as follows:
The basic idea is to use the QR code image generated by zxing first, then read the image, insert the icon in it, and then output the entire image.
In the recent project, I needed to generate a QR code and found several examples to combine and make the final effect. The QR code can be generated in image format (jpg, etc.) or displayed on a web page. This article is for record purposes only, with many similarities, please include it.
Note: The Zxing wrapper utility class is required, and the general process is to read the embedded image, convert the content into a QR code, embed the image into the QR code, and output the image. This article is for record purposes only, with many similarities, please include it.
The complete code is as follows:
import Java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics;2D; import java.awt.Image; import java.awt.Shape; import java.awt.geom.AffineTransform; import java.awt.geom.RoundRectangle;2D; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import 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 picture width private static final int IMAGE_WIDTH = 80; private static final int IMAGE_HEIGHT = 80; private static final int IMAGE_HALF_WIDTH = IMAGE_WIDTH / 2; private static final int FRAME_WIDTH = 2; // QR Code encoder private static 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";//Content of the QR code BufferedImage image = genBarcode(content, 400, 400, "F:\\amazed.png"); if (!ImageIO.write(image, "jpg", new File("F:\\2122.jpg"))) { throw new IOException("Could not write an image of format "); } /** //Replace the above code with this one, and the stream will be read into the page OutputStream os = response.getOutputStream(); if (!ImageIO.write(image, "jpg", os)) { throw new IOException("Could not write an image of format "); } **/ } catch (WriterException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); } } return image; } private static BufferedImage genBarcode(String content, int width, int height, String srcImagePath) throws WriterException, IOException { // Read the source image BufferedImage scaleImage = scale(srcImagePath, IMAGE_WIDTH, , true); int[][] srcPixels = new int[IMAGE_WIDTH][IMAGE_HEIGHT]; for (int i = 0; i < scaleImage.getWidth(); i++) { for (int j = 0; j < scaleImage.getHeight(); j++) { srcPixels[i][j] = scaleImage.getRGB(i, j); } } Map<EncodeHintType, Object> hint = new HashMap<EncodeHintType, Object>(); hint.put(EncodeHintType.CHARACTER_SET, "utf-8"); //Content encoding hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//Error level hint.put(EncodeHintType.MARGIN, 1); //Set the width of the blank area outside the QR code border // Generate QR code BitMatrix matrix = mutiWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hint); // Convert a two-dimensional matrix to a one-dimensional pixel array int halfW = matrix.getWidth() / 2; int halfH = matrix.getHeight() / 2; int[] pixels = new int[width * height]; for (int y = 0; y < matrix.getHeight(); y++) { for (int x = 0; x < matrix.getWidth(); x++) { // Read the picture if (x > halfW - IMAGE_HALF_WIDTH && x < halfW + IMAGE_HALF_WIDTH && y > halfH - IMAGE_HALF_WIDTH && y < halfH + IMAGE_HALF_WIDTH) { pixels[y * width + x] = srcPixels[x - halfW + IMAGE_HALF_WIDTH][y - halfH + IMAGE_HALF_WIDTH]; } // Form a border around the picture else if ((x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW - IMAGE_HALF_WIDTH + FRAME_WIDTH && y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH) || (x > halfW + IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH && y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH) || (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH && y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH - IMAGE_HALF_WIDTH + FRAME_WIDTH) || (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH && y > halfH + IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH)) { pixels[y * width + x] = 0xfffffff; } else { // Here you can modify the color of the QR code, and you can specify the color of the QR code and the background separately; pixels[y * width + x] = matrix.get(x, y) &63; 0xff000000 : 0xfffffff; } } } BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image.getRaster().setDataElements(0, 0, width, height, pixels); return image; } /** * Scale the original image passed in according to height and width to generate an icon that meets the requirements * * @param srcImageFile * Source file address * @param height * Target height * @param width * Target width * @param hasFiller * Whether to fill in blank when the proportion is not correct: true for filling; false for not filling; * @throws IOException */ private static BufferedImage scale(String srcImageFile, int width, int height, RenderingHints hint) int width, boolean hasFiller) throws IOException { double ratio = 0.0; // scale ratio File file = new File(srcImageFile); BufferedImage srcImage = ImageIO.read(file); Image destImage = srcImage.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH); // calculate scale if ((srcImage.getHeight() > height) || (srcImage.getWidth() > width)) { if (srcImage.getHeight() > srcImage.getWidth()) { ratio = (new Integer(height)).doubleValue() / srcImage.getHeight(); } else { ratio = (new Integer(width)).doubleValue() / srcImage.getWidth(); } AffineTransformOp op = new AffineTransformOp( AffineTransform.getScaleInstance(ratio, ratio), null); destImage = op.filter(srcImage, null); } if (hasFiller) {// padding BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D graphic = image.createGraphics(); graphic.setColor(Color.PINK); graphic.fillRect(10, 10, width, height); graphic.drawRect(100, 360, width, height); if (width == destImage.getWidth(null)) { graphic.drawImage(destImage, 0, (height - destImage.getHeight(null)) / 2, destImage.getWidth(null), destImage.getHeight(null), Color.white, null); Shape shape = new RoundRectangle2D.Float(0, (height - destImage.getHeight(null)) / 2, width, width, 20, 20); graphic.setStroke(new BasicStroke(5f)); graphic.draw(shape); } else { graphic.drawImage(destImage, (width - destImage.getWidth(null)) / 2, 0, destImage.getWidth(null), destImage.getHeight(null), Color.white, null); Shape shape = new RoundRectangle2D.Float((width - destImage.getWidth(null)) / 2, 0, width, width, 20, 20); graphic.setStroke(new BasicStroke(5f)); graphic.draw(shape); } graphic.dispose(); destImage = image; } return (BufferedImage) destImage; } }
That's all for this article, I hope it will be helpful to everyone's learning, and I also hope everyone will support the Yelling Tutorial more.
Statement: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, does not edit manually, and does not assume relevant legal responsibility. If you find any copyright-infringing content, please send an email to notice#w3Please report any infringement by sending an email to codebox.com (replace # with @ when sending emails) and provide relevant evidence. Once verified, this site will immediately delete the infringing content.