欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Java QRCodeWriter 类使用示例 - 例 1:getQR

最编程 2024-05-06 19:19:56
...
import com.google.zxing.qrcode.QRCodeWriter; //导入依赖的package包/类
public static Bitmap getQR(final byte[] content, final int size) {
    final QRCodeWriter writer = new QRCodeWriter();
    try {
        final Map<EncodeHintType, Object> encodingHints = new HashMap<>();
        encodingHints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        encodingHints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

        final BitMatrix encoding = writer.encode(Utils.makeQR(content), BarcodeFormat.QR_CODE, size, size, encodingHints);
        final Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                bitmap.setPixel(i, j, encoding.get(i, j) ? COLOR_DARK : COLOR_LIGHT);
            }
        }

        return bitmap;
    } catch (WriterException e) {
        Log.e("QRUtils", "Failed to get QR code", e);
    }
    return null;
}
 

上一篇: 企业代码生成系统

下一篇:

推荐阅读