com.google.zxing.EncodeHintType Java Examples

The following examples show how to use com.google.zxing.EncodeHintType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EncodingHandler.java    From vmqApk with MIT License 7 votes vote down vote up
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;
}
 
Example #2
Source File: PayUtil.java    From springboot-pay-example with Apache License 2.0 6 votes vote down vote up
/**
 * 根据url生成二位图片对象
 *
 * @param codeUrl
 * @return
 * @throws WriterException
 */
public static BufferedImage getQRCodeImge(String codeUrl) throws WriterException {
    Map<EncodeHintType, Object> hints = new Hashtable();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
    hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
    int width = 256;
    BitMatrix bitMatrix = (new MultiFormatWriter()).encode(codeUrl, BarcodeFormat.QR_CODE, width, width, hints);
    BufferedImage image = new BufferedImage(width, width, 1);
    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < width; ++y) {
            image.setRGB(x, y, bitMatrix.get(x, y) ? -16777216 : -1);
        }
    }

    return image;
}
 
Example #3
Source File: EncodingHandler.java    From Mobike with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #4
Source File: BarcodeTools.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static BufferedImage DataMatrix(String code,
        int dmWidth, int dmHeigh) {
    try {
        HashMap hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        BitMatrix bitMatrix = new MultiFormatWriter().encode(code,
                BarcodeFormat.DATA_MATRIX, dmWidth, dmHeigh, hints);

        return MatrixToImageWriter.toBufferedImage(bitMatrix);

    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}
 
Example #5
Source File: BarcodeUtils.java    From code-scanner with MIT License 6 votes vote down vote up
/**
 * Encode text content
 *
 * @param content Text to be encoded
 * @param format  Result barcode format
 * @param width   Result image width
 * @param height  Result image height
 * @param hints   Encoder hints
 * @return Barcode bit matrix, if it was encoded successfully, {@code null} otherwise
 * @see EncodeHintType
 * @see BitMatrix
 */
@Nullable
public static BitMatrix encodeBitMatrix(@NonNull final String content,
        @NonNull final BarcodeFormat format, final int width, final int height,
        @Nullable final Map<EncodeHintType, ?> hints) {
    Objects.requireNonNull(content);
    Objects.requireNonNull(format);
    final MultiFormatWriter writer = new MultiFormatWriter();
    try {
        if (hints != null) {
            return writer.encode(content, format, width, height, hints);
        } else {
            return writer.encode(content, format, width, height);
        }
    } catch (final WriterException e) {
        return null;
    }
}
 
Example #6
Source File: QRCodeUtilTest.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
private BarcodeParamDTO initBarcodeParam() {
	BarcodeParamDTO paramDTO = new BarcodeParamDTO();
	paramDTO.setWidth(200);
	paramDTO.setHeight(200);
	paramDTO.setFilepath(qrcodeFile);
	paramDTO.setImageFormat("png");
	paramDTO.setBarcodeFormat(BarcodeFormat.QR_CODE);

	// 编码参数
	Map<EncodeHintType, Object> encodeHints = new HashMap<EncodeHintType, Object>();
	encodeHints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
	paramDTO.setEncodeHints(encodeHints);

	// 解码参数
	HashMap<DecodeHintType, Object> decodeHints = new HashMap<DecodeHintType, Object>();
	decodeHints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
	paramDTO.setDecodeHints(decodeHints);

	return paramDTO;
}
 
Example #7
Source File: PDF417Writer.java    From letv with Apache License 2.0 6 votes vote down vote up
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException {
    if (format != BarcodeFormat.PDF_417) {
        throw new IllegalArgumentException("Can only encode PDF_417, but got " + format);
    }
    PDF417 encoder = new PDF417();
    if (hints != null) {
        if (hints.containsKey(EncodeHintType.PDF417_COMPACT)) {
            encoder.setCompact(((Boolean) hints.get(EncodeHintType.PDF417_COMPACT)).booleanValue());
        }
        if (hints.containsKey(EncodeHintType.PDF417_COMPACTION)) {
            encoder.setCompaction((Compaction) hints.get(EncodeHintType.PDF417_COMPACTION));
        }
        if (hints.containsKey(EncodeHintType.PDF417_DIMENSIONS)) {
            Dimensions dimensions = (Dimensions) hints.get(EncodeHintType.PDF417_DIMENSIONS);
            encoder.setDimensions(dimensions.getMaxCols(), dimensions.getMinCols(), dimensions.getMaxRows(), dimensions.getMinRows());
        }
    }
    return bitMatrixFromEncoder(encoder, contents, width, height);
}
 
Example #8
Source File: EncodingHandler.java    From CodeScaner with MIT License 6 votes vote down vote up
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;
}
 
