您的位置:首页 > 编程语言 > Go语言

实现扫描二维码和生成带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

本文原创下载请注明出处
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: