用于qr代码生成的Android Zxing库

时间:2012-11-09 06:07:15

标签: android zxing

我想在我的应用程序中生成文本的qr代码我必须zxing库但我不知道实现它。我该如何实现这个?任何帮助

3 个答案:

答案 0 :(得分:3)

        QRCodeWriter writer = new QRCodeWriter();
        try
        {
            EnumMap<EncodeHintType, Object> hint = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
            hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, dimention, dimention, hint);
            int width = bitMatrix.getWidth();
            int height = bitMatrix.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] = bitMatrix.get(x, y) ? 0xFF000000
                    // : 0xFFFFFFFF;
                    pixels[offset + x] = bitMatrix.get(x, y) ? colorBack : colorFront;
                }
            }

            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;

答案 1 :(得分:3)

您需要将最新版ZXing中的core.jar文件添加到您的项目中。您还需要向项目中再添加两个类。

以下是关于如何操作的step-by-step guide

答案 2 :(得分:2)

below code can help you to generate qr code 

Intent intent = new Intent();
intent.setAction(Intents.Encode.ACTION);
intent.putExtra(Intents.Encode.FORMAT, BarcodeFormat.QR_CODE.toString());
intent.putExtra(Intents.Encode.TYPE, Contents.Type.TEXT);
intent.putExtra(Intents.Encode.DATA, codeString);
QRCodeEncoder qrcode = new QRCodeEncoder(YourActivity.this, intent,250);

try {
Bitmap bitmap = qrcode.encodeAsBitmap();
imgBarcode = (ImageView) findViewById(R.id.imgbarcode);
imgBarcode.setImageBitmap(bitmap);

} catch (WriterException e) {
e.printStackTrace();
}