Example #9
Source File: QRCodeWriter.java    From letv with Apache License 2.0 6 votes vote down vote up
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException {
    if (contents.length() == 0) {
        throw new IllegalArgumentException("Found empty contents");
    } else if (format != BarcodeFormat.QR_CODE) {
        throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
    } else if (width < 0 || height < 0) {
        throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height);
    } else {
        ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
        int quietZone = 4;
        if (hints != null) {
            ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION);
            if (requestedECLevel != null) {
                errorCorrectionLevel = requestedECLevel;
            }
            Integer quietZoneInt = (Integer) hints.get(EncodeHintType.MARGIN);
            if (quietZoneInt != null) {
                quietZone = quietZoneInt.intValue();
            }
        }
        return renderResult(Encoder.encode(contents, errorCorrectionLevel, hints), width, height, quietZone);
    }
}
 
Example #10
Source File: OneDimensionalCodeWriter.java    From ScreenCapture with MIT License 6 votes vote down vote up
/**
 * Encode the contents following specified format.
 * {@code width} and {@code height} are required size. This method may return bigger size
 * {@code BitMatrix} when specified size is too small. The user can set both {@code width} and
 * {@code height} to zero to get minimum size barcode. If negative value is set to {@code width}
 * or {@code height}, {@code IllegalArgumentException} is thrown.
 */
@Override
public BitMatrix encode(String contents,
                        BarcodeFormat format,
                        int width,
                        int height,
                        Map<EncodeHintType,?> hints) throws WriterException {
  if (contents.isEmpty()) {
    throw new IllegalArgumentException("Found empty contents");
  }

  if (width < 0 || height < 0) {
    throw new IllegalArgumentException("Negative size is not allowed. Input: "
                                           + width + 'x' + height);
  }

  int sidesMargin = getDefaultMargin();
  if (hints != null && hints.containsKey(EncodeHintType.MARGIN)) {
    sidesMargin = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
  }

  boolean[] code = encode(contents);
  return renderResult(code, width, height, sidesMargin);
}
 
Example #11
Source File: PaymentUtils.java    From java-pay with Apache License 2.0 6 votes vote down vote up
/**
 * 生成二维码并响应到浏览器
 *
 * @param content
 * @param response
 */
public static void createQRCode(String content, HttpServletResponse response) {
    int width = 300, height = 300;
    String format = "png";
    Map<EncodeHintType, Object> hashMap = new HashMap<>();
    hashMap.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8);
    hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
    hashMap.put(EncodeHintType.MARGIN, 1);
    try {
        response.setHeader("Cache-control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("content-type", "image/png");
        response.setCharacterEncoding(StandardCharsets.UTF_8.displayName());
        response.setDateHeader("Expires", 0);
        BitMatrix bitMatrix = new MultiFormatWriter()
                .encode(content, BarcodeFormat.QR_CODE, width, height, hashMap);
        BufferedImage img = MatrixToImageWriter.toBufferedImage(bitMatrix);
        ImageIO.write(img, format, response.getOutputStream());
    } catch (Exception e) {
        log.warn("create QRCode error message:{}", e.getMessage());
    }
}
 
Example #12
Source File: AttestationActivity.java    From Auditor with MIT License 6 votes vote down vote up
private Bitmap createQrCode(final byte[] contents) {
    final BitMatrix result;
    try {
        final QRCodeWriter writer = new QRCodeWriter();
        final Map<EncodeHintType,Object> hints = new EnumMap<>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.ISO_8859_1);
        final int size = Math.min(imageView.getWidth(), imageView.getHeight());
        result = writer.encode(new String(contents, StandardCharsets.ISO_8859_1), BarcodeFormat.QR_CODE,
                size, size, hints);
    } catch (WriterException e) {
        throw new RuntimeException(e);
    }

    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) ? BLACK : WHITE;
        }
    }

    return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.RGB_565);
}
 
Example #13
Source File: EncodingHandler.java    From QrCodeDemo4 with MIT License 6 votes vote down vote up
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;
}
 
Example #14
Source File: ZxingUtils.java    From wish-pay with Apache License 2.0 6 votes vote down vote up
/**
 * 二维码信息写成JPG BufferedImage
 *
 * @param content
 * @param width
 * @param height
 * @return
 */
