/** * 同步创建指定前景色、指定背景色、带logo的二维码图片。该方法是耗时操作,请在子线程中调用。 * * @param content 要生成的二维码图片内容 * @param size 图片宽高,单位为px * @param foregroundColor 二维码图片的前景色 * @param backgroundColor 二维码图片的背景色 * @param logo 二维码图片的logo */ public static Bitmap syncEncodeQRCode(String content, int size, int foregroundColor, int backgroundColor, Bitmap logo) { try { BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, HINTS); int[] pixels = new int[size * size]; for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (matrix.get(x, y)) { pixels[y * size + x] = foregroundColor; } else { pixels[y * size + x] = backgroundColor; } } } Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, size, 0, 0, size, size); return addLogoToQRCode(bitmap, logo); } catch (Exception e) { return null; } }
/** * 传入字符串生成二维码 * * @param str * @return * @throws WriterException */ public static Bitmap Create2DCode(String str) throws WriterException { // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败 BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 300, 300); 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] = 0xff000000; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // 通过像素数组生成bitmap,具体参考api bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
public static @NonNull Bitmap create(String data) { try { BitMatrix result = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, 512, 512); Bitmap bitmap = Bitmap.createBitmap(result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888); for (int y = 0; y < result.getHeight(); y++) { for (int x = 0; x < result.getWidth(); x++) { if (result.get(x, y)) { bitmap.setPixel(x, y, Color.BLACK); } } } return bitmap; } catch (WriterException e) { Log.w(TAG, e); return Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888); } }
public Bitmap encodeAsBitmap() throws WriterException { if (!encoded) return null; Map<EncodeHintType, Object> hints = null; String encoding = guessAppropriateEncoding(contents); hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); if (encoding != null) { hints.put(EncodeHintType.CHARACTER_SET, encoding); } hints.put(EncodeHintType.MARGIN, 2); /* default = 4 */ MultiFormatWriter writer = new MultiFormatWriter(); BitMatrix result = writer.encode(contents, format, dimension, dimension, hints); int width = result.getWidth(); int height = result.getHeight(); int[] pixels = new int[width * height]; // 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; }
/** * 生成二维码 * * @param content 二维码内容 * @param size 二维码内容大小 * @param color 二维码颜色 */ public static Bitmap createQRCode(String content, int size,int bgColor, int color) { try { Hashtable<EncodeHintType, String> hints = new Hashtable<>(); hints.put(EncodeHintType.CHARACTER_SET, CHARACTER_SET); BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints); int[] pixels = new int[size * size]; for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (bitMatrix.get(x, y)) { pixels[y * size + x] = color; } else { pixels[y * size + x] = bgColor; } } } Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, size, 0, 0, size, size); return bitmap; } catch (WriterException e) { return null; } }
/** * 生成条形二维码 */ public static byte[] 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); byte[] buf = null; try { BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.CODE_128, codeWidth, height, null); BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(image, FORMAT_NAME, out); out.close(); buf = out.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return buf; }
public static Bitmap createQRCode(String content, int widthAndHeight) { if (TextUtils.isEmpty(content)) return null; try { Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //Fault tolerance level. MAX hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //Set the width of the blank margin. hints.put(EncodeHintType.MARGIN, 0); BitMatrix matrix = new MultiFormatWriter().encode(content, 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] = 0xff000000; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } catch (WriterException e) { e.printStackTrace(); } return null; }
/** * Generate a two-dimensional code image * * @param content An incoming string, usually a URL * @param widthAndHeight widthAndHeight * @param openErrorCorrectionLevel Does fault tolerance open? * @return A two-dimensional code image [Bitmap] */ public static Bitmap createQRCode(String content, int widthAndHeight, boolean openErrorCorrectionLevel) { try { if (TextUtils.isEmpty(content) || TextUtils.equals("null", content) || "".equals(content)) { return null; } //Processing Chinese characters, if you do not need to change the source code, convert the string into ISO-8859-1 code. Map hints = openErrorCorrectionLevel(openErrorCorrectionLevel); BitMatrix matrix = new MultiFormatWriter().encode(new String(content.getBytes("UTF-8"), "ISO-8859-1"), BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, hints); matrix = updateBit(matrix, 8); Bitmap bitmap = generateQRBitmap(matrix); return bitmap; } catch (Exception e) { e.printStackTrace(); } return null; }
private Bitmap endcode(String input) { BarcodeFormat format = BarcodeFormat.QR_CODE; EnumMap<EncodeHintType, Object> hint = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); hint.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix result = null; try { result = new MultiFormatWriter().encode(input, BarcodeFormat.QR_CODE, 500, 500, hint); } catch (WriterException e) { e.printStackTrace(); } int w = result.getWidth(); int h = result.getHeight(); int[] pixels = new int[w * h]; for (int y = 0; y < h; y++) { int offset = y * w; for (int x = 0; x < w; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; } } Bitmap bit = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); bit.setPixels(pixels, 0, w, 0, 0, w, h); ImageView imageView = (ImageView) findViewById(R.id.qr_code_id); imageView.setImageBitmap(bit); return bit; }
@Nullable static Bitmap createQrCode(DisplayMetrics dm, String input) { int smallestDimen = Math.min(dm.widthPixels, dm.heightPixels); try { // Generate QR code final BitMatrix encoded = new QRCodeWriter().encode( input, QR_CODE, smallestDimen, smallestDimen); // Convert QR code to Bitmap int width = encoded.getWidth(); int height = encoded.getHeight(); int[] pixels = new int[width * height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { pixels[y * width + x] = encoded.get(x, y) ? BLACK : WHITE; } } Bitmap qr = Bitmap.createBitmap(width, height, ARGB_8888); qr.setPixels(pixels, 0, width, 0, 0, width, height); return qr; } catch (WriterException e) { if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); return null; } }
private static float moduleSize(int[] leftTopBlack, BitMatrix image) throws NotFoundException { int height = image.getHeight(); int width = image.getWidth(); int x = leftTopBlack[0]; int y = leftTopBlack[1]; boolean inBlack = true; int transitions = 0; while (x < width && y < height) { if (inBlack != image.get(x, y)) { if (++transitions == 5) { break; } inBlack = !inBlack; } x++; y++; } if (x == width || y == height) { throw NotFoundException.getNotFoundInstance(); } return (x - leftTopBlack[0]) / 7.0f; }
private static void drawBullsEye(BitMatrix matrix, int center, int size) { for (int i = 0; i < size; i += 2) { for (int j = center - i; j <= center + i; j++) { matrix.set(j, center - i); matrix.set(j, center + i); matrix.set(center - i, j); matrix.set(center + i, j); } } matrix.set(center - size, center - size); matrix.set(center - size + 1, center - size); matrix.set(center - size, center - size + 1); matrix.set(center + size, center - size); matrix.set(center + size, center - size + 1); matrix.set(center + size, center + size - 1); }
@Override public Result decode(BinaryBitmap image, Map<DecodeHintType,?> hints) throws NotFoundException, ChecksumException, FormatException { DecoderResult decoderResult; if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) { BitMatrix bits = extractPureBits(image.getBlackMatrix()); decoderResult = decoder.decode(bits, hints); } else { throw NotFoundException.getNotFoundInstance(); } ResultPoint[] points = NO_POINTS; Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.MAXICODE); String ecLevel = decoderResult.getECLevel(); if (ecLevel != null) { result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, ecLevel); } return result; }
/** * Convert the ByteMatrix to BitMatrix. * * @param matrix The input matrix. * @return The output matrix. */ private static BitMatrix convertByteMatrixToBitMatrix(ByteMatrix matrix) { int matrixWidgth = matrix.getWidth(); int matrixHeight = matrix.getHeight(); BitMatrix output = new BitMatrix(matrixWidgth, matrixHeight); output.clear(); for (int i = 0; i < matrixWidgth; i++) { for (int j = 0; j < matrixHeight; j++) { // Zero is white in the bytematrix if (matrix.get(i, j) == 1) { output.set(i, j); } } } return output; }
public static Bitmap createQRCode(String str, int size) 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, size, size); int width = matrix.getWidth(); int height = matrix.getHeight(); int[] pixels = new int[width * height]; for(int x = 0; x < width; x ++){ for(int y = 0; y < height; y ++){ if(matrix.get(x, y)){ pixels[y * width + x] = BLACK; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
/** * <p>Creates a finder that will look in a portion of the whole image.</p> * * @param image image to search * @param startX left column from which to start searching * @param startY top row from which to start searching * @param width width of region to search * @param height height of region to search * @param moduleSize estimated module size so far */ AlignmentPatternFinder(BitMatrix image, int startX, int startY, int width, int height, float moduleSize, ResultPointCallback resultPointCallback) { this.image = image; this.possibleCenters = new ArrayList<>(5); this.startX = startX; this.startY = startY; this.width = width; this.height = height; this.moduleSize = moduleSize; this.crossCheckStateCount = new int[3]; this.resultPointCallback = resultPointCallback; }
/** * @param bitMatrix {@link BitMatrix} to parse * @throws FormatException if dimension is < 8 or > 144 or not 0 mod 2 */ BitMatrixParser(BitMatrix bitMatrix) throws FormatException { int dimension = bitMatrix.getHeight(); if (dimension < 8 || dimension > 144 || (dimension & 0x01) != 0) { throw FormatException.getFormatInstance(); } version = readVersion(bitMatrix); this.mappingBitMatrix = extractDataRegion(bitMatrix); this.readMappingMatrix = new BitMatrix(this.mappingBitMatrix.getWidth(), this.mappingBitMatrix.getHeight()); }
@Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) throws WriterException { if (format != BarcodeFormat.CODE_39) { throw new IllegalArgumentException("Can only encode CODE_39, but got " + format); } return super.encode(contents, format, width, height, hints); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_share); QRImage = (ImageView)findViewById(R.id.QRCode); buttonGenerate = (Button)findViewById(R.id.button_generate); buttonGenerate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { QRCodeWriter writer = new QRCodeWriter(); hackmelockDevice = dbHelper.getConfig(); String toEncode = utils.majorMinorToHexString(hackmelockDevice.Major, hackmelockDevice.Minor) + ":" + utils.bytesToHex(hackmelockDevice.keys[KeyID]) + ":" + KeyID +":" + System.currentTimeMillis()/1000 + ":" + DateTo; Log.d("Share","Text to encode: " + toEncode); //contains sensitive information (key) try { BitMatrix bitMatrix = writer.encode(toEncode, BarcodeFormat.QR_CODE, 256, 256); 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); } } QRImage.setImageBitmap(bmp); QRImage.setVisibility(View.VISIBLE); buttonGenerate.setVisibility(View.VISIBLE); } catch (WriterException e) { e.printStackTrace(); } } }); }
public DecoderResult decode(boolean[][] image, Map<DecodeHintType, ?> hints) throws ChecksumException, FormatException { int dimension = image.length; BitMatrix bits = new BitMatrix(dimension); for (int i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { if (image[i][j]) { bits.set(j, i); } } } return decode(bits, (Map) hints); }
private static void createQRImage(File qrFile, String qrCodeText, int size, String fileType) throws WriterException, IOException { // Create the ByteMatrix for the QR-Code that encodes the given String Hashtable hintMap = new Hashtable(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hintMap); // Make the BufferedImage that are to hold the QRCode int matrixWidth = byteMatrix.getWidth(); BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB); image.createGraphics(); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, matrixWidth, matrixWidth); // Paint and save the image using the ByteMatrix graphics.setColor(Color.BLACK); for (int i = 0; i < matrixWidth; i++) { for (int j = 0; j < matrixWidth; j++) { if (byteMatrix.get(i, j)) { graphics.fillRect(i, j, 1, 1); } } } ImageIO.write(image, fileType, qrFile); }
/** * <p>Convenience method that can decode a QR Code represented as a 2D array of booleans. * "true" is taken to mean a black module.</p> * * @param image booleans representing white/black QR Code modules * @param hints decoding hints that should be used to influence decoding * @return text and bytes encoded within the QR Code * @throws FormatException if the QR Code cannot be decoded * @throws ChecksumException if error correction fails */ public DecoderResult decode(boolean[][] image, Map<DecodeHintType,?> hints) throws ChecksumException, FormatException { int dimension = image.length; BitMatrix bits = new BitMatrix(dimension); for (int i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { if (image[i][j]) { bits.set(j, i); } } } return decode(bits, hints); }
public static void createQRCode(String qrCodeData, String filePath, String charset, Map hintMap, int qrCodeheight, int qrCodewidth) throws WriterException, IOException { BitMatrix matrix = new MultiFormatWriter().encode( new String(qrCodeData.getBytes(charset), charset), BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintMap); Object object = new Object(); MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath .lastIndexOf('.') + 1), new File(filePath)); }
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException { if (format == BarcodeFormat.EAN_8) { return super.encode(contents, format, width, height, hints); } throw new IllegalArgumentException("Can only encode EAN_8, but got " + format); }
public static Bitmap createQrCode(String contents, int desiredWidth, int desiredHeight) { Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.MARGIN, 2); MultiFormatWriter writer = new MultiFormatWriter(); BitMatrix result = null; try { result = writer.encode(contents, BarcodeFormat.QR_CODE, desiredWidth, desiredHeight, hints); } catch (WriterException e) { } int width = result.getWidth(); int height = result.getHeight(); int[] pixels = new int[width * height]; // 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) ? 0xFF000000 : 0xFFFFFFFF; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565); // Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); System.gc(); return bitmap; }
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, stream)) { throw new IOException("Could not write an image of format " + format); } }
@Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException { if (format != BarcodeFormat.UPC_E) { throw new IllegalArgumentException("Can only encode UPC_E, but got " + format); } return super.encode(contents, format, width, height, hints); }
/** * Generate a two-dimensional code image * * @param content content * @return A two-dimensional code image */ public static Bitmap createQRCode(String content) { try { if (TextUtils.isEmpty(content) || TextUtils.equals("null", content) || "".equals(content)) { return null; } BitMatrix matrix = new MultiFormatWriter().encode(new String(content.getBytes("UTF-8"), "iso-8859-1"), BarcodeFormat.QR_CODE, 200, 200); Bitmap bitmap = generateQRBitmap(matrix); return bitmap; } catch (Exception e) { e.printStackTrace(); } return null; }
public static BufferedImage createQRCode(String qrCodeData, String charset, Map hintMap, int qrCodeheight, int qrCodewidth) throws WriterException, IOException { //Create the BitMatrix representing the QR code BitMatrix matrix = new MultiFormatWriter().encode( new String(qrCodeData.getBytes(charset), charset), BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintMap); //Create BufferedImages from the QRCode BitMatricies return MatrixToImageWriter.toBufferedImage(matrix); }
public AztecDetectorResult(BitMatrix bits, ResultPoint[] points, boolean compact, int nbDatablocks, int nbLayers) { super(bits, points); this.compact = compact; this.nbDatablocks = nbDatablocks; this.nbLayers = nbLayers; }
static public Bitmap generateBitmap(@NonNull String contentsToEncode, int imageWidth, int imageHeight, int marginSize) throws WriterException { Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); if (marginSize != MARGIN_AUTOMATIC) { // We want to generate with a custom margin size hints.put(EncodeHintType.MARGIN, marginSize); } MultiFormatWriter writer = new MultiFormatWriter(); BitMatrix result = writer .encode(contentsToEncode, BarcodeFormat.QR_CODE, imageWidth, imageHeight, hints); final int width = result.getWidth(); final int height = result.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] = result.get(x, y) ? colorBack : colorWhite; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
public DecoderResult decode(AztecDetectorResult detectorResult) throws FormatException { ddata = detectorResult; BitMatrix matrix = detectorResult.getBits(); boolean[] rawbits = extractBits(matrix); boolean[] correctedBits = correctBits(rawbits); String result = getEncodedData(correctedBits); return new DecoderResult(null, result, null, null); }
/** * 创建二维码 * * @param content content * @param widthPix widthPix * @param heightPix heightPix * @param logoBm logoBm * @return 二维码 */ public static Bitmap createQRCode(String content, int widthPix, int heightPix, Bitmap logoBm) { try { if (content == null || "".equals(content)) { return null; } // 配置参数 Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 容错级别 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 图像数据转换,使用了矩阵转换 BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix, heightPix, hints); int[] pixels = new int[widthPix * heightPix]; // 下面这里按照二维码的算法,逐个生成二维码的图片, // 两个for循环是图片横列扫描的结果 for (int y = 0; y < heightPix; y++) { for (int x = 0; x < widthPix; x++) { if (bitMatrix.get(x, y)) { pixels[y * widthPix + x] = 0xff000000; } else { pixels[y * widthPix + x] = 0xffffffff; } } } // 生成二维码图片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix); if (logoBm != null) { bitmap = addLogo(bitmap, logoBm); } //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大! return bitmap; } catch (WriterException e) { e.printStackTrace(); } return null; }
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) { ByteMatrix input = code.getMatrix(); if (input == null) { throw new IllegalStateException(); } int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); int qrWidth = inputWidth + (quietZone * 2); int qrHeight = inputHeight + (quietZone * 2); int outputWidth = Math.max(width, qrWidth); int outputHeight = Math.max(height, qrHeight); int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight); int leftPadding = (outputWidth - (inputWidth * multiple)) / 2; int topPadding = (outputHeight - (inputHeight * multiple)) / 2; BitMatrix output = new BitMatrix(outputWidth, outputHeight); int inputY = 0; int outputY = topPadding; while (inputY < inputHeight) { int inputX = 0; int outputX = leftPadding; while (inputX < inputWidth) { if (input.get(inputX, inputY) == (byte) 1) { output.setRegion(outputX, outputY, multiple, multiple); } inputX++; outputX += multiple; } inputY++; outputY += multiple; } return output; }
private static Codeword detectCodeword(BitMatrix image, int minColumn, int maxColumn, boolean leftToRight, int startColumn, int imageRow, int minCodewordWidth, int maxCodewordWidth) { startColumn = adjustCodewordStartColumn(image, minColumn, maxColumn, leftToRight, startColumn, imageRow); int[] moduleBitCount = getModuleBitCount(image, minColumn, maxColumn, leftToRight, startColumn, imageRow); if (moduleBitCount == null) { return null; } int endColumn; int codewordBitCount = PDF417Common.getBitCountSum(moduleBitCount); if (leftToRight) { endColumn = startColumn + codewordBitCount; } else { for (int i = 0; i < moduleBitCount.length / 2; i++) { int tmpCount = moduleBitCount[i]; moduleBitCount[i] = moduleBitCount[(moduleBitCount.length - 1) - i]; moduleBitCount[(moduleBitCount.length - 1) - i] = tmpCount; } endColumn = startColumn; startColumn = endColumn - codewordBitCount; } if (!checkCodewordSkew(codewordBitCount, minCodewordWidth, maxCodewordWidth)) { return null; } int decodedValue = PDF417CodewordDecoder.getDecodedValue(moduleBitCount); int codeword = PDF417Common.getCodeword(decodedValue); if (codeword == -1) { return null; } return new Codeword(startColumn, endColumn, getCodewordBucketNumber(decodedValue), codeword); }
public static void qrCode(int width,int height,String content,String suffix,String imgPath){ Hashtable<EncodeHintType, String> hints= new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix; try { bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints); bitMatrix = deleteWhite(bitMatrix); File outputFile = new File(imgPath); MatrixToImageWriter.writeToFile(bitMatrix, suffix, outputFile); } catch (Exception e) { e.printStackTrace(); } }