二维码识别源代码.docx
- 文档编号:23427302
- 上传时间:2023-05-17
- 格式:DOCX
- 页数:20
- 大小:18.71KB
二维码识别源代码.docx
《二维码识别源代码.docx》由会员分享,可在线阅读,更多相关《二维码识别源代码.docx(20页珍藏版)》请在冰豆网上搜索。
二维码识别源代码
QRCodeDecode
usingSystem;
usingSystem.Text;
usingQRCodeImage=ThoughtWorks.QRCode.Codec.Data.QRCodeImage;
usingQRCodeSymbol=ThoughtWorks.QRCode.Codec.Data.QRCodeSymbol;
usingReedSolomon=ThoughtWorks.QRCode.Codec.Ecc.ReedSolomon;
usingDecodingFailedException=ThoughtWorks.QRCode.ExceptionHandler.DecodingFailedException;
usingInvalidDataBlockException=ThoughtWorks.QRCode.ExceptionHandler.InvalidDataBlockException;
usingSymbolNotFoundException=ThoughtWorks.QRCode.ExceptionHandler.SymbolNotFoundException;
usingPoint=ThoughtWorks.QRCode.Geom.Point;
usingQRCodeDataBlockReader=ThoughtWorks.QRCode.Codec.Reader.QRCodeDataBlockReader;
usingQRCodeImageReader=ThoughtWorks.QRCode.Codec.Reader.QRCodeImageReader;
usingDebugCanvas=ThoughtWorks.QRCode.Codec.Util.DebugCanvas;
usingDebugCanvasAdapter=ThoughtWorks.QRCode.Codec.Util.DebugCanvasAdapter;
usingQRCodeUtility=ThoughtWorks.QRCode.Codec.Util.QRCodeUtility;
namespaceThoughtWorks.QRCode.Codec
{
publicclassQRCodeDecoder
{
internalQRCodeSymbolqrCodeSymbol;
internalintnumTryDecode;
internalSystem.Collections.ArrayListresults;
internalSystem.Collections.ArrayListlastResults=System.Collections.ArrayList.Synchronized(newSystem.Collections.ArrayList(10));
internalstaticDebugCanvascanvas;
internalQRCodeImageReaderimageReader;
internalintnumLastCorrections;
internalboolcorrectionSucceeded;
publicstaticDebugCanvasCanvas
{
get
{
returnQRCodeDecoder.canvas;
}
set
{
QRCodeDecoder.canvas=value;
}
}
virtualinternalPoint[]AdjustPoints
{
get
{
//notethatadjustsaffectdependently
//i.e.belowmeans(0,0),(2,3),(3,4),(1,2),(2,1),(1,1),(-1,-1)
//Point[]adjusts={newPoint(0,0),newPoint(2,3),newPoint(1,1),
//newPoint(-2,-2),newPoint(1,-1),newPoint(-1,0),newPoint(-2,-2)};
System.Collections.ArrayListadjustPoints=System.Collections.ArrayList.Synchronized(newSystem.Collections.ArrayList(10));
for(intd=0;d<4;d++)
adjustPoints.Add(newPoint(1,1));
intlastX=0,lastY=0;
for(inty=0;y>-4;y--)
{
for(intx=0;x>-4;x--)
{
if(x!
=y&&((x+y)%2==0))
{
adjustPoints.Add(newPoint(x-lastX,y-lastY));
lastX=x;
lastY=y;
}
}
}
Point[]adjusts=newPoint[adjustPoints.Count];
for(inti=0;i adjusts[i]=(Point)adjustPoints[i]; returnadjusts; } } internalclassDecodeResult { internalintnumCorrections; internalboolcorrectionSucceeded; internalsbyte[]decodedBytes; privateQRCodeDecoderenclosingInstance; publicDecodeResult(QRCodeDecoderenclosingInstance,sbyte[]decodedBytes,intnumErrors,boolcorrectionSucceeded) { InitBlock(enclosingInstance); this.decodedBytes=decodedBytes; this.numCorrections=numErrors; this.correctionSucceeded=correctionSucceeded; } privatevoidInitBlock(QRCodeDecoderenclosingInstance) { this.enclosingInstance=enclosingInstance; } virtualpublicsbyte[]DecodedBytes { get { returndecodedBytes; } } virtualpublicintNumErrors { get { returnnumCorrections; } } virtualpublicboolCorrectionSucceeded { get { returncorrectionSucceeded; } } publicQRCodeDecoderEnclosing_Instance { get { returnenclosingInstance; } } } publicQRCodeDecoder() { numTryDecode=0; results=System.Collections.ArrayList.Synchronized(newSystem.Collections.ArrayList(10)); QRCodeDecoder.canvas=newDebugCanvasAdapter(); } /*publicbyte[]decode(QRCodeImageqrCodeImage)throwsDecodingFailedException{ canvas.println("Decodingstarted."); int[][]intImage=imageToIntArray(qrCodeImage); try{ QRCodeImageReaderreader=newQRCodeImageReader(); qrCodeSymbol=reader.getQRCodeSymbol(intImage); }catch(SymbolNotFoundExceptione){ thrownewDecodingFailedException(e.getMessage()); } canvas.println("CreatedQRCodesymbol."); canvas.println("Readingsymbol."); canvas.println("Version: "+qrCodeSymbol.getVersionReference()); canvas.println("Maskpattern: "+qrCodeSymbol.getMaskPatternRefererAsString()); int[]blocks=qrCodeSymbol.getBlocks(); canvas.println("Correctingdataerrors."); int[]dataBlocks=correctDataBlocks(blocks); try{ byte[]decodedByteArray= getDecodedByteArray(dataBlocks,qrCodeSymbol.getVersion()); canvas.println("Decodingfinished."); returndecodedByteArray; }catch(InvalidDataBlockExceptione){ thrownewDecodingFailedException(e.getMessage()); } }*/ publicvirtualsbyte[]decodeBytes(QRCodeImageqrCodeImage) { Point[]adjusts=AdjustPoints; System.Collections.ArrayListresults=System.Collections.ArrayList.Synchronized(newSystem.Collections.ArrayList(10)); while(numTryDecode { try { DecodeResultresult=decode(qrCodeImage,adjusts[numTryDecode]); if(result.CorrectionSucceeded) { returnresult.DecodedBytes; } else { results.Add(result); canvas.println("Decodingsucceededbutcouldnotcorrect"); canvas.println("allerrors.Retrying.."); } } catch(DecodingFailedExceptiondfe) { if(dfe.Message.IndexOf("FinderPattern")>=0) throwdfe; } finally { numTryDecode+=1; } } if(results.Count==0) thrownewDecodingFailedException("Giveupdecoding"); intlowestErrorIndex=-1; intlowestError=System.Int32.MaxValue; for(inti=0;i { DecodeResultresult=(DecodeResult)results[i]; if(result.NumErrors { lowestError=result.NumErrors; lowestErrorIndex=i; } } canvas.println("Alltrialsneedforcorrecterror"); canvas.println("Reporting#"+(lowestErrorIndex)+"that,"); canvas.println("correctedminimumerrors("+lowestError+")"); canvas.println("Decodingfinished."); return((DecodeResult)results[lowestErrorIndex]).DecodedBytes; } publicvirtualStringdecode(QRCodeImageqrCodeImage,Encodingencoding) { sbyte[]data=decodeBytes(qrCodeImage); byte[]byteData=newbyte[data.Length]; Buffer.BlockCopy(data,0,byteData,0,byteData.Length); /* char[]decodedData=newchar[data.Length]; for(inti=0;i { decodedData[i]=Convert.to(data[i]); } returnnewString(decodedData); */ StringdecodedData; decodedData=encoding.GetString(byteData); returndecodedData; } publicvirtualStringdecode(QRCodeImageqrCodeImage) { sbyte[]data=decodeBytes(qrCodeImage); byte[]byteData=newbyte[data.Length]; Buffer.BlockCopy(data,0,byteData,0,byteData.Length); Encodingencoding; if(QRCodeUtility.IsUnicode(byteData)) { encoding=Encoding.Unicode; } else { encoding=Encoding.ASCII; } StringdecodedData; decodedData=encoding.GetString(byteData); returndecodedData; } internalvirtualDecodeResultdecode(QRCodeImageqrCodeImage,Pointadjust) { try { if(numTryDecode==0) { canvas.println("Decodingstarted"); int[][]intImage=imageToIntArray(qrCodeImage); imageReader=newQRCodeImageReader(); qrCodeSymbol=imageReader.getQRCodeSymbol(intImage); } else { canvas.println("--"); canvas.println("Decodingrestarted#"+(numTryDecode)); qrCodeSymbol=imageReader.getQRCodeSymbolWithAdjustedGrid(adjust); } } catch(SymbolNotFoundExceptione) { thrownewDecodingFailedException(e.Message); } canvas.println("CreatedQRCodesymbol."); canvas.println("Readingsymbol."); canvas.println("Version: "+qrCodeSymbol.VersionReference); canvas.println("Maskpattern: "+qrCodeSymbol.MaskPatternRefererAsString); //blockscontainsall(dataandRS)blocksinQRCodesymbol int[]blocks=qrCodeSymbol.Blocks; canvas.println("Correctingdataerrors."); //nowblocksturntodatablocks(correctedandextractedfromoriginalblocks) blocks=correctDataBlocks(blocks); try { sbyte[]decodedByteArray=getDecodedByteArray(blocks,qrCodeSymbol.Version,qrCodeSymbol.NumErrorCollectionCode); returnnewDecodeResult(this,decodedByteArray,numLastCorrections,correctionSucceeded); } catch(InvalidDataBlockExceptione) { canvas.println(e.Message); thrownewDecodingFailedException(e.Message); } } internalvirtualint[][]imageToIntArray(QRCodeImageimage) { intwidth=image.Width; intheight=image.Height; int[][]intImage=newint[width][]; for(inti=0;i { intImage[i]=newint[height]; } for(inty=0;y { for(intx=0;x { intImage[x][y]=image.getPixel(x,y); } } returnintImage; } internalvirtualint[]correctDataBlocks(int[]blocks) { intnumCorrections=0; intdataCapacity=qrCodeSymbol.DataCapacity; int[]dataBlocks=newint[dataCapacity]; intnumErrorCollectionCode=qrCodeSymbol.NumErrorCollectionCode; intnumRSBlocks=qrCodeSymbol.NumRSBlocks; inteccPerRSBlock=numErrorCollectionCode/numRSBlocks; if(numRSBlocks==1) { ReedSolomoncorrector=newReedSolomon(blocks,eccPerRSBlock); corrector.correct(); numCorrections+=corrector.NumCorrectedErrors; if(numCorrections>0) canvas.println(System.Convert.ToStri
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 二维码 识别 源代码
![提示](https://static.bdocx.com/images/bang_tan.gif)