1、二维码识别源代码QRCode Decodeusing System;using System.Text;using QRCodeImage = ThoughtWorks.QRCode.Codec.Data.QRCodeImage;using QRCodeSymbol = ThoughtWorks.QRCode.Codec.Data.QRCodeSymbol;using ReedSolomon = ThoughtWorks.QRCode.Codec.Ecc.ReedSolomon;using DecodingFailedException = ThoughtWorks.QRCode.Except
2、ionHandler.DecodingFailedException;using InvalidDataBlockException = ThoughtWorks.QRCode.ExceptionHandler.InvalidDataBlockException;using SymbolNotFoundException = ThoughtWorks.QRCode.ExceptionHandler.SymbolNotFoundException;using Point = ThoughtWorks.QRCode.Geom.Point;using QRCodeDataBlockReader =
3、ThoughtWorks.QRCode.Codec.Reader.QRCodeDataBlockReader;using QRCodeImageReader = ThoughtWorks.QRCode.Codec.Reader.QRCodeImageReader;using DebugCanvas = ThoughtWorks.QRCode.Codec.Util.DebugCanvas;using DebugCanvasAdapter = ThoughtWorks.QRCode.Codec.Util.DebugCanvasAdapter;using QRCodeUtility = Though
4、tWorks.QRCode.Codec.Util.QRCodeUtility;namespace ThoughtWorks.QRCode.Codec public class QRCodeDecoder internal QRCodeSymbol qrCodeSymbol; internal int numTryDecode; internal System.Collections.ArrayList results; internal System.Collections.ArrayList lastResults = System.Collections.ArrayList.Synchro
5、nized(new System.Collections.ArrayList(10); internal static DebugCanvas canvas; internal QRCodeImageReader imageReader; internal int numLastCorrections; internal bool correctionSucceeded; public static DebugCanvas Canvas get return QRCodeDecoder.canvas; set QRCodeDecoder.canvas = value; virtual inte
6、rnal Point AdjustPoints get / note that adjusts affect dependently / i.e. below means (0,0), (2,3), (3,4), (1,2), (2,1), (1,1), (-1,-1) / Point adjusts = new Point(0,0), new Point(2,3), new Point(1,1), / new Point(-2,-2), new Point(1,-1), new Point(-1,0), new Point(-2,-2); System.Collections.ArrayLi
7、st adjustPoints = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10); for (int d = 0; d - 4; y-) for (int x = 0; x - 4; x-) if (x != y & (x + y) % 2 = 0) adjustPoints.Add(new Point(x - lastX, y - lastY); lastX = x; lastY = y; Point adjusts = new PointadjustPoints.Count; f
8、or (int i = 0; i adjusts.Length; i+) adjustsi = (Point) adjustPointsi; return adjusts; internal class DecodeResult internal int numCorrections; internal bool correctionSucceeded; internal sbyte decodedBytes; private QRCodeDecoder enclosingInstance; public DecodeResult(QRCodeDecoder enclosingInstance
9、, sbyte decodedBytes, int numErrors, bool correctionSucceeded) InitBlock(enclosingInstance); this.decodedBytes = decodedBytes; this.numCorrections = numErrors; this.correctionSucceeded = correctionSucceeded; private void InitBlock(QRCodeDecoder enclosingInstance) this.enclosingInstance = enclosingIn
10、stance; virtual public sbyte DecodedBytes get return decodedBytes; virtual public int NumErrors get return numCorrections; virtual public bool CorrectionSucceeded get return correctionSucceeded; public QRCodeDecoder Enclosing_Instance get return enclosingInstance; public QRCodeDecoder() numTryDecode
11、 = 0; results = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10); QRCodeDecoder.canvas = new DebugCanvasAdapter(); /* public byte decode(QRCodeImage qrCodeImage) throws DecodingFailedException canvas.println(Decoding started.); int intImage = imageToIntArray(qrCodeImage
12、); try QRCodeImageReader reader = new QRCodeImageReader(); qrCodeSymbol = reader.getQRCodeSymbol(intImage); catch (SymbolNotFoundException e) throw new DecodingFailedException(e.getMessage(); canvas.println(Created QRCode symbol.); canvas.println(Reading symbol.); canvas.println(Version: + qrCodeSym
13、bol.getVersionReference(); canvas.println(Mask pattern: + qrCodeSymbol.getMaskPatternRefererAsString(); int blocks = qrCodeSymbol.getBlocks(); canvas.println(Correcting data errors.); int dataBlocks = correctDataBlocks(blocks); try byte decodedByteArray = getDecodedByteArray(dataBlocks, qrCodeSymbol
14、.getVersion(); canvas.println(Decoding finished.); return decodedByteArray; catch (InvalidDataBlockException e) throw new DecodingFailedException(e.getMessage(); */ public virtual sbyte decodeBytes(QRCodeImage qrCodeImage) Point adjusts = AdjustPoints; System.Collections.ArrayList results = System.C
15、ollections.ArrayList.Synchronized(new System.Collections.ArrayList(10); while (numTryDecode = 0) throw dfe; finally numTryDecode += 1; if (results.Count = 0) throw new DecodingFailedException(Give up decoding); int lowestErrorIndex = - 1; int lowestError = System.Int32.MaxValue; for (int i = 0; i re
16、sults.Count; i+) DecodeResult result = (DecodeResult) resultsi; if (result.NumErrors lowestError) lowestError = result.NumErrors; lowestErrorIndex = i; canvas.println(All trials need for correct error); canvas.println(Reporting # + (lowestErrorIndex) + that,); canvas.println(corrected minimum errors
17、 ( + lowestError + ); canvas.println(Decoding finished.); return (DecodeResult) resultslowestErrorIndex).DecodedBytes; public virtual String decode(QRCodeImage qrCodeImage, Encoding encoding) sbyte data = decodeBytes(qrCodeImage); byte byteData = new bytedata.Length; Buffer.BlockCopy(data, 0, byteDa
18、ta, 0, byteData.Length); /* char decodedData = new chardata.Length; for (int i = 0; i data.Length; i+) decodedDatai = Convert.to(datai); return new String(decodedData); */ String decodedData; decodedData = encoding.GetString(byteData); return decodedData; public virtual String decode(QRCodeImage qrC
19、odeImage) sbyte data = decodeBytes(qrCodeImage); byte byteData = new bytedata.Length; Buffer.BlockCopy(data, 0, byteData, 0, byteData.Length); Encoding encoding; if (QRCodeUtility.IsUnicode(byteData) encoding = Encoding.Unicode; else encoding = Encoding.ASCII; String decodedData; decodedData = encod
20、ing.GetString(byteData); return decodedData; internal virtual DecodeResult decode(QRCodeImage qrCodeImage, Point adjust) try if (numTryDecode = 0) canvas.println(Decoding started); int intImage = imageToIntArray(qrCodeImage); imageReader = new QRCodeImageReader(); qrCodeSymbol = imageReader.getQRCod
21、eSymbol(intImage); else canvas.println(-); canvas.println(Decoding restarted # + (numTryDecode); qrCodeSymbol = imageReader.getQRCodeSymbolWithAdjustedGrid(adjust); catch (SymbolNotFoundException e) throw new DecodingFailedException(e.Message); canvas.println(Created QRCode symbol.); canvas.println(
22、Reading symbol.); canvas.println(Version: + qrCodeSymbol.VersionReference); canvas.println(Mask pattern: + qrCodeSymbol.MaskPatternRefererAsString); / blocks contains all (data and RS) blocks in QR Code symbol int blocks = qrCodeSymbol.Blocks; canvas.println(Correcting data errors.); / now blocks tu
23、rn to data blocks (corrected and extracted from original blocks) blocks = correctDataBlocks(blocks); try sbyte decodedByteArray = getDecodedByteArray(blocks, qrCodeSymbol.Version, qrCodeSymbol.NumErrorCollectionCode); return new DecodeResult(this, decodedByteArray, numLastCorrections, correctionSucc
24、eeded); catch (InvalidDataBlockException e) canvas.println(e.Message); throw new DecodingFailedException(e.Message); internal virtual int imageToIntArray(QRCodeImage image) int width = image.Width; int height = image.Height; int intImage = new intwidth; for (int i = 0; i width; i+) intImagei = new intheight; for (int y = 0; y height; y+) for (int x = 0; x 0) canvas.println(System.Convert.ToStri
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2