public static BufferedImage writeInfoToJpgBuffImg(String content, int width, int height) {
    if (width < 250) {
        width = 250;
    }
    if (height < 250) {
        height = 250;
    }
    BufferedImage re = null;

    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    Map hints = new HashMap();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    try {
        BitMatrix bitMatrix = multiFormatWriter.encode(content,
                BarcodeFormat.QR_CODE, width, height, hints);
        re = MatrixToImageWriter.toBufferedImage(bitMatrix);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return re;
}
 
Example #15
Source File: ZxingUtils.java    From wish-pay with Apache License 2.0 6 votes vote down vote up
/**
 * 将内容contents生成长为width,宽为width的图片,返回刘文静
 */
public static ServletOutputStream getQRCodeImge(String contents, int width, int height) throws IOException {
    ServletOutputStream stream = null;
    try {
        Map<EncodeHintType, Object> hints = Maps.newHashMap();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
        MatrixToImageWriter.writeToStream(bitMatrix, "png", stream);
        return stream;
    } catch (Exception e) {
        log.error("create QR code error!", e);
        return null;
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}
 
Example #16
Source File: BarCodeGenerator.java    From rebuild with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param content
 * @param format
 * @param height
 * @return
 */
public static BitMatrix createBarCode(String content, BarcodeFormat format, int height) {
    Map<EncodeHintType, Object> hints = new HashMap<>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    hints.put(EncodeHintType.MARGIN, 0);

    // 条形码宽度为自适应
    int width = format == BarcodeFormat.QR_CODE ? height : 0;
    try {
        return new MultiFormatWriter().encode(content, format, width, height, hints);

    } catch (WriterException ex) {
        throw new RebuildException("Encode BarCode failed : " + content, ex);
    }
}
 
Example #17
Source File: QRCodeHelper.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
public static Bitmap generateBitmap(String content, int width, int height) {
    int margin = 5;  //自定义白边边框宽度
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    Hashtable hints = new Hashtable<>();
    hints.put(EncodeHintType.MARGIN, 0);
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    try {
        BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
      //  encode = updateBit(encode, margin);  //生成新的bitMatrix
        int[] pixels = new int[width * height];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (encode.get(j, i)) {
                    pixels[i * width + j] = 0x00000000;
                } else {
                    pixels[i * width + j] = 0xffffffff;
                }
            }
        }
        return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #18
Source File: ReceiveFragment.java    From xmrwallet with Apache License 2.0 6 votes vote down vote up
public Bitmap generate(String text, int width, int height) {
    if ((width <= 0) || (height <= 0)) return null;
    Map<EncodeHintType, Object> hints = new HashMap<>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
    try {
        BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
        int[] pixels = new int[width * height];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (bitMatrix.get(j, i)) {
                    pixels[i * width + j] = 0x00000000;
                } else {
                    pixels[i * height + j] = 0xffffffff;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
        bitmap = addLogo(bitmap);
        return bitmap;
    } catch (WriterException ex) {
        Timber.e(ex);
    }
    return null;
}
 
Example #19
Source File: ZxingHelper.java    From seezoon-framework-all with Apache License 2.0 6 votes vote down vote up
private BufferedImage createImage(String content, InputStream logo, boolean needCompress) throws Exception {
	Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
	hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
	hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
	hints.put(EncodeHintType.MARGIN, 1);
	BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, imageSize, imageSize,
			hints);
	int width = bitMatrix.getWidth();
	int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
		}
	}
	if (logo == null) {
		return image;
	}
	// 插入logo
	this.insertLogo(image, logo, needCompress);
	return image;
}
 
Example #20
Source File: ActionCreateCode.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
    *二维码实现
    * @param msg /二维码包含的信息
    */
public static byte[] getBarCodeWo(String msg){
		byte[] bs = null;
        try {
            String format = "png";
            Map<EncodeHintType,String> map =new HashMap<EncodeHintType, String>();
            //设置编码 EncodeHintType类中可以设置MAX_SIZE, ERROR_CORRECTION,CHARACTER_SET,DATA_MATRIX_SHAPE,AZTEC_LAYERS等参数
            map.put(EncodeHintType.CHARACTER_SET,"UTF-8");
            map.put(EncodeHintType.MARGIN,"1");
            //生成二维码
            BitMatrix bitMatrix = new MultiFormatWriter().encode(msg, BarcodeFormat.QR_CODE,300,300,map);
            BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			ImageIO.write(image, "gif", out);
			bs = out.toByteArray();
            
        }catch (Exception e) {
            e.printStackTrace();
        }
        return bs;
}
 
Example #21
Source File: ZxingUtils.java    From wish-pay with Apache License 2.0 6 votes vote down vote up
/**
 * 将内容contents生成长为width,宽为width的图片,图片路径由imgPath指定
 */
public static File getQRCodeImge(String contents, int width, int height, String imgPath) {
    try {
        Map<EncodeHintType, Object> hints = Maps.newHashMap();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF8");

        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);

        File imageFile = new File(imgPath);
        writeToFile(bitMatrix, "png", imageFile);

        return imageFile;

    } catch (Exception e) {
        log.error("create QR code error!", e);
        return null;
    }
}
 
