bitcoin-atm

bitcoin atm for pyc inc.

git clone https://9o.is/git/bitcoin-atm.git

decoder.js

(2863B)


      1 /*
      2   Ported to JavaScript by Lazar Laszlo 2011 
      3   
      4   lazarsoft@gmail.com, www.lazarsoft.info
      5   
      6 */
      7 
      8 /*
      9 *
     10 * Copyright 2007 ZXing authors
     11 *
     12 * Licensed under the Apache License, Version 2.0 (the "License");
     13 * you may not use this file except in compliance with the License.
     14 * You may obtain a copy of the License at
     15 *
     16 *      http://www.apache.org/licenses/LICENSE-2.0
     17 *
     18 * Unless required by applicable law or agreed to in writing, software
     19 * distributed under the License is distributed on an "AS IS" BASIS,
     20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     21 * See the License for the specific language governing permissions and
     22 * limitations under the License.
     23 */
     24 
     25 
     26 Decoder={};
     27 Decoder.rsDecoder = new ReedSolomonDecoder(GF256.QR_CODE_FIELD);
     28 
     29 Decoder.correctErrors=function( codewordBytes,  numDataCodewords)
     30 {
     31 	var numCodewords = codewordBytes.length;
     32 	// First read into an array of ints
     33 	var codewordsInts = new Array(numCodewords);
     34 	for (var i = 0; i < numCodewords; i++)
     35 	{
     36 		codewordsInts[i] = codewordBytes[i] & 0xFF;
     37 	}
     38 	var numECCodewords = codewordBytes.length - numDataCodewords;
     39 	try
     40 	{
     41 		Decoder.rsDecoder.decode(codewordsInts, numECCodewords);
     42 		//var corrector = new ReedSolomon(codewordsInts, numECCodewords);
     43 		//corrector.correct();
     44 	}
     45 	catch ( rse)
     46 	{
     47 		throw rse;
     48 	}
     49 	// Copy back into array of bytes -- only need to worry about the bytes that were data
     50 	// We don't care about errors in the error-correction codewords
     51 	for (var i = 0; i < numDataCodewords; i++)
     52 	{
     53 		codewordBytes[i] =  codewordsInts[i];
     54 	}
     55 }
     56 
     57 Decoder.decode=function(bits)
     58 {
     59 	var parser = new BitMatrixParser(bits);
     60 	var version = parser.readVersion();
     61 	var ecLevel = parser.readFormatInformation().ErrorCorrectionLevel;
     62 	
     63 	// Read codewords
     64 	var codewords = parser.readCodewords();
     65 
     66 	// Separate into data blocks
     67 	var dataBlocks = DataBlock.getDataBlocks(codewords, version, ecLevel);
     68 	
     69 	// Count total number of data bytes
     70 	var totalBytes = 0;
     71 	for (var i = 0; i < dataBlocks.length; i++)
     72 	{
     73 		totalBytes += dataBlocks[i].NumDataCodewords;
     74 	}
     75 	var resultBytes = new Array(totalBytes);
     76 	var resultOffset = 0;
     77 	
     78 	// Error-correct and copy data blocks together into a stream of bytes
     79 	for (var j = 0; j < dataBlocks.length; j++)
     80 	{
     81 		var dataBlock = dataBlocks[j];
     82 		var codewordBytes = dataBlock.Codewords;
     83 		var numDataCodewords = dataBlock.NumDataCodewords;
     84 		Decoder.correctErrors(codewordBytes, numDataCodewords);
     85 		for (var i = 0; i < numDataCodewords; i++)
     86 		{
     87 			resultBytes[resultOffset++] = codewordBytes[i];
     88 		}
     89 	}
     90 	
     91 	// Decode the contents of that stream of bytes
     92 	var reader = new QRCodeDataBlockReader(resultBytes, version.VersionNumber, ecLevel.Bits);
     93 	return reader;
     94 	//return DecodedBitStreamParser.decode(resultBytes, version, ecLevel);
     95 }