关于安卓集成zxing扫码功能问题

一、场景

安卓应用需要集成扫码功能

二、解决方案

从网上了解到的方法大部分都是集成zxing开源方案的,具体地址如下:

https://github.com/zxing/zxing

 

之前在网上找到一个案例集成,发现没有支持相册图片扫码功能,于是再次寻找方案,看到的大体思路如下:

1、界面增加跳转访问相册,获取图片uri;

2、对图片进行采样压缩处理;

3、将压缩后的bitmap图片流文件使用zxing 接口进行解码识别;

4、返回识别结果;

流程看起来很简单清晰,但是在调试过程中发现使用的方案总是识别不成功,调试发现代码抛出 com.google.zxing.NotFoundException 异常,在网上查了很多回答

大部分是说接口调用时的hint 设置问题如: https://github.com/zxing/zxing/issues/216

Hashtable<DecodeHintType, String> hints = new Hashtable<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF8"); //设置二维码内容的编码

scanBitmap = BitmapUtil.decodeUri(this, uri, 500, 500);
RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
try {
    return reader.decode(bitmap1, hints);
} catch (NotFoundException e) {
    e.printStackTrace();
} catch (ChecksumException e) {
    e.printStackTrace();
} catch (FormatException e) {
    e.printStackTrace();
}

然而我这边将 hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE); 这个加上,以及将图片压缩的比例减小,即尽量还原图片,甚至直接使用原图还是不行。

在调试了无数遍之后,就知道还是要经历小马过河的场景,觉得使用的代码可能有问题。

后面继续寻找用了一个github上面关注较多的方案:https://github.com/yuzhiqiang1993/zxing

此方法刚开始也是失败的,后面又换了几张图片,竟然有成功的,于是大喜过望,开始研究其源码,发现其调用的解码接口类和之前的有所不同。

之前的:

QRCodeReader reader = new QRCodeReader();
try {
    return reader.decode(bitmap1, hints);
} catch (NotFoundException e) 

现在成功的:

MultiFormatReader multiFormatReader = new MultiFormatReader();
// 解码的参数
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(2);
// 可以解析的编码类型
Vector<BarcodeFormat> decodeFormats = new Vector<BarcodeFormat>();


// 扫描的类型  一维码和二维码
decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS);
decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);
decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);

hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
// 设置解析的字符编码格式为UTF8
//  hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
// 设置解析配置参数
multiFormatReader.setHints(hints);
// 开始对图像资源解码
Result rawResult = null;
try {
    rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(scanBitmap))));

    Log.i("解析结果", rawResult.getText());

} catch (Exception e)

两者使用的reader 不同,不知道是不是因为后者支持的类型多了,所以成功的概率要大一些了,但是自己使用的图片都是二维码,按理跟这个没有区别,但是不管了,

能用就行,实力和时间不允许我深究。

所以如果要用比较完备的扫码功能的话,可以参考后者的github开源使用方法,作者也提供了引用库和简单的使用方法,亲测可行。

 

