private Bitmap renderQRCode(String barcode, BarcodeFormat format, int width) throws WriterException { float scale = this.context.getResources().getDisplayMetrics().density; width *= scale * 0.6; Writer barWriter = new MultiFormatWriter(); Bitmap bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888); BitMatrix bm = barWriter.encode(barcode, format, width, width); for (int j = 0; j < width; j++) { int[] row = new int[width]; for (int i = 0; i < width; i++) { row[i] = bm.get(i, j) ? Color.BLACK : Color.TRANSPARENT; } bitmap.setPixels(row, 0, width, 0, j, width, 1); } CropUtility cropUtility = new CropUtility(); Bitmap cropped = cropUtility.rectangularCrop(bitmap, Color.TRANSPARENT, 0); FactoryManager manager = new FactoryManager(); ShapeFactory shapeFactory = manager.getRecommendedShapeFactory(); return shapeFactory.createShape(new RectangleShape(this.context), cropped, Constants.LOGO_BACKGROUND_COLOUR, padding); }
private void generarQR(String dato, ImageView objeto) throws WriterException { Writer generador = new QRCodeWriter(); String datoFinal = Uri.encode(dato, getString(R.string.fragment_detalle_promocion_utf8)); BitMatrix bm = generador.encode(datoFinal, BarcodeFormat.QR_CODE, 150, 150); imagenBitMap = Bitmap.createBitmap(150, 150, Bitmap.Config.ARGB_8888); for (int i = 0; i < 150; i++) { //width for (int j = 0; j < 150; j++) { //height imagenBitMap.setPixel(i, j, bm.get(i, j) ? Color.BLACK : Color.WHITE); } } if (imagenBitMap != null) { objeto.setImageBitmap(imagenBitMap); } }
private void generarQR(String dato, ImageView objeto) throws WriterException { Writer generador = new QRCodeWriter(); String datoFinal = Uri.encode(dato, getString(R.string.fragment_idguanajoven_utf8)); BitMatrix bm = generador.encode(datoFinal, BarcodeFormat.QR_CODE, 150, 150); imagenBitMap = Bitmap.createBitmap(150, 150, Bitmap.Config.ARGB_8888); for (int i = 0; i < 150; i++) { //width for (int j = 0; j < 150; j++) { //height imagenBitMap.setPixel(i, j, bm.get(i, j) ? Color.BLACK : Color.WHITE); } } if (imagenBitMap != null) { objeto.setImageBitmap(imagenBitMap); } }
public static Image createQR(String content, int size) { WritableImage img = new WritableImage(size, size); PixelWriter pw = img.getPixelWriter(); try { Writer writer = new QRCodeWriter(); BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, size, size); for (int i = 0; i < matrix.getHeight(); i++) { for (int j = 0; j < matrix.getWidth(); j++) { if (matrix.get(i, j)) { pw.setColor(i, j, Color.BLACK); } } } } catch (WriterException ex) { Logger.getLogger(QRGenerator.class.getName()).log(Level.SEVERE, null, ex); } return img; }
@Override @SuppressWarnings("unchecked") public BufferedImage generateQRCode(String value, int w, int h){ Writer writer = new MultiFormatWriter(); @SuppressWarnings("rawtypes") Hashtable hints = new Hashtable(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-1"); BitMatrix matrix; try { matrix = writer.encode(value, BarcodeFormat.QR_CODE, w, h,hints); } catch (Exception e) { throw new RuntimeException("Erro ao tentar criar bitMatrix para qrcode!",e); } BufferedImage buffImage = MatrixToImageWriter.toBufferedImage(matrix); return buffImage; }
/** * @param args * @throws WriterException * @throws IOException * @throws FileNotFoundException */ public static void main(String[] args) throws WriterException, FileNotFoundException, IOException { Writer writer = new MultiFormatWriter(); Hashtable hints = new Hashtable(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-1"); long now = System.currentTimeMillis(); //String contents = gson.toJson(docPK); String contents = "12345-TESTE123"; log.debug(contents.length()+""); BitMatrix matrix = writer.encode( contents , BarcodeFormat.QR_CODE, 350, 350,hints); BufferedImage buffImage = MatrixToImageWriter.toBufferedImage(matrix); ImageIO.write(buffImage, IMG_EXT, new File("qrCodePerfeito.png")); }
@Override @SuppressWarnings("unchecked") public BufferedImage generateQRCode(String value, int w, int h){ Writer writer = new MultiFormatWriter(); @SuppressWarnings("rawtypes") Hashtable hints = new Hashtable(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-1"); BitMatrix matrix; try { matrix = writer.encode(value, BarcodeFormat.QR_CODE, w, h,hints); } catch (Exception e) { throw new RuntimeException("Erro ao tentar criar bitMatrix para qrcode!",e); } BufferedImage buffImage = MatrixToImageWriter.toBufferedImage(matrix); return buffImage; }
private static void compareToGoldenFile(String contents, ErrorCorrectionLevel ecLevel, int resolution, String fileName) throws WriterException, IOException { BufferedImage image = loadImage(fileName); assertNotNull(image); BitMatrix goldenResult = createMatrixFromImage(image); assertNotNull(goldenResult); Map<EncodeHintType,Object> hints = new EnumMap<>(EncodeHintType.class); hints.put(EncodeHintType.ERROR_CORRECTION, ecLevel); Writer writer = new QRCodeWriter(); BitMatrix generatedResult = writer.encode(contents, BarcodeFormat.QR_CODE, resolution, resolution, hints); assertEquals(resolution, generatedResult.getWidth()); assertEquals(resolution, generatedResult.getHeight()); assertEquals(goldenResult, generatedResult); }
private void doWrite(OutputStream os, Path path) throws IOException { try { Writer writer = new MultiFormatWriter(); Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name()); hints.put(EncodeHintType.MARGIN, Integer.valueOf(margin)); hints.put(EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.forBits(errorCorrectionLevel.getBits())); BitMatrix matrix = writer.encode(uri.toUriString(), BarcodeFormat.QR_CODE, width, height, hints); if (os != null) { MatrixToImageWriter.writeToStream(matrix, imageFormatName, os); } else { MatrixToImageWriter.writeToPath(matrix, imageFormatName, path); } } catch (WriterException e) { throw new IOException(e); } }
@Override public Response getQRCode(final String url) throws IOException { final ByteArrayOutputStream buf = new ByteArrayOutputStream(); final Writer writer = new QRCodeWriter(); final BitMatrix matrix; try { matrix = writer.encode(url, BarcodeFormat.QR_CODE, 100, 100); } catch (WriterException e) { throw new ClapException(e); } MatrixToImageWriter.writeToStream(matrix, "PNG", buf); final byte[] bytes = buf.toByteArray(); LOGGER.debug("qrcode for " + url + " generated"); return returnImage(new ByteArrayInputStream(bytes), "qrcode.png"); }
public BufferedImage getQRCode(String strQRCode) { BitMatrix bitMatrix; Writer writer = new QRCodeWriter(); try { // Write QR Code bitMatrix = writer.encode(strQRCode, BarcodeFormat.QR_CODE,800,800); MatrixToImageWriter.toBufferedImage(bitMatrix); // System.out.println("QR Code Generated."); return MatrixToImageWriter.toBufferedImage(bitMatrix); } catch (Exception e) { System.out.println("Exception Found." + e.getMessage()); } return null; }
@GetMapping public ResponseEntity<byte[]> getBarcode( // @RequestParam(name = CONTENT) final String content // , @RequestParam(name = FORMAT) final String formatStr// , @RequestParam(name = WIDTH, required = false) final int width// , @RequestParam(name = HEIGHT, required = false) final int height// , @RequestParam(name = ECL, required = false) final String eclStr// ) { // // Get parameters and check for wrong values final BarcodeFormat format = BarcodeFormat.valueOf(formatStr); final Map<EncodeHintType, ErrorCorrectionLevel> hints = extractHints(eclStr, format); // // Encode given content into given format final BitMatrix matrix; try { final Writer encoder = new MultiFormatWriter(); matrix = encoder.encode(content, format, width, height, hints); } catch (final WriterException ex) { throw new AdempiereException("Failed creating barcode", ex); } final String barcodeFilename = "barcode.png"; final byte[] barcodeData = toByteArray(matrix, "png"); final HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_PNG); headers.set(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + barcodeFilename + "\""); headers.setCacheControl("must-revalidate, post-check=0, pre-check=0"); final ResponseEntity<byte[]> response = new ResponseEntity<>(barcodeData, headers, HttpStatus.OK); return response; }
@Test public void testQRCodeWriter() throws WriterException { // The QR should be multiplied up to fit, with extra padding if necessary int bigEnough = 256; Writer writer = new QRCodeWriter(); BitMatrix matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, bigEnough, bigEnough, null); assertNotNull(matrix); assertEquals(bigEnough, matrix.getWidth()); assertEquals(bigEnough, matrix.getHeight()); // The QR will not fit in this size, so the matrix should come back bigger int tooSmall = 20; matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, tooSmall, tooSmall, null); assertNotNull(matrix); assertTrue(tooSmall < matrix.getWidth()); assertTrue(tooSmall < matrix.getHeight()); // We should also be able to handle non-square requests by padding them int strangeWidth = 500; int strangeHeight = 100; matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, strangeWidth, strangeHeight, null); assertNotNull(matrix); assertEquals(strangeWidth, matrix.getWidth()); assertEquals(strangeHeight, matrix.getHeight()); }
public QRImage(String text, int width, int height) { super(width, height, BufferedImage.TYPE_INT_RGB); // get a byte matrix for the data BitMatrix matrix = null; com.google.zxing.Writer writer = new QRCodeWriter(); try { matrix = writer.encode(text, com.google.zxing.BarcodeFormat.QR_CODE, width, height); } catch (com.google.zxing.WriterException e) { e.printStackTrace(); } //System.out.println("Current width, height: " + width + " " + height); // generate an image from the byte matrix //width = matrix.getWidth(); //height = matrix.getHeight(); //System.out.println("Matrix width, height: " + width + " " + height); //byte[][] array = matrix.getArray(); // create buffered image to draw to //BufferedImage image = new BufferedImage(width, height, // BufferedImage.TYPE_INT_RGB); // iterate through the matrix and draw the pixels to the image for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { boolean grayValue = matrix.get(x, y);//array[y][x] & 0xff; this.setRGB(x, y, (grayValue ? 0 : 0xFFFFFF)); } } }
/** * @param args * @throws WriterException * @throws IOException * @throws FileNotFoundException */ public static void main(String[] args) throws WriterException, FileNotFoundException, IOException { Writer writer = new MultiFormatWriter(); Hashtable hints = new Hashtable(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-1"); long now = System.currentTimeMillis(); //String contents = gson.toJson(docPK); String contents = "12345-TESTE123"; log.debug(contents.length()+""); BitMatrix matrix = writer.encode( contents , BarcodeFormat.QR_CODE, 350, 350,hints); BufferedImage buffImage = MatrixToImageWriter.toBufferedImage(matrix); ImageIO.write(buffImage, JazzOMRImageParser.IMG_EXT, new File("qrCodePerfeito.png")); }