Beispiel #1
0
        /*	public byte[] decode(QRCodeImage qrCodeImage) throws DecodingFailedException{
         *  canvas.println("Decoding started.");
         *  int[][] intImage = imageToIntArray(qrCodeImage);
         *  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: " + qrCodeSymbol.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.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.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
            while (numTryDecode < adjusts.Length)
            {
                try
                {
                    DecodeResult result = decode(qrCodeImage, adjusts[numTryDecode]);
                    if (result.CorrectionSucceeded)
                    {
                        return(result.DecodedBytes);
                    }
                    else
                    {
                        results.Add(result);
                        canvas.println("Decoding succeeded but could not correct");
                        canvas.println("all errors. Retrying..");
                    }
                }
                catch (DecodingFailedException dfe)
                {
                    if (dfe.Message.IndexOf("Finder Pattern") >= 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 < results.Count; i++)
            {
                DecodeResult result = (DecodeResult)results[i];
                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 (" + lowestError + ")");

            canvas.println("Decoding finished.");
            return(((DecodeResult)results[lowestErrorIndex]).DecodedBytes);
        }
Beispiel #2
0
        internal virtual int[][] imageToIntArray(QRCodeImage image)
        {
            int width  = image.Width;
            int height = image.Height;

            int[][] intImage = new int[width][];
            for (int i = 0; i < width; i++)
            {
                intImage[i] = new int[height];
            }
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    intImage[x][y] = image.getPixel(x, y);
                }
            }
            return(intImage);
        }
Beispiel #3
0
 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.getQRCodeSymbol(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("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 turn 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, correctionSucceeded));
     }
     catch (InvalidDataBlockException e)
     {
         canvas.println(e.Message);
         throw new DecodingFailedException(e.Message);
     }
 }
Beispiel #4
0
        public virtual String decode(QRCodeImage qrCodeImage)
        {
            sbyte[] data     = decodeBytes(qrCodeImage);
            byte[]  byteData = new byte[data.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 = encoding.GetString(byteData);
            return(decodedData);
        }
Beispiel #5
0
        public virtual String decode(QRCodeImage qrCodeImage, Encoding encoding)
        {
            sbyte[] data     = decodeBytes(qrCodeImage);
            byte[]  byteData = new byte[data.Length];

            Buffer.BlockCopy(data, 0, byteData, 0, byteData.Length);

            /*
             * char[] decodedData = new char[data.Length];
             * for (int i = 0; i < data.Length; i++)
             * {
             *  decodedData[i] = Convert.to(data[i]);
             *
             * }
             * return new String(decodedData);
             */
            String decodedData;

            decodedData = encoding.GetString(byteData);
            return(decodedData);
        }
 internal virtual int[][] imageToIntArray(QRCodeImage image)
 {
     int width = image.Width;
     int height = image.Height;
     int[][] intImage = new int[width][];
     for (int i = 0; i < width; i++)
     {
         intImage[i] = new int[height];
     }
     for (int y = 0; y < height; y++)
     {
         for (int x = 0; x < width; x++)
         {
             intImage[x][y] = image.GetPixel(x, y);
         }
     }
     return intImage;
 }
 internal virtual DecodeResult Decode(QRCodeImage qrCodeImage, Point adjust)
 {
     try
     {
         if (numTryDecode == 0)
         {
             canvas.Print("Decoding started");
             int[][] intImage = imageToIntArray(qrCodeImage);
             imageReader = new QRCodeImageReader();
             qrCodeSymbol = imageReader.GetQRCodeSymbol(intImage);
         }
         else
         {
             canvas.Print("--");
             canvas.Print("Decoding restarted #" + (numTryDecode));
             qrCodeSymbol = imageReader.GetQRCodeSymbolWithAdjustedGrid(adjust);
         }
     }
     catch (SymbolNotFoundException e)
     {
         throw new DecodingFailedException(e.Message);
     }
     canvas.Print("Created QRCode symbol.");
     canvas.Print("Reading symbol.");
     canvas.Print("Version: " + qrCodeSymbol.VersionReference);
     canvas.Print("Mask pattern: " + qrCodeSymbol.MaskPatternRefererAsString);
     // blocks contains all (data and RS) blocks in QR Code symbol
     int[] blocks = qrCodeSymbol.Blocks;
     canvas.Print("Correcting data errors.");
     // now blocks turn 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, numLastCorrectionFailures);
     }
     catch (InvalidDataBlockException e)
     {
         canvas.Print(e.Message);
         throw new DecodingFailedException(e.Message);
     }
 }
        /*	public byte[] decode(QRCodeImage qrCodeImage) throws DecodingFailedException{
            canvas.println("Decoding started.");
            int[][] intImage = imageToIntArray(qrCodeImage);
            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: " + qrCodeSymbol.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.getVersion());
            canvas.println("Decoding finished.");
            return decodedByteArray;
            } catch (InvalidDataBlockException e) {
            throw new DecodingFailedException(e.getMessage());
            }
        }*/
        public virtual sbyte[] DecodeBytes(QRCodeImage qrCodeImage)
        {
            Point[] adjusts = AdjustPoints;
            ArrayList results = ArrayList.Synchronized(new ArrayList(10));
            numTryDecode = 0;
            while (numTryDecode < adjusts.Length)
            {
                try
                {
                    DecodeResult result = Decode(qrCodeImage, adjusts[numTryDecode]);
                    if (result.IsCorrectionSucceeded)
                    {
                        return result.DecodedBytes;
                    }
                    else
                    {
                        results.Add(result);
                        canvas.Print("Decoding succeeded but could not correct");
                        canvas.Print("all errors. Retrying..");
                    }

                }
                catch (DecodingFailedException dfe)
                {
                    if (dfe.Message.IndexOf("Finder Pattern") >= 0)
                    throw dfe;
                }
                finally
                {
                    numTryDecode += 1;
                }
            }

            if (results.Count == 0)
                throw new DecodingFailedException("Give up decoding");

            int minErrorIndex = -1;
            int minError = Int32.MaxValue;

            for (int i = 0; i < results.Count; i++)
            {
                DecodeResult result = (DecodeResult) results[i];

                if (result.NumCorrectionFailures < minError) {
                    minError = result.NumCorrectionFailures;
                    minErrorIndex = i;
                }
            }

            canvas.Print("All trials need for correct error");
            canvas.Print("Reporting #" + (minErrorIndex)+" that,");
            canvas.Print("corrected minimum errors (" +minError + ")");

            canvas.Print("Decoding finished.");
            return ((DecodeResult)results[minErrorIndex]).DecodedBytes;
        }
        public virtual String Decode(QRCodeImage qrCodeImage)
        {
            sbyte[] data = DecodeBytes(qrCodeImage);
            byte[] byteData = new byte[data.Length];
            Buffer.BlockCopy(data, 0, byteData, 0, byteData.Length);

            Encoding encoding = Encoding.GetEncoding(StringHelper.GuessEncoding(byteData));

            /*
            if (QRCodeHelper.IsUnicode(byteData))
            {
                encoding = Encoding.Unicode;
            }
            else
            {
                encoding = Encoding.UTF8;
            }
            */

            String decodedData;
            decodedData = encoding.GetString(byteData);
            return decodedData;
        }
        public virtual string Decode(QRCodeImage qrCodeImage, Encoding encoding)
        {
            sbyte[] data = DecodeBytes(qrCodeImage);
            byte[] byteData = new byte[data.Length];

            Buffer.BlockCopy(data, 0, byteData, 0, byteData.Length);
            /*
            char[] decodedData = new char[data.Length];
            for (int i = 0; i < data.Length; i++)
            {
                decodedData[i] = Convert.to(data[i]);

            }
            return new String(decodedData);
            */
            String decodedData;
            decodedData = encoding.GetString(byteData);
            return decodedData;
        }
Beispiel #11
0
        public virtual String decode(QRCodeImage qrCodeImage)
        {
            sbyte[] data = decodeBytes(qrCodeImage);
            byte[] byteData = new byte[data.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 = encoding.GetString(byteData);
            return decodedData;
        }