Files contained in zxing-1.6-core.jar: META-INF/MANIFEST.MF com.google.zxing.BarcodeFormat.class com.google.zxing.Binarizer.class com.google.zxing.BinaryBitmap.class com.google.zxing.ChecksumException.class com.google.zxing.DecodeHintType.class com.google.zxing.EncodeHintType.class com.google.zxing.FormatException.class com.google.zxing.LuminanceSource.class com.google.zxing.MultiFormatReader.class com.google.zxing.MultiFormatWriter.class com.google.zxing.NotFoundException.class com.google.zxing.Reader.class com.google.zxing.ReaderException.class com.google.zxing.Result.class com.google.zxing.ResultMetadataType.class com.google.zxing.ResultPoint.class com.google.zxing.ResultPointCallback.class com.google.zxing.Writer.class com.google.zxing.WriterException.class com.google.zxing.client.result.AbstractDoCoMoResultParser.class com.google.zxing.client.result.AddressBookAUResultParser.class com.google.zxing.client.result.AddressBookDoCoMoResultParser.class com.google.zxing.client.result.AddressBookParsedResult.class com.google.zxing.client.result.BizcardResultParser.class com.google.zxing.client.result.BookmarkDoCoMoResultParser.class com.google.zxing.client.result.CalendarParsedResult.class com.google.zxing.client.result.EmailAddressParsedResult.class com.google.zxing.client.result.EmailAddressResultParser.class com.google.zxing.client.result.EmailDoCoMoResultParser.class com.google.zxing.client.result.ExpandedProductParsedResult.class com.google.zxing.client.result.ExpandedProductResultParser.class com.google.zxing.client.result.GeoParsedResult.class com.google.zxing.client.result.GeoResultParser.class com.google.zxing.client.result.ISBNParsedResult.class com.google.zxing.client.result.ISBNResultParser.class com.google.zxing.client.result.ParsedResult.class com.google.zxing.client.result.ParsedResultType.class com.google.zxing.client.result.ProductParsedResult.class com.google.zxing.client.result.ProductResultParser.class com.google.zxing.client.result.ResultParser.class com.google.zxing.client.result.SMSMMSResultParser.class com.google.zxing.client.result.SMSParsedResult.class com.google.zxing.client.result.SMSTOMMSTOResultParser.class com.google.zxing.client.result.TelParsedResult.class com.google.zxing.client.result.TelResultParser.class com.google.zxing.client.result.TextParsedResult.class com.google.zxing.client.result.URIParsedResult.class com.google.zxing.client.result.URIResultParser.class com.google.zxing.client.result.URLTOResultParser.class com.google.zxing.client.result.VCardResultParser.class com.google.zxing.client.result.VEventResultParser.class com.google.zxing.client.result.WifiParsedResult.class com.google.zxing.client.result.WifiResultParser.class com.google.zxing.client.result.optional.AbstractNDEFResultParser.class com.google.zxing.client.result.optional.NDEFRecord.class com.google.zxing.client.result.optional.NDEFSmartPosterParsedResult.class com.google.zxing.client.result.optional.NDEFSmartPosterResultParser.class com.google.zxing.client.result.optional.NDEFTextResultParser.class com.google.zxing.client.result.optional.NDEFURIResultParser.class com.google.zxing.common.BitArray.class com.google.zxing.common.BitMatrix.class com.google.zxing.common.BitSource.class com.google.zxing.common.CharacterSetECI.class com.google.zxing.common.Collections.class com.google.zxing.common.Comparator.class com.google.zxing.common.DecoderResult.class com.google.zxing.common.DefaultGridSampler.class com.google.zxing.common.DetectorResult.class com.google.zxing.common.ECI.class com.google.zxing.common.GlobalHistogramBinarizer.class com.google.zxing.common.GridSampler.class com.google.zxing.common.HybridBinarizer.class com.google.zxing.common.PerspectiveTransform.class com.google.zxing.common.StringUtils.class com.google.zxing.common.detector.MonochromeRectangleDetector.class com.google.zxing.common.detector.WhiteRectangleDetector.class com.google.zxing.common.reedsolomon.GF256.class com.google.zxing.common.reedsolomon.GF256Poly.class com.google.zxing.common.reedsolomon.ReedSolomonDecoder.class com.google.zxing.common.reedsolomon.ReedSolomonEncoder.class com.google.zxing.common.reedsolomon.ReedSolomonException.class com.google.zxing.datamatrix.DataMatrixReader.class com.google.zxing.datamatrix.decoder.BitMatrixParser.class com.google.zxing.datamatrix.decoder.DataBlock.class com.google.zxing.datamatrix.decoder.DecodedBitStreamParser.class com.google.zxing.datamatrix.decoder.Decoder.class com.google.zxing.datamatrix.decoder.Version.class com.google.zxing.datamatrix.detector.Detector.class com.google.zxing.multi.ByQuadrantReader.class com.google.zxing.multi.GenericMultipleBarcodeReader.class com.google.zxing.multi.MultipleBarcodeReader.class com.google.zxing.multi.qrcode.QRCodeMultiReader.class com.google.zxing.multi.qrcode.detector.MultiDetector.class com.google.zxing.multi.qrcode.detector.MultiFinderPatternFinder.class com.google.zxing.oned.CodaBarReader.class com.google.zxing.oned.Code128Reader.class com.google.zxing.oned.Code128Writer.class com.google.zxing.oned.Code39Reader.class com.google.zxing.oned.Code39Writer.class com.google.zxing.oned.Code93Reader.class com.google.zxing.oned.EAN13Reader.class com.google.zxing.oned.EAN13Writer.class com.google.zxing.oned.EAN8Reader.class com.google.zxing.oned.EAN8Writer.class com.google.zxing.oned.EANManufacturerOrgSupport.class com.google.zxing.oned.ITFReader.class com.google.zxing.oned.ITFWriter.class com.google.zxing.oned.MultiFormatOneDReader.class com.google.zxing.oned.MultiFormatUPCEANReader.class com.google.zxing.oned.OneDReader.class com.google.zxing.oned.UPCAReader.class com.google.zxing.oned.UPCEANExtensionSupport.class com.google.zxing.oned.UPCEANReader.class com.google.zxing.oned.UPCEANWriter.class com.google.zxing.oned.UPCEReader.class com.google.zxing.oned.rss.AbstractRSSReader.class com.google.zxing.oned.rss.DataCharacter.class com.google.zxing.oned.rss.FinderPattern.class com.google.zxing.oned.rss.Pair.class com.google.zxing.oned.rss.RSS14Reader.class com.google.zxing.oned.rss.RSSUtils.class com.google.zxing.oned.rss.expanded.BitArrayBuilder.class com.google.zxing.oned.rss.expanded.ExpandedPair.class com.google.zxing.oned.rss.expanded.RSSExpandedReader.class com.google.zxing.oned.rss.expanded.decoders.AI013103decoder.class com.google.zxing.oned.rss.expanded.decoders.AI01320xDecoder.class com.google.zxing.oned.rss.expanded.decoders.AI01392xDecoder.class com.google.zxing.oned.rss.expanded.decoders.AI01393xDecoder.class com.google.zxing.oned.rss.expanded.decoders.AI013x0x1xDecoder.class com.google.zxing.oned.rss.expanded.decoders.AI013x0xDecoder.class com.google.zxing.oned.rss.expanded.decoders.AI01AndOtherAIs.class com.google.zxing.oned.rss.expanded.decoders.AI01decoder.class com.google.zxing.oned.rss.expanded.decoders.AI01weightDecoder.class com.google.zxing.oned.rss.expanded.decoders.AbstractExpandedDecoder.class com.google.zxing.oned.rss.expanded.decoders.AnyAIDecoder.class com.google.zxing.oned.rss.expanded.decoders.BlockParsedResult.class com.google.zxing.oned.rss.expanded.decoders.CurrentParsingState.class com.google.zxing.oned.rss.expanded.decoders.DecodedChar.class com.google.zxing.oned.rss.expanded.decoders.DecodedInformation.class com.google.zxing.oned.rss.expanded.decoders.DecodedNumeric.class com.google.zxing.oned.rss.expanded.decoders.DecodedObject.class com.google.zxing.oned.rss.expanded.decoders.FieldParser.class com.google.zxing.oned.rss.expanded.decoders.GeneralAppIdDecoder.class com.google.zxing.pdf417.PDF417Reader.class com.google.zxing.pdf417.decoder.BitMatrixParser.class com.google.zxing.pdf417.decoder.DecodedBitStreamParser.class com.google.zxing.pdf417.decoder.Decoder.class com.google.zxing.pdf417.detector.Detector.class com.google.zxing.qrcode.QRCodeReader.class com.google.zxing.qrcode.QRCodeWriter.class com.google.zxing.qrcode.decoder.BitMatrixParser.class com.google.zxing.qrcode.decoder.DataBlock.class com.google.zxing.qrcode.decoder.DataMask.class com.google.zxing.qrcode.decoder.DecodedBitStreamParser.class com.google.zxing.qrcode.decoder.Decoder.class com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.class com.google.zxing.qrcode.decoder.FormatInformation.class com.google.zxing.qrcode.decoder.Mode.class com.google.zxing.qrcode.decoder.Version.class com.google.zxing.qrcode.detector.AlignmentPattern.class com.google.zxing.qrcode.detector.AlignmentPatternFinder.class com.google.zxing.qrcode.detector.Detector.class com.google.zxing.qrcode.detector.FinderPattern.class com.google.zxing.qrcode.detector.FinderPatternFinder.class com.google.zxing.qrcode.detector.FinderPatternInfo.class com.google.zxing.qrcode.encoder.BlockPair.class com.google.zxing.qrcode.encoder.ByteMatrix.class com.google.zxing.qrcode.encoder.Encoder.class com.google.zxing.qrcode.encoder.MaskUtil.class com.google.zxing.qrcode.encoder.MatrixUtil.class com.google.zxing.qrcode.encoder.QRCode.class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值