android实现二维码,并将二维码保存至SD卡

来源:互联网 发布:facebook登录网络异常 编辑:程序博客网 时间:2024/06/06 01:45

利用开源zxing(http://code.google.com/p/zxing/)项目生成二维码

1.下载zxing的核心包,core.jar,一般在网上没有直接提供此jar文件的下载,后面我将会上传,在项目根目录下建立libs文件夹,将下载的jar文件放在libs目录下,注意,文件夹的名字一定要是libs,否则会报错。

2.实现二维码的生成,业务类

public class QRCodeService {// 图片大小,注意图片不要设置太大,否则会影响二维码的识别private static final int IMAGE_WIDTH = 30;/** * 生成普通二维码方法 *  * @param 传入二维码的内容 * @return Bitmap 插入到二维码中间的位图对象,可以传入Null,传入NULL后,生成的二维码中间不带图片 */public Bitmap cretaeBitmap(String str, Bitmap mBitmap) throws Exception {Map<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败BitMatrix matrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, 300, 300, hints);int width = matrix.getWidth();int height = matrix.getHeight();// 二维矩阵转为一维像素数组,也就是一直横着排了int halfW = width / 2;int halfH = height / 2;int[] pixels = new int[width * height];for (int y = 0; y < height; y++) {for (int x = 0; x < width; x++) {// 如果mBitmap不为空,则添加中间小图片if (mBitmap != null && x > halfW - IMAGE_WIDTH&& x < halfW + IMAGE_WIDTH && y > halfH - IMAGE_WIDTH&& y < halfH + IMAGE_WIDTH) {pixels[y * width + x] = mBitmap.getPixel(x - halfW+ IMAGE_WIDTH, y - halfH + IMAGE_WIDTH);} else {if (matrix.get(x, y)) {pixels[y * width + x] = 0xff314785;// 生成二维码填充的颜色。黑色}}}}Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);// 通过像素数组生成bitmap位图bitmap.setPixels(pixels, 0, width, 0, 0, width, height);return bitmap;}/** * 生成中间带图片的二维码 *  * @param image *            设置需要放在二维码中间的图片 * @content 传入二维码的内容 * @resources Resources获得对象 */public Bitmap createImageViewQRBitmap(int image, String content,Resources resources) throws Exception {// 构造需要插入的位图对象Bitmap mBitmap = ((BitmapDrawable) resources.getDrawable(image)).getBitmap();// 进行缩放图片Matrix m = new Matrix();float sx = (float) 2 * IMAGE_WIDTH / mBitmap.getWidth();float sy = (float) 2 * IMAGE_WIDTH / mBitmap.getHeight();m.setScale(sx, sy);// 重新构造位图对象mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(),mBitmap.getHeight(), m, true);mBitmap = cretaeBitmap(content, mBitmap);return mBitmap;}}
3.将生成的二维码保存在SD卡上

/** * 将生成的二维码保存在SD卡 */public class QRToSDcardSaveService {/** * 将位图对象转换为字节数组 * @param bm * @return */private byte[] Bitmap2Bytes(Bitmap bitmap) {ByteArrayOutputStream outputStream = new ByteArrayOutputStream();bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);return outputStream.toByteArray();}/** * 保存二维码至SD卡 * @param filename * @param bitmap */public void saveToSDCard(String filename, Bitmap bitmap) throws Exception {// 获取SD卡的路径:Environment.getExternalStorageDirectory()File file = new File(Environment.getExternalStorageDirectory(),filename);FileOutputStream outStream = new FileOutputStream(file);outStream.write(Bitmap2Bytes(bitmap));outStream.close();}}
4.调用

public class MyCardActivity extends Activity {private QRCodeService service;private QRToSDcardSaveService dcardSaveService;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        ImageView imageView=(ImageView)this.findViewById(R.id.card);//用来显示二维码的视图         service=new QRCodeService();//生成二维码的业务类         dcardSaveService=new QRToSDcardSaveService();//保存二维码至SD卡的业务类        try {        //生成二维码        Bitmap b=service.createImageViewQRBitmap(R.drawable.ic_launcher, "要存入的信息",getResources());        imageView.setImageBitmap(b);//将生成的二维码显示在视图控件上        dcardSaveService.saveToSDCard("ggg1.png", b);//将生成的二维码保存在SD卡上} catch (Exception e) {e.printStackTrace();Toast.makeText(this, R.string.error, 1).show();}    }}
注意:将二维码保存在SD卡上需要写入权限,在项目清单文件中增加

<!-- 往SDCard写入数据权限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
源码下载地址:android实现二维码源码下载


原创粉丝点击