protected Bitmap createQrCodeBitmap(String input, int size) { Log.d(Config.LOGTAG, "qr code requested size: " + size); try { final QRCodeWriter QR_CODE_WRITER = new QRCodeWriter(); final Hashtable<EncodeHintType, Object> hints = new Hashtable<>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); final BitMatrix result = QR_CODE_WRITER.encode(input, BarcodeFormat.QR_CODE, size, size, hints); final int width = result.getWidth(); final int height = result.getHeight(); final int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { final int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT; } } final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Log.d(Config.LOGTAG, "output size: " + width + "x" + height); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } catch (final WriterException e) { return null; } }
/* * 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)); }
public BufferedImage getQRCode(String codeText, Integer size) { try { Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<>(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); QRCodeWriter qrCodeWriter = new QRCodeWriter(); byteMatrix = qrCodeWriter.encode(codeText, BarcodeFormat.QR_CODE, size, size, hintMap); int imageWidth = byteMatrix.getWidth(); image = new BufferedImage(imageWidth, imageWidth, BufferedImage.TYPE_INT_RGB); image.createGraphics(); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, imageWidth, imageWidth); graphics.setColor(Color.BLACK); for (int i = 0; i < imageWidth; i++) { for (int j = 0; j < imageWidth; j++) { if (byteMatrix.get(i, j)) { graphics.fillRect(i, j, 1, 1); } } } return image; } catch (WriterException e) { return null; } }
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); }
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); }
public static Bitmap generate(String data) throws WriterException { Bitmap bmp; QRCodeWriter writer = new QRCodeWriter(); BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.QR_CODE, 512, 512); int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE); } } return bmp; }
private Bitmap createBitmap(String content, final int size) { final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.MARGIN, 0); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); BitMatrix result; try { result = sQRCodeWriter.encode(content, BarcodeFormat.QR_CODE, size, size, hints); } catch (WriterException ex) { mLogger.warn("qr encoder failed: " + ex.toString()); return null; } final int width = result.getWidth(); final int height = result.getHeight(); final int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { final int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT; } } final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
public static Bitmap bitmap(@Nonnull final String content, final int size) { try { final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.MARGIN, 0); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); final BitMatrix result = QR_CODE_WRITER.encode(content, BarcodeFormat.QR_CODE, size, size, hints); final int width = result.getWidth(); final int height = result.getHeight(); final int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { final int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT; } } final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } catch (final WriterException x) { log.info("problem creating qr code", x); return null; } }
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 int[] generateQR(String text, int dimension) { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix matrix = null; try { matrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, dimension, dimension); } catch (WriterException e) { e.printStackTrace(); } int[] pixels = new int[dimension * dimension]; for (int y = 0; y < dimension; y++) { int offset = y * dimension; for (int x = 0; x < dimension; x++) { if (matrix.get(x, y)) pixels[offset + x] = Color.BLACK; else pixels[offset + x] = Color.WHITE; } } return pixels; }
// 生成QR图 private void createImage() { try { // 需要引入core包 QRCodeWriter writer = new QRCodeWriter(); String text = RQcode; Log.i(TAG, "生成的文本:" + text); if (text == null || "".equals(text) || text.length() < 1) { return; } // 把输入的文本转为二维码 BitMatrix martix = writer.encode(text, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT); System.out.println("w:" + martix.getWidth() + "h:" + martix.getHeight()); Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints); int[] pixels = new int[QR_WIDTH * QR_HEIGHT]; for (int y = 0; y < QR_HEIGHT; y++) { for (int x = 0; x < QR_WIDTH; x++) { if (bitMatrix.get(x, y)) { pixels[y * QR_WIDTH + x] = 0xff000000; } else { pixels[y * QR_WIDTH + x] = 0xffffffff; } } } barcodeBitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888); barcodeBitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT); imgOutput.setImageBitmap(barcodeBitmap); } catch (WriterException e) { e.printStackTrace(); } }
private Bitmap encodeString(String input) throws WriterException { BarcodeFormat QR_CODE = BarcodeFormat.QR_CODE; // byte[] b = input.getBytes(); // //convert the byte array into a UTF-8 string // String data = ""; // try { // data = new String(b, "ISO-8859-1"); // } // catch (UnsupportedEncodingException e) { // //the program shouldn't be able to get here // Log.i("The message", "cannot convert to ISO"); // } Hashtable<EncodeHintType, Object> hints = null; String encoding = input; Log.i("Encoding", encoding); if (encoding != null) { hints = new Hashtable<EncodeHintType, Object>(2); hints.put(EncodeHintType.CHARACTER_SET, encoding); } QRCodeWriter writer = new QRCodeWriter(); BitMatrix result = writer.encode(input, QR_CODE, 300, 300, hints); Log.i("Result", result.toString()); int width = result.getWidth(); int height = result.getHeight(); int[] pixels = new int[width * height]; Log.i("Result", pixels.toString()); // All are 0, or black, by default for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }