public Bitmap createBitmap(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = matrix.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; }
/** * Given an image source, convert to a binary bitmap. * * Override this to use a custom binarizer. * * @param source the image source * @return a BinaryBitmap */ protected BinaryBitmap toBitmap(LuminanceSource source) { return new BinaryBitmap(new HybridBinarizer(source)); }
/** * BitMatrix转BufferedImage * * @param matrix BitMatrix * @param foreColor 前景色 * @param backColor 背景色 * @return BufferedImage * @since 4.1.2 */ public static BufferedImage toImage(BitMatrix matrix, int foreColor, int backColor) { final int width = matrix.getWidth(); final 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) ? foreColor : backColor); } } return image; } }
/** * Given an image source, convert to a binary bitmap. * * Override this to use a custom binarizer. * * @param source the image source * @return a BinaryBitmap */ protected BinaryBitmap toBitmap(LuminanceSource source) { return new BinaryBitmap(new HybridBinarizer(source.invert())); } }
/** * BitMatrix转BufferedImage * * @param matrix BitMatrix * @param foreColor 前景色 * @param backColor 背景色 * @return BufferedImage * @since 4.1.2 */ public static BufferedImage toImage(BitMatrix matrix, int foreColor, int backColor) { final int width = matrix.getWidth(); final 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) ? foreColor : backColor); } } return image; } }
/** * Given an image source, convert to a binary bitmap. * * Override this to use a custom binarizer. * * @param source the image source * @return a BinaryBitmap */ protected BinaryBitmap toBitmap(LuminanceSource source) { if (isInverted) { isInverted = false; return new BinaryBitmap(new HybridBinarizer(source.invert())); } else { isInverted = true; return new BinaryBitmap(new HybridBinarizer(source)); } }
/** * 将二维码输出为 ASCII * * @param bitMatrix * @return */ private static String toAscii(BitMatrix bitMatrix) { StringBuilder sb = new StringBuilder(); for (int rows = 0; rows < bitMatrix.getHeight(); rows++) { for (int cols = 0; cols < bitMatrix.getWidth(); cols++) { boolean x = bitMatrix.get(rows, cols); if (!x) { // white sb.append("\033[47m \033[0m"); } else { sb.append("\033[30m \033[0;39m"); } } sb.append("\n"); } return sb.toString(); }
/** * 读取二维码信息 * * @param filePath 文件路径 * @param hintMap hintMap * @return 二维码内容 */ private static String readQRCode(File filePath, Map hintMap) { try { BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer( new BufferedImageLuminanceSource(ImageIO.read(new FileInputStream(filePath))))); Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, hintMap); return qrCodeResult.getText(); } catch (Exception e) { e.printStackTrace(); return null; } }
QRCodeWriter writer = new QRCodeWriter(); try { BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, 512, 512); int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); Bitmap 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); } } ((ImageView) findViewById(R.id.img_result_qr)).setImageBitmap(bmp); } catch (WriterException e) { e.printStackTrace(); }
private String decodeQrPng(String encodedQrCode) throws IOException, NotFoundException, ChecksumException, FormatException { byte[] decodedByte = Base64.getDecoder().decode(encodedQrCode); BufferedImage image = ImageIO.read(new ByteArrayInputStream(decodedByte)); BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); Map<DecodeHintType, Object> hintMap = new HashMap<>(); hintMap.put(DecodeHintType.PURE_BARCODE, true); return reader.decode(bitmap, hintMap).getText(); }
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight); int width = matrix.getWidth(); int height = matrix.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (matrix.get(x, y)) { pixels[y * width + x] = BLACK; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } }
private String qrCodeText(String dataUrl) throws Exception { QRCodeReader reader = new QRCodeReader(); String[] rawSplit = dataUrl.split(","); assertEquals("data:image/png;base64", rawSplit[0]); byte[] decodedByte = Base64.getDecoder().decode(rawSplit[1]); BufferedImage image = ImageIO.read(new ByteArrayInputStream(decodedByte)); BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image); Map<DecodeHintType, Object> hintMap = new HashMap<>(); hintMap.put(DecodeHintType.PURE_BARCODE, true); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); return reader.decode(bitmap, hintMap).getText(); }
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight); int width = matrix.getWidth(); int height = matrix.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (matrix.get(x, y)) { pixels[y * width + x] = BLACK; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } }
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight); int width = matrix.getWidth(); int height = matrix.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (matrix.get(x, y)) { pixels[y * width + x] = BLACK; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } }
/** * As {@link #toBufferedImage(BitMatrix)}, but allows customization of the output. * * @param matrix {@link BitMatrix} to write * @param config output configuration * @return {@link BufferedImage} representation of the input */ public static BufferedImage toBufferedImage(BitMatrix matrix, MatrixToImageConfig config) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, config.getBufferedImageColorModel()); int onColor = config.getPixelOnColor(); int offColor = config.getPixelOffColor(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? onColor : offColor); } } return image; }
/** * As {@link #toBitmap(BitMatrix)}, but allows customization of the output. * * @param matrix {@link BitMatrix} to write * @param config output configuration * @return {@link Bitmap} representation of the input */ public static Bitmap toBitmap(BitMatrix matrix, MatrixToImageConfig config) { final int onColor = config.getPixelOnColor(); final int offColor = config.getPixelOffColor(); final int width = matrix.getWidth(); final int height = matrix.getHeight(); final int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = matrix.get(x, y) ? onColor : offColor; } } Bitmap image = Bitmap.createBitmap(width, height, config.getBufferedImageColorModel()); image.setPixels(pixels, 0, width, 0, 0, width, height); return image; }
public static Bitmap generateQRCode(final String url) { Bitmap qrCode = null; Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class); hints.put(EncodeHintType.MARGIN, 2); try { BitMatrix matrix = new MultiFormatWriter() .encode(url, BarcodeFormat.QR_CODE, 200, 200, hints); int h = matrix.getHeight(); int w = matrix.getWidth(); int[] pixels = new int[h * w]; for (int i = 0; i < h; i++) { int offset = i * w; for (int j = 0; j < w; j++) { pixels[offset + j] = matrix.get(j, i) ? Color.BLACK : Color.WHITE; } } qrCode = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); qrCode.setPixels(pixels, 0, w, 0, 0, w, h); } catch (WriterException ignored) { // ignored because exception would be thrown from ZXing library. } return qrCode; }
private static SVGGraphics2D toSvgDocument(BitMatrix matrix, MatrixToImageConfig config) { int width = matrix.getWidth(); int height = matrix.getHeight(); DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); String svgNS = "http://www.w3.org/2000/svg"; Document document = domImpl.createDocument(svgNS, "svg", null); SVGGraphics2D svgGraphics = new SVGGraphics2D(document); svgGraphics.setColor(new Color(config.getPixelOffColor())); svgGraphics.fillRect(0,0, width, height); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { if (matrix.get(x,y)) { svgGraphics.setColor(new Color(config.getPixelOnColor())); svgGraphics.fillRect(x, y, 1, 1); } } } return svgGraphics; }