/* * create QR code with overlay */ public static void createQRCode( String qrCodeData, String outputFilePath, String overlayFilePath, String charset, Map<EncodeHintType, ErrorCorrectionLevel> hintMap, int qrCodeheight, int qrCodewidth) throws WriterException, IOException { // create QR code <BufferedImage> QRCodeWriter qrWriter = new QRCodeWriter(); BitMatrix matrix = qrWriter.encode(qrCodeData, BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintMap); BufferedImage image = MatrixToImageWriter.toBufferedImage(matrix); // read overlay image BufferedImage overlay = ImageIO.read(new File(overlayFilePath)); // Draw the new image int deltaHeight = image.getHeight() - overlay.getHeight(); int deltaWidth = image.getWidth() - overlay.getWidth(); BufferedImage combined = new BufferedImage(qrCodeheight, qrCodewidth, BufferedImage.TYPE_INT_ARGB); Graphics2D g = (Graphics2D) combined.getGraphics(); g.drawImage(image, 0, 0, null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f)); g.drawImage(overlay, (int) Math.round(deltaWidth / 2), (int) Math.round(deltaHeight / 2), null); ImageIO.write(combined, "PNG", new File(outputFilePath)); }
private List<BufferedImage> exportSplitCompress(String s) throws IOException, WriterException { final QRCodeWriter writer = new QRCodeWriter(); final int multiple = 1; final Hashtable hints = new Hashtable(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); final Compression comp = new CompressionZlib(); final byte data[] = comp.compress(s.getBytes("UTF-8")); final List<BufferedImage> result = new ArrayList<BufferedImage>(); final List<byte[]> blocs = new ArrayList<byte[]>(); for (int i = 0; i < 4; i++) { blocs.add(getSplited(data, i, 4)); } blocs.add(xor(blocs)); for (byte d[] : blocs) { // Encoder.DEFAULT_BYTE_MODE_ENCODING final BitMatrix bit = writer.encode(new String(d, "ISO-8859-1"), BarcodeFormat.QR_CODE, multiple); result.add(MatrixToImageWriter.toBufferedImage(bit)); } return Collections.unmodifiableList(result); }
/** * 写入二维码、以及将照片logo写入二维码中 * * @param matrix 要写入的二维码 * @param format 二维码照片格式 * @param imagePath 二维码照片保存路径 * @param logoPath logo路径 * @throws IOException */ @SuppressWarnings({"deprecation"}) public static void writeToFile(BitMatrix matrix, String format, String imagePath, String logoPath) throws IOException { MatrixToImageWriter.writeToFile(matrix, format, new File(imagePath), new MatrixToImageConfig()); // 添加logo图片, 此处一定需要重新进行读取,而不能直接使用二维码的BufferedImage 对象 BufferedImage img = ImageIO.read(new File(imagePath)); MatrixToImageWriterEx.overlapImage(img, format, imagePath, logoPath, DEFAULT_CONFIG); }
public static BufferedImage toImage(String content, int width, int height) { try { BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height); return MatrixToImageWriter.toBufferedImage(bitMatrix); } catch (WriterException e) { throw new RuntimeException(e); } }
private List<BufferedImage> exportFlashcodeSimple(String s) throws IOException, WriterException { final QRCodeWriter writer = new QRCodeWriter(); final int multiple = 1; final Hashtable hints = new Hashtable(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); final BitMatrix bit = writer.encode(s, BarcodeFormat.QR_CODE, multiple); final BufferedImage im = MatrixToImageWriter.toBufferedImage(bit); return Arrays.asList(im); }
/** * 生成二维码 * * @param content 条码文本内容 * @param width 条码宽度 * @param height 条码高度 * @param fileType 文件类型,如png * @param savePath 保存路径 */ @SuppressWarnings({"rawtypes", "unchecked", "deprecation"}) public static void encode( String content, int width, int height, String fileType, String savePath) { try { content = new String(content.getBytes(ToolString.encoding), ToolString.encoding); // 二维码内容 Hashtable hints = new Hashtable(); hints.put(EncodeHintType.CHARACTER_SET, ToolString.encoding); BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); File file = new File(savePath); MatrixToImageWriter.writeToFile(bitMatrix, fileType, file); } catch (Exception e) { e.printStackTrace(); } }
private List<BufferedImage> exportFlashcodeCompress(String s) throws IOException, WriterException { final QRCodeWriter writer = new QRCodeWriter(); final int multiple = 1; final Hashtable hints = new Hashtable(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); final Compression comp = new CompressionZlib(); final byte data[] = comp.compress(s.getBytes("UTF-8")); // Encoder.DEFAULT_BYTE_MODE_ENCODING final BitMatrix bit = writer.encode(new String(data, "ISO-8859-1"), BarcodeFormat.QR_CODE, multiple); final BufferedImage im = MatrixToImageWriter.toBufferedImage(bit); return Arrays.asList(im); }
public static BufferedImage toSingleQrCodeBufferedImage( String s, ErrorCorrectionLevel ec, int scaleFactor) throws WriterException { QRCode qrCode = new QRCode(); Encoder.encode(s, ec, qrCode); BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(qrCode.getMatrix()); if (scaleFactor != 1) { int newWidth = bufferedImage.getWidth() * scaleFactor; int newHeight = bufferedImage.getHeight() * scaleFactor; Image image = bufferedImage.getScaledInstance(newWidth, newHeight, Image.SCALE_FAST); bufferedImage = ImageIoUtils.toBufferedImage(image); } return (bufferedImage); }
/** * 二维码编码 * * @param contents * @param width * @param height * @param imgPath */ public static void encode2(String contents, int width, int height, String imgPath) { Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); // 指定纠错等级 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 指定编码格式 hints.put(EncodeHintType.CHARACTER_SET, "GBK"); try { BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints); MatrixToImageWriter.writeToFile(bitMatrix, "png", new File(imgPath)); } catch (Exception e) { e.printStackTrace(); } }
public static void setCode() throws Exception { String filePath = "/home/static/images/"; String fileName = "dcn.png"; JSONObject json = new JSONObject(); json.put("dcn", "http://kf.d.cn"); // String content = json.toJSONString(); String content = "http://kf.d.cn"; int width = 200; int height = 200; String format = "png"; Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); Path path = FileSystems.getDefault().getPath(filePath, fileName); MatrixToImageWriter.writeToPath(bitMatrix, format, path); }
/* * create simple QR code */ public static void createQRCode( String qrCodeData, String filePath, String charset, int qrCodeheight, int qrCodewidth, Map<EncodeHintType, ?> hintEncodeMap) throws WriterException, IOException { BitMatrix matrix = new MultiFormatWriter() .encode( new String(qrCodeData.getBytes(charset), charset), BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintEncodeMap); ImageIO.write(MatrixToImageWriter.toBufferedImage(matrix), "PNG", new File(filePath)); }
/** * 条形码编码 * * @param contents * @param width * @param height * @param imgPath */ public static void encode(String contents, int width, int height, String imgPath) { int codeWidth = 3 + // start guard (7 * 6) + // left bars 5 + // middle guard (7 * 6) + // right bars 3; // end guard codeWidth = Math.max(codeWidth, width); try { BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.EAN_13, codeWidth, height, null); MatrixToImageWriter.writeToFile(bitMatrix, "png", new File(imgPath)); } catch (Exception e) { e.printStackTrace(); } }
/** @param args */ public static void main(String[] args) { Charset charset = Charset.forName("UTF-8"); CharsetEncoder encoder = charset.newEncoder(); byte[] b = null; try { // Convert a string to UTF-8 bytes in a ByteBuffer ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("http://www.google.co.in/")); b = bbuf.array(); } catch (CharacterCodingException e) { System.out.println(e.getMessage()); } String data; try { data = new String(b, "UTF-8"); // get a byte matrix for the data BitMatrix matrix = null; ByteMatrix byteMatrix = null; int h = 100; int w = 100; com.google.zxing.Writer writer = new MultiFormatWriter(); try { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); matrix = writer.encode(data, com.google.zxing.BarcodeFormat.QR_CODE, w, h, hints); } catch (com.google.zxing.WriterException e) { System.out.println(e.getMessage()); } // change this path to match yours (this is my mac home folder, you can use: c:\\qr_png.png if // you are on windows) String filePath = "D:\\CreateQR.JPG"; File file = new File(filePath); try { MatrixToImageWriter.writeToFile(matrix, "JPG", file); System.out.println("printing to " + file.getAbsolutePath()); } catch (IOException e) { System.out.println(e.getMessage()); } } catch (UnsupportedEncodingException e) { System.out.println(e.getMessage()); } }
/** * Generate a QR code with the payment information of a given size, optionally, with branding * * @param size A size of the QR code (without the branding) in pixels * @param paymentString A SPAYD string with payment information * @param hasBranging A flag that can be used to turn on/off branding * @return An image with the payment QR code * @throws IOException */ public static BufferedImage getQRCode(Integer size, String paymentString, boolean hasBranging) throws IOException { if (size == null) { size = SpaydConstants.defQRSize; } else if (size < SpaydConstants.minQRSize) { size = SpaydConstants.minQRSize; } else if (size > SpaydConstants.maxQRSize) { size = SpaydConstants.maxQRSize; } BitMatrix matrix = null; int h = size; int w = size; int barsize = -1; Writer writer = new MultiFormatWriter(); try { Map<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-1"); QRCode code = Encoder.encode(paymentString, ErrorCorrectionLevel.M, hints); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); barsize = size / (code.getMatrix().getWidth() + 8); matrix = writer.encode(paymentString, com.google.zxing.BarcodeFormat.QR_CODE, w, h, hints); } catch (com.google.zxing.WriterException e) { System.out.println(e.getMessage()); } if (matrix == null || barsize < 0) { throw new InvalidFormatException(); } BufferedImage image = MatrixToImageWriter.toBufferedImage(matrix); if (hasBranging) { Graphics2D g = (Graphics2D) image.getGraphics(); BasicStroke bs = new BasicStroke(2); g.setStroke(bs); g.setColor(Color.BLACK); g.drawLine(0, 0, w, 0); g.drawLine(0, 0, 0, h); g.drawLine(w, 0, w, h); g.drawLine(0, h, w, h); String str = "QR Platba"; int fontSize = size / 12; g.setFont(new Font("Verdana", Font.BOLD, fontSize)); FontMetrics fm = g.getFontMetrics(); Rectangle2D rect = fm.getStringBounds(str, g); g.setColor(Color.WHITE); g.fillRect( 2 * barsize, h - fm.getAscent(), (int) rect.getWidth() + 4 * barsize, (int) rect.getHeight()); int padding = 4 * barsize; BufferedImage paddedImage = new BufferedImage(w + 2 * padding, h + padding + (int) rect.getHeight(), image.getType()); Graphics2D g2 = paddedImage.createGraphics(); g2.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); g2.setFont(new Font("Verdana", Font.BOLD, fontSize)); g2.setPaint(Color.WHITE); g2.fillRect(0, 0, paddedImage.getWidth(), paddedImage.getHeight()); g2.drawImage(image, padding, padding, Color.WHITE, null); g2.setColor(Color.BLACK); g2.drawString(str, padding + 4 * barsize, (int) (padding + h + rect.getHeight() - barsize)); image = paddedImage; } return image; }