Example #22
Source File: ZxingUtils.java    From MeetingFilm with Apache License 2.0 6 votes vote down vote up
/** 将内容contents生成长为width,宽为width的图片,图片路径由imgPath指定
    */
public static File getQRCodeImge(String contents, int width, int height, String imgPath) {
	try {
           Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
           hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
           hints.put(EncodeHintType.CHARACTER_SET, "UTF8");

		BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);

           File imageFile = new File(imgPath);
		writeToFile(bitMatrix, "png", imageFile);

           return imageFile;

	} catch (Exception e) {
		log.error("create QR code error!", e);
           return null;
	}
}
 
Example #23
Source File: QREncoder.java    From Lunary-Ethereum-Wallet with GNU General Public License v3.0 6 votes vote down vote up
public Bitmap encodeAsBitmap() throws WriterException {
    if (!encoded) return null;

    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
        hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    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;
}
 
Example #24
Source File: QrCodeGenerator.java    From QrCodeLib with MIT License 6 votes vote down vote up
public static Bitmap getQrCodeImage(String data, int width, int height) {
    if (data == null || data.length() == 0) {
        return null;
    }
    Map<EncodeHintType, Object> hintsMap = new HashMap<>(3);
    hintsMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
    hintsMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    hintsMap.put(EncodeHintType.MARGIN, 0);
    try {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(data, BarcodeFormat.QR_CODE, width, height, hintsMap);
        Bitmap bitmap = bitMatrix2Bitmap(bitMatrix);
        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #25
Source File: PMedia.java    From PHONK with GNU General Public License v3.0 6 votes vote down vote up
public Bitmap generateQRCode(String text) {
    Bitmap bmp = null;

    Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // H = 30% damage

    int size = 256;

    BitMatrix bitMatrix = null;
    try {
        bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hintMap);

        int width = bitMatrix.getWidth();
        bmp = Bitmap.createBitmap(width, width, Bitmap.Config.RGB_565);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < width; y++) {
                bmp.setPixel(y, x, bitMatrix.get(x, y) == true ? Color.BLACK : Color.WHITE);
            }
        }
    } catch (WriterException e) {
        e.printStackTrace();
    }

    return bmp;
}
 
Example #26
Source File: EncodingHandler.java    From QrCodeLib with MIT License 6 votes vote down vote up
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;
}
 
Example #27
Source File: QRCodeEncode.java    From Tok-Android with GNU General Public License v3.0 6 votes vote down vote up
public Bitmap encodeAsBitmap() throws WriterException {
    if (!encoded) return null;

    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
        hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    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;
}
 
Example #28
Source File: EAN8Writer.java    From Tesseract-OCR-Scanner with Apache License 2.0 5 votes vote down vote up
@Override
public BitMatrix encode(String contents,
                        BarcodeFormat format,
                        int width,
                        int height,
                        Map<EncodeHintType,?> hints) throws WriterException {
  if (format != BarcodeFormat.EAN_8) {
    throw new IllegalArgumentException("Can only encode EAN_8, but got "
        + format);
  }

  return super.encode(contents, format, width, height, hints);
}
 
Example #29
Source File: RxQRCode.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
public static Bitmap creatQRCode(CharSequence content, int QR_WIDTH, int QR_HEIGHT, int backgroundColor, int codeColor) {
    Bitmap bitmap = null;
    try {
        // 判断URL合法性
        if (content == null || "".equals(content) || content.length() < 1) {
            return null;
        }
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 图像数据转换,使用了矩阵转换
        BitMatrix bitMatrix = new QRCodeWriter().encode(content + "", BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
        int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
        // 下面这里按照二维码的算法,逐个生成二维码的图片,
        // 两个for循环是图片横列扫描的结果
        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] = codeColor;
                } else {
                    pixels[y * QR_WIDTH + x] = backgroundColor;
                }
            }
        }
        // 生成二维码图片的格式,使用ARGB_8888
        bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return bitmap;
}
 
Example #30
Source File: PDF417Writer.java    From letv with Apache License 2.0 5 votes vote down vote up
@Deprecated
public BitMatrix encode(String contents, BarcodeFormat format, boolean compact, int width, int height, int minCols, int maxCols, int minRows, int maxRows, Compaction compaction) throws WriterException {
    Map<EncodeHintType, Object> hints = new EnumMap(EncodeHintType.class);
    hints.put(EncodeHintType.PDF417_COMPACT, Boolean.valueOf(compact));
    hints.put(EncodeHintType.PDF417_COMPACTION, compaction);
    hints.put(EncodeHintType.PDF417_DIMENSIONS, new Dimensions(minCols, maxCols, minRows, maxRows));
    return encode(contents, format, width, height, hints);
}