android 二维码生成

来源:互联网 发布:手机足球关注软件 编辑:程序博客网 时间:2024/04/30 12:01

1.使用第三方jar,简介

ZXing是一个开放源码的第三方库,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。该项目可实现的条形码编码和解码。目前支持以下格式:UPC-A,UPC-E、EAN-8,EAN-13、39码、93码。ZXing是个很经典的条码/二维码识别的开源类库,以前在功能机上,就有开发者使用J2ME运用ZXing了,不过要支持JSR-234规范(自动对焦)的手机才能发挥其威力。

2 .导入jar。zixing.jar下载地址:

http://download.csdn.net/detail/haoaoo/9702775

3.代码示例:

 public void showQrCode(final ImageView imageView) {        final String filePath = getFileRoot(this) + File.separator                + "qr_" + System.currentTimeMillis() + ".jpg";        new Thread(new Runnable() {            @Override            public void run() {                boolean success = QrCodeUtil.createQRImage("www.baidu.com", 800, 800, BitmapFactory.decodeResource(getResources(), R.drawable.app_icon),                        filePath);                if (success) {                    runOnUiThread(new Runnable() {                        @Override                        public void run() {                            imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));                        }                    });                }            }        }).start();    }private String getFileRoot(Context context) {        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {            File external = context.getExternalFilesDir(null);            if (external != null) {                return external.getAbsolutePath();            }        }        return context.getFilesDir().getAbsolutePath();    }
1 0