解析图片二维码


public class BitmapLuminanceSource extends LuminanceSource {

		private byte bitmapPixels[];

		protected BitmapLuminanceSource(Bitmap bitmap) {
			super(bitmap.getWidth(), bitmap.getHeight());

			// 首先,要取得该图片的像素数组内容
			int[] data = new int[bitmap.getWidth() * bitmap.getHeight()];
			this.bitmapPixels = new byte[bitmap.getWidth() * bitmap.getHeight()];
			bitmap.getPixels(data, 0, getWidth(), 0, 0, getWidth(), getHeight());

			// 将int数组转换为byte数组,也就是取像素值中蓝色值部分作为辨析内容
			for (int i = 0; i < data.length; i++) {
				this.bitmapPixels[i] = (byte) data[i];
			}
		}

		@Override
		public byte[] getMatrix() {
			// 返回我们生成好的像素数据
			return bitmapPixels;
		}

		@Override
		public byte[] getRow(int y, byte[] row) {
			// 这里要得到指定行的像素数据
			System.arraycopy(bitmapPixels, y * getWidth(), row, 0, getWidth());
			return row;
		}
	}

下面的方法是关键

Map<DecodeHintType, String> hints = new HashMap<DecodeHintType, String>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");

// 获得待解析的图片
BitmapLuminanceSource source = new BitmapLuminanceSource(bitMap);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
Result result;
try {
	result = reader.decode(bitmap1, hints);
	// 得到解析后的文字
	System.out.println(result.getText() + "\r\n" + result.getBarcodeFormat().toString());
} catch (NotFoundException e) {
	e.printStackTrace();
} catch (ChecksumException e) {
	e.printStackTrace();
} catch (FormatException e) {
	e.printStackTrace();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值