2 * Copyright 2007 ZXing authors
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
16 namespace com.google.zxing.qrcode
\r
19 using System.Collections;
\r
20 using com.google.zxing.common;
\r
21 using com.google.zxing.qrcode.decoder;
\r
22 using com.google.zxing.qrcode.detector;
\r
24 public sealed class QRCodeReader: Reader
\r
26 private static ResultPoint[] NO_POINTS = new ResultPoint[0];
\r
27 private Decoder decoder = new Decoder();
\r
30 * Locates and decodes a QR code in an image.
\r
32 * @return a String representing the content encoded by the QR code
\r
33 * @throws ReaderException if a QR code cannot be found, or cannot be decoded
\r
35 public Result decode(MonochromeBitmapSource image) {
\r
37 return decode(image, null);
\r
40 throw new ReaderException(e.Message);
\r
45 public Result decode(MonochromeBitmapSource image, Hashtable hints){
\r
47 DecoderResult decoderResult;
\r
48 ResultPoint[] points;
\r
49 if (hints != null && hints.ContainsKey(DecodeHintType.PURE_BARCODE)) {
\r
50 BitMatrix bits = extractPureBits(image);
\r
51 decoderResult = decoder.decode(bits);
\r
54 DetectorResult detectorResult = new Detector(image).detect(hints);
\r
55 decoderResult = decoder.decode(detectorResult.getBits());
\r
56 points = detectorResult.getPoints();
\r
59 Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.QR_CODE);
\r
60 if (decoderResult.getByteSegments() != null) {
\r
61 result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments());
\r
64 }catch(Exception e){
\r
65 throw new ReaderException(e.Message);
\r
71 * This method detects a barcode in a "pure" image -- that is, pure monochrome image
\r
72 * which contains only an unrotated, unskewed, image of a barcode, with some white border
\r
73 * around it. This is a specialized method that works exceptionally fast in this special
\r
76 private static BitMatrix extractPureBits(MonochromeBitmapSource image){
\r
77 // Now need to determine module size in pixels
\r
79 int height = image.getHeight();
\r
80 int width = image.getWidth();
\r
81 int minDimension = Math.Min(height, width);
\r
83 // First, skip white border by tracking diagonally from the top left down and to the right:
\r
84 int borderWidth = 0;
\r
85 while (borderWidth < minDimension && !image.isBlack(borderWidth, borderWidth)) {
\r
88 if (borderWidth == minDimension) {
\r
89 throw new ReaderException();
\r
92 // And then keep tracking across the top-left black module to determine module size
\r
93 int moduleEnd = borderWidth;
\r
94 while (moduleEnd < minDimension && image.isBlack(moduleEnd, moduleEnd)) {
\r
97 if (moduleEnd == minDimension) {
\r
98 throw new ReaderException();
\r
101 int moduleSize = moduleEnd - borderWidth;
\r
103 // And now find where the rightmost black module on the first row ends
\r
104 int rowEndOfSymbol = width - 1;
\r
105 while (rowEndOfSymbol >= 0 && !image.isBlack(rowEndOfSymbol, borderWidth)) {
\r
108 if (rowEndOfSymbol < 0) {
\r
109 throw new ReaderException();
\r
113 // Make sure width of barcode is a multiple of module size
\r
114 if ((rowEndOfSymbol - borderWidth) % moduleSize != 0) {
\r
115 throw new ReaderException();
\r
117 int dimension = (rowEndOfSymbol - borderWidth) / moduleSize;
\r
119 // Push in the "border" by half the module width so that we start
\r
120 // sampling in the middle of the module. Just in case the image is a
\r
121 // little off, this will help recover.
\r
122 borderWidth += moduleSize >> 1;
\r
124 int sampleDimension = borderWidth + (dimension - 1) * moduleSize;
\r
125 if (sampleDimension >= width || sampleDimension >= height) {
\r
126 throw new ReaderException();
\r
129 // Now just read off the bits
\r
130 BitMatrix bits = new BitMatrix(dimension);
\r
131 for (int i = 0; i < dimension; i++) {
\r
132 int iOffset = borderWidth + i * moduleSize;
\r
133 for (int j = 0; j < dimension; j++) {
\r
134 if (image.isBlack(borderWidth + j * moduleSize, iOffset)) {
\r