1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
@SneakyThrows public static BufferedImage genBarCodeWithImg(String codeContent, String srcImagePath) { BufferedImage scaleImage = scale(srcImagePath); 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); } } HashMap<EncodeHintType, Object> hint = new HashMap<>(); hint.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hint.put(EncodeHintType.MARGIN, 1); MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); BitMatrix matrix = multiFormatWriter.encode(codeContent, BarcodeFormat.QR_CODE, CODE_WIDTH, CODE_HEIGHT, hint); int halfW = matrix.getWidth() / 2; int halfH = matrix.getHeight() / 2; int[] pixels = new int[CODE_HEIGHT * CODE_WIDTH]; 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 && y > halfH - IMAGE_HALF_WIDTH && y < halfH + IMAGE_HALF_WIDTH) { pixels[y * CODE_WIDTH + x] = srcPixels[x - halfW + IMAGE_HALF_WIDTH][y - halfH + IMAGE_HALF_WIDTH]; } 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 * CODE_WIDTH + x] = BACKGROUND_COLOR; } else { pixels[y * CODE_WIDTH + x] = matrix.get(x, y) ? FRONT_COLOR : BACKGROUND_COLOR; } } } BufferedImage image = new BufferedImage(CODE_WIDTH, CODE_HEIGHT, BufferedImage.TYPE_INT_RGB); image.getRaster().setDataElements(0, 0, CODE_WIDTH, CODE_HEIGHT, pixels); return image; }
@SneakyThrows public static BufferedImage scale(String srcImageFile) { double radio = 0.0; File file = new File(srcImageFile); BufferedImage srcImage = ImageIO.read(file); Image destImage = srcImage.getScaledInstance(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.SCALE_SMOOTH); if ((srcImage.getHeight() > IMAGE_HEIGHT) || (srcImage.getWidth() > IMAGE_WIDTH)) { if ((srcImage.getHeight() > srcImage.getWidth())) { radio = (new Integer(IMAGE_HEIGHT)).doubleValue() / srcImage.getHeight(); } else { radio = (new Integer(IMAGE_WIDTH)).doubleValue() / srcImage.getWidth(); } AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(radio, radio), null); destImage = op.filter(srcImage, null); } if (HAS_FILLER) { BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = image.createGraphics(); graphics.setColor(Color.white); graphics.fillRect(10, 10, IMAGE_WIDTH, IMAGE_HEIGHT); graphics.drawRect(100, 360, IMAGE_WIDTH, IMAGE_HEIGHT); if (IMAGE_WIDTH == destImage.getHeight(null)) { graphics.drawImage(destImage, 0, (IMAGE_HEIGHT - destImage.getHeight(null)) / 2, destImage.getWidth(null), destImage.getHeight(null), Color.white, null); Shape shape = new RoundRectangle2D.Float(0, (IMAGE_HEIGHT - destImage.getHeight(null)) / 2, IMAGE_WIDTH, IMAGE_WIDTH, 20, 20); graphics.setStroke(new BasicStroke(5f)); graphics.draw(shape); } else { graphics.drawImage(destImage, (IMAGE_WIDTH - destImage.getWidth(null)) / 2, 0, destImage.getWidth(null), destImage.getHeight(null), Color.white, null); Shape shape = new RoundRectangle2D.Float((IMAGE_WIDTH - destImage.getWidth(null)) / 2, 0, IMAGE_WIDTH, IMAGE_WIDTH, 20, 20); graphics.setStroke(new BasicStroke(5f)); graphics.draw(shape); } graphics.dispose(); destImage = image; } return (BufferedImage) destImage; }
|