二维码的生成和扫描

生成二维码:

/**
* 二维码的生成
* @param content 二维码的内容
* @param width 二维码的宽度
* @param height
* @return
*/
private Bitmap QrCodeImage(String content, int width, int height) {
if(content==null||"".equals(content)||content.length()<1){
return null;
}

QRCodeWriter qrCodeWriter = new QRCodeWriter();
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
int[] pixels = new int[width * height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
//当这个字节矩阵有值时,则给这个数组像素点设置为黑色,反之为白色
if (encode.get(j, i)) {
pixels[i * width + j] = 0x00000000;
} else {
pixels[i * width + j] = 0xffffffff;
}
}
}
return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}



扫描二维码:
/**
* 解析QR图,扫描二维码,将扫描出来的结果放在签名处
*/

private void scanningImage() {
Map<DecodeHintType, String> hints = new HashMap<DecodeHintType, String>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
// 获得待解析的图片
Bitmap scanBitmap = ((BitmapDrawable) imageView1.getDrawable()).getBitmap();
int width = scanBitmap.getWidth();
int height = scanBitmap.getHeight();
int[] pixels = new int[width * height];
scanBitmap.getPixels(pixels, 0, width, 0, 0, width, height);//这行代码不能省略
RGBLuminanceSource source=new RGBLuminanceSource(width,height,pixels);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
Result result;
try {
result = reader.decode(bitmap1, hints);
// 得到解析后的文字
editor.setText(result.getText());
} catch (NotFoundException e) {
e.printStackTrace();
} catch (ChecksumException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
}
}

转载于:https://www.cnblogs.com/xhf-wonder/p/6801702.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值