该【二维码识别源代码 】是由【kunpengchaoyue】上传分享,文档一共【10】页,该文档可以免费在线阅读,需要了解更多关于【二维码识别源代码 】的内容,可以使用淘豆网的站内搜索功能,选择自己适合的文档,以下文字是截取该文章内的部分文字,如需要获得完整电子版,请下载此文档到您的设备,方便您编辑和打印。{
{
QRCodeDecode
usingSystem;
;
usingQRCodeImage=;
usingQRCodeSymbol=;
usingReedSolomon=;
usingDecodingFailedException=;
usingInvalidDataBlockException=;
usingSymbolNotFoundException=;
usingPoint=;
usingQRCodeDataBlockReader=;
usingQRCodeImageReader=;
usingDebugCanvas=;
usingDebugCanvasAdapter=;
usingQRCodeUtility=;
{
publicclassQRCodeDecoder
{
internalQRCodeSymbolqrCodeSymbol;
internalintnumTryDecode;
;
=((lO));
internalstaticDebugCanvascanvas;
internalQRCodeImageReaderimageReader;
internalintnumLastCorrections;
internalboolcorrectionSucceeded;
publicstaticDebugCanvasCanvas
{
get
{
;
set
=value;
}
virtualinternalPoint[]AdjustPoints
{
get
//notethatadjustsaffectdependently
//(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)};
=((lO));
for(intd=0;d<4;d++)
(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))
{
(newPoint(x-lastX,y-lastY));lastX=x;
lastY=y;
Point[]adjusts=newPoint[];for(inti=0;i<;i++)
adjusts[i]=(Point)adjustPoints[i];returnadjusts;
internalclassDecodeResult
{
internalintnumCorrections;
internalboolcorrectionSucceeded;
internalsbyte[]decodedBytes;
privateQRCodeDecoderenclosinginstance;
publicDecodeResult(QRCodeDecoderenclosinginstance,sbyte[]decodedBytes,intnumErrors,boolcorrectionSucceeded){
InitBlock(enclosinglnstance);
=decodedBytes;
=numErrors;
=correctionSucceeded;
privatevoidInitBlock(QRCodeDecoderenclosinginstance){
=enclosinginstance;
virtualpublicsbyte[]DecodedBytes
{
get
{
returndecodedBytes;
virtualpublicintNumErrors
{
get
{
returnnumCorrections;
virtualpublicboolCorrectionSucceeded{
get
{
returncorrectionSucceeded;
}
}
publicQRCodeDecoderEnclosing_Instance
{
get
{
returnenclosingInstance;
}
publicQRCodeDecoder()
{
numTryDecode=0;
results=((lO));=newDebugCanvasAdapter();
}
/*publicbyte[]decode(QRCodeImageqrCodelmage)throwsDecodingFailedException{("Decodingstarted.");
int[][]intImage=imageTolntArray(qrCodelmage);
try{
QRCodeImageReaderreader=newQRCodeImageReader();
qrCodeSymbol=(intlmage);
}catch(SymbolNotFoundExceptione){
thrownewDecodingFailedException(());
}
("CreatedQRCodesymbol.");
("Readingsymbol.");
("Version:"+());
("Maskpattern:"+());int[]blocks=();
("Correctingdataerrors.");
int[]dataBlocks=correctDataBlocks(blocks);
try{
byte[]decodedByteArray=
getDecodedByteArray(dataBlocks,());("Decodingfinished.");
returndecodedByteArray;
}catch(InvalidDataBlockExceptione){
thrownewDecodingFailedException(());
}
}*/
publicvirtualsbyte[]decodeBytes(QRCodeImageqrCodeImage)
{
Point[]adjusts=AdjustPoints;
=((lO));while(numTryDecode<)
{
try
{
DecodeResultresult=decode(qrCodeImage,adjusts[numTryDecode]);
if()
{
;
else
(result);
("Decodingsucceededbutcouldnotcorrect");
("..");
}
catch(DecodingFailedExceptiondfe)
{
if(("FinderPattern")>=0)throwdfe;
finally
{
numTryDecode+=1;
}
if(==0)
thrownewDecodingFailedException("Giveupdecoding");intlowestErrorlndex=-1;
intlowestError=;for(inti=0;i<;i++)
DecodeResultresult=(DecodeResult)results[i];if(<lowestError)
{
lowestError=;lowestErrorIndex=i;
("Alltrialsneedforcorrecterror");
("Reporting#"+(lowestErrorIndex)+"that,");("correctedminimumerrors("+lowestError+")");
("Decodingfinished.");
return((DecodeResult)results[lowestErrorIndex]).DecodedBytes;
}
publicvirtualStringdecode(QRCodeImageqrCodelmage,Encodingencoding){
sbyte[]data=decodeBytes(qrCodelmage);
byte[]byteData=newbyte[];
(data,0,byteData,0,);
/*
char[]decodedData=newchar[];
for(inti=0;i<;i++)
{
decodedData[i]=(data[i]);
}
returnnewString(decodedData);
*/
StringdecodedData;
decodedData=(byteData);
returndecodedData;
}
publicvirtualStringdecode(QRCodeImageqrCodeImage)
{
sbyte[]data=decodeBytes(qrCodelmage);
byte[]byteData=newbyte[];
(data,0,byteData,0,);
Encodingencoding;
if((byteData))
{
encoding=;
{
{
else
{
encoding=;
}
StringdecodedData;
decodedData=(byteData);returndecodedData;
}
internalvirtualDecodeResultdecode(QRCodeImageqrCodelmage,Pointadjust)
try
{
if(numTryDecode==0)
("Decodingstarted");
int[][]intImage=imageTolntArray(qrCodelmage);
imageReader=newQRCodeImageReader();qrCodeSymbol=(intlmage);
}
else
{
("--");
("Decodingrestarted#"+(numTryDecode));
qrCodeSymbol=(adjust);
catch(SymbolNotFoundExceptione)
{
thrownewDecodingFailedException();
}
("CreatedQRCodesymbol.");
("Readingsymbol.");
("Version:"+);("Maskpattern:"+);//blockscontainsall(dataandRS)blocksinQRCodesymbolint[]blocks=;
("Correctingdataerrors.");
//nowblocksturntodatablocks(correctedandextractedfromoriginalblocks)blocks=correctDataBlocks(blocks);
try
{
sbyte[]decodedByteArray=getDecodedByteArray(blocks,,);
returnnewDecodeResult(this,decodedByteArray,numLastCorrections,correctionSucceeded);
}
catch(InvalidDataBlockExceptione)
();
thrownewDecodingFailedException();
{
{
{
internalvirtualint[][]imageToIntArray(QRCodeImageimage){
intwidth=;
intheight=;
int[][]intImage=newint[width][];
for(inti=0;i<width;i++){
intlmage[i]=newint[height];
}
for(inty=0;y<height;y++)
{
for(intx=0;x<width;x++)
{
intlmage[x][y]=(x,y);
}
}
returnintlmage;
internalvirtualint[]correctDataBlocks(int[]blocks)
{
intnumCorrections=0;
intdataCapacity=;
int[]dataBlocks=newint[dataCapacity];
intnumErrorCollectionCode=;
intnumRSBlocks=;
inteccPerRSBlock=numErrorCollectionCode/numRSBlocks;
if(numRSBlocks==1)
{
ReedSolomoncorrector=newReedSolomon(blocks,eccPerRSBlock);
();
numCorrections+=;
if(numCorrections>0)
((numCorrections)+"dataerrorscorrected.");else
("Noerrorsfound.");
numLastCorrections=numCorrections;
correctionSucceeded=;
returnblocks;
}
else
{
//wehavetointerleavedatablocksbecausesymbolhas2ormoreRSblocks
intnumLongerRSBlocks=dataCapacity%numRSBlocks;
if(numLongerRSBlocks==0)
//symbolhasonly1typeofRSblock
intlengthRSBlock=dataCapacity/numRSBlocks;int[][]tmpArray=newint[numRSBlocks][];
for(inti=0;i<numRSBlocks;i++)
{
{
{
{
tmpArray[i]=newint[lengthRSBlock];
}
int[][]RSBlocks=tmpArray;
//obtainRSblocksfor(inti=0;i<numRSBlocks;i++)
for(intj=0;j<lengthRSBlock;j++)
{
RSBlocks[i][j]=blocks[j*numRSBlocks+i];
}
ReedSolomoncorrector=newReedSolomon(RSBlocks[i],eccPerRSBlock);();
numCorrections+=;
correctionSucceeded=;
}
//obtainonlydatapart
intp=0;
for(inti=0;i<numRSBlocks;i++)
for(intj=0;j<lengthRSBlock-eccPerRSBlock;j++){
dataBlocks[p++]=RSBlocks[i][j];
}
}
else
//symbolhas2typesofRSblocks
intlengthShorterRSBlock=dataCapacity/numRSBlocks;
intlengthLongerRSBlock=dataCapacity/numRSBlocks+1;
intnumShorterRSBlocks=numRSBlocks-numLongerRSBlocks;int[][]tmpArray2=newint[numShorterRSBlocks][];
for(inti2=0;i2<numShorterRSBlocks;i2++)
{
tmpArray2[i2]=newint[lengthShorterRSBlock];
int[][]shorterRSBlocks=tmpArray2;
int[][]tmpArray3=newint[numLongerRSBlocks][];for(inti3=0;i3<numLongerRSBlocks;i3++){
tmpArray3[i3]=newint[lengthLongerRSBlock];
int[][]longerRSBlocks=tmpArray3;for(inti=0;i<numRSBlocks;i++)
}
if(i<numShorterRSBlocks)
{
//getshorterRSBlock(s)intmod=0;
for(intj=0;j<lengthShorterRSBlock;j++){
if(j==lengthShorterRSBlock-eccPerRSBlock)mod=numLongerRSBlocks;
shorterRSBlocks[i][j]=blocks[j*numRSBlocks+i+mod];
ReedSolomoncorrector=newReedSolomon(shorterRSBlocks[i],eccPerRSBlock);();
numCorrections+=;
correctionSucceeded=;
else
//getlongerRSBlocksintmod=0;
for(intj=0;j<lengthLongerRSBlock;j++){
if(j==lengthShorterRSBlock-eccPerRSBlock)mod=numShorterRSBlocks;
longerRSBlocks[i-numShorterRSBlocks][j]=blocks[j*numRSBlocks+i-mod];
ReedSolomoncorrector=newReedSolomon(longerRSBlocks[i-numShorterRSBlocks],eccPerRSBlock);();
numCorrections+=;
correctionSucceeded=;
}
0;i<numRSBlocks;i++)
intp=0;for(inti:{
if(i<numShorterRSBlocks)
{
for(intj=0;j<lengthShorterRSBlock-eccPerRSBlock;j++){
dataBlocks[p++]=shorterRSBlocks[i][j];
}
}
else
for(intj=0;j<lengthLongerRSBlock-eccPerRSBlock;j++)
{
dataBlocks[p++]=longerRSBlocks[i-numShorterRSBlocks][j];
}
}
if(numCorrections>0)
((numCorrections)+"dataerrorscorrected.");
else
("Noerrorsfound.");numLastCorrections=numCorrections;returndataBlocks;
}
internalvirtualsbyte[]getDecodedByteArray(int[]blocks,intversion,intnumErrorCorrectionCode)
{
sbyte[]byteArray;
{
}
QRCodeDataBlockReaderreader=newQRCodeDataBlockReader(blocks,version,numErrorCorrectionCode);
try
{
byteArray=;
}
catch(InvalidDataBlockExceptione)
{
throwe;
}
returnbyteArray;
}
internalvirtualStringgetDecodedString(int[]blocks,intversion,intnumErrorCorrectionCode)
{
StringdataString=null;
QRCodeDataBlockReaderreader=newQRCodeDataBlockReader(blocks,version,numErrorCorrectionCode);
try
{
dataString=;
catch(){
thrownewInvalidDataBlockException();
returndataString;
}
{
二维码识别源代码 来自淘豆网m.daumloan.com转载请标明出处.