实现扫描二维码和生成带logo的二维码
2015-11-04 21:34
706 查看
欢迎来到风的博客
今天讲的是如何引用google的zxing库实现扫描二维码和生成带logo的二维码,源码库可以从github上下载[]https://github.com/zxing/zxing];在文章结尾也会分享我的Demo喜欢请关注我[]http://myfengnull.github.io/]
扫描二维码
Intent startScan = new Intent(MainActivity.this,CaptureActivity.class); startActivity(startScan);
生成不带logo的二维码
String in = input.getText().toString(); if (in.equals("")) { Toast.makeText(MainActivity.this, "请输入文本", Toast.LENGTH_SHORT).show(); } try { //TODO 也可以生成二维码 // Bitmap qrcod = EncodingHandler.createQRCode(in, 400); // img.setImageBitmap(qrcod); // 写入数据信息到图片 int width = 400, height = 400; QRCodeWriter writer = new QRCodeWriter(); //把内容编码 BitMatrix matrix = writer.encode(in, BarcodeFormat.QR_CODE, width, height); int ms[] = new int[width * height]; //变换赋值 for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (matrix.get(x, y)) { //黑点 ms[y * width + x] = 0xff000000; } else { //白点 ms[y * width + x] = 0xffffffff; } } } //TODO 缓存 Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); image.setPixels(ms, 0, width, 0, 0, width, height); //TODO 展示图片 img.setImageBitmap(image); FileOutputStream out = new FileOutputStream("/sdcard/code2.png"); //TODO 压缩 image.compress(Bitmap.CompressFormat.PNG, 100, out); Log.e("MMMM", "创建成功"); } catch (WriterException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
带logo的二维码
//Todo 带logo的二维码 @Override public void onClick(View v) { String in = input.getText().toString(); if (in.equals("")) { Toast.makeText(MainActivity.this, "请输入文本", Toast.LENGTH_SHORT).show(); } try { // 写入数据信息到图片 int width = 400, height = 400; QRCodeWriter writer = new QRCodeWriter(); //把内容编码 BitMatrix matrix = writer.encode(in, BarcodeFormat.QR_CODE, width, height); int ms[] = new int[width * height]; //变换赋值 for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (matrix.get(x, y)) { //黑点 ms[y * width + x] = 0xff000000; } else { //白点 ms[y * width + x] = 0xffffffff; } } } //TODO 缓存 Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); image.setPixels(ms, 0, width, 0, 0, width, height); //logo地址 Bitmap logo = BitmapFactory.decodeFile("/sdcard/gur03.png"); image = insertLogo(image, logo); //TODO 展示图片 img.setImageBitmap(image); //存储生成的二维码地址 FileOutputStream out = new FileOutputStream("/sdcard/code2.png"); //TODO 压缩 image.compress(Bitmap.CompressFormat.PNG, 100, out); Log.e("MMMM", "创建成功"); } catch (WriterException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } public Bitmap insertLogo(Bitmap src, Bitmap logo) { // 获取到两张图片的宽高 int width = src.getWidth(); int height = src.getHeight(); int gwidth = logo.getWidth(); int gheight = logo.getHeight(); // 大小图片的比例 float scale = width * 1.0f / 5 / gwidth; // 工作缓冲区 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // 获取缓冲区的画布 Canvas ca = new Canvas(bitmap); // 话大图片 ca.drawBitmap(src, 0, 0, null); ca.scale(scale, scale, width / 2, height / 2); // 话小图片 ca.drawBitmap(logo, (width - gwidth) / 2, (height - gheight) / 2, null); // 保存所画内容 ca.save(Canvas.ALL_SAVE_FLAG); // 还原画布 ca.restore(); return bitmap; }
注意利用画布将图片覆盖在生成的二维码上,图片是30*30的,
原理是二维码中间有可以忽略的阴影
app引用BarCodeTest类库
Demo下载地址:https://github.com/MyfengNull/QrCode
本文原创下载请注明出处
相关文章推荐
- 进阶之路(基础篇)OC——Category&Extension
- Apriori algorithm---数据挖掘初学1
- Django引入静态资源文件
- django部署
- godep的save和update
- 支付宝被Google Play下架事件
- 支付宝被Google Play下架事件
- django执行过程
- HDU5512 Pagodas(博弈)
- Google推荐的图片加载库Glide介绍及使用
- UVA-11383 Golden Tiger Claw (KM算法)
- 论文阅读 Visual Categorization with Bags of Keypoints
- uva111
- 简单几何(极角排序) POJ 2007 Scrambled Polygon
- go环境搭建
- Django——错误收集
- golang安装
- 开启go语言之旅
- LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)
- 1153 - Got a packet bigger than 'max_allowed_packet' bytes With statement