Java Code Examples for com.google.zxing.common.BitMatrix#getWidth()
The following examples show how to use
com.google.zxing.common.BitMatrix#getWidth() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QRCodeReader.java From analyzer-of-android-for-Apache-Weex with Apache License 2.0 | 6 votes |
private static float moduleSize(int[] leftTopBlack, BitMatrix image) throws NotFoundException { int height = image.getHeight(); int width = image.getWidth(); int x = leftTopBlack[0]; int y = leftTopBlack[1]; boolean inBlack = true; int transitions = 0; while (x < width && y < height) { if (inBlack != image.get(x, y)) { if (++transitions == 5) { break; } inBlack = !inBlack; } x++; y++; } if (x == width || y == height) { throw NotFoundException.getNotFoundInstance(); } return (x - leftTopBlack[0]) / 7.0f; }
Example 2
Source File: QREncoder.java From Lunary-Ethereum-Wallet with GNU General Public License v3.0 | 6 votes |
public Bitmap encodeAsBitmap() throws WriterException { if (!encoded) return null; Map<EncodeHintType, Object> hints = null; String encoding = guessAppropriateEncoding(contents); if (encoding != null) { hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, encoding); } MultiFormatWriter writer = new MultiFormatWriter(); BitMatrix result = writer.encode(contents, format, dimension, dimension, hints); int width = result.getWidth(); int height = result.getHeight(); int[] pixels = new int[width * height]; // All are 0, or black, by default for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
Example 3
Source File: EncodingHandler.java From QrCodeDemo4 with MIT License | 6 votes |
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight); int width = matrix.getWidth(); int height = matrix.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (matrix.get(x, y)) { pixels[y * width + x] = BLACK; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
Example 4
Source File: EncodingHandler.java From vinci with Apache License 2.0 | 6 votes |
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight); int width = matrix.getWidth(); int height = matrix.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (matrix.get(x, y)) { pixels[y * width + x] = BLACK; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
Example 5
Source File: QRCodeRasterizedImageProducer.java From jasperreports with GNU Lesser General Public License v3.0 | 6 votes |
public BufferedImage getImage(BitMatrix matrix, Color onColor) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); int onArgb = JRColorUtil.getOpaqueArgb(onColor, Color.BLACK);//not actually opaque for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { if (matrix.get(x, y)) { image.setRGB(x, y, onArgb); } } } return image; }
Example 6
Source File: QRCodeReader.java From Telegram with GNU General Public License v2.0 | 6 votes |
private static float moduleSize(int[] leftTopBlack, BitMatrix image) throws NotFoundException { int height = image.getHeight(); int width = image.getWidth(); int x = leftTopBlack[0]; int y = leftTopBlack[1]; boolean inBlack = true; int transitions = 0; while (x < width && y < height) { if (inBlack != image.get(x, y)) { if (++transitions == 5) { break; } inBlack = !inBlack; } x++; y++; } if (x == width || y == height) { throw NotFoundException.getNotFoundInstance(); } return (x - leftTopBlack[0]) / 7.0f; }
Example 7
Source File: WhiteRectangleDetector.java From weex with Apache License 2.0 | 5 votes |
/** * @param image barcode image to find a rectangle in * @param initSize initial size of search area around center * @param x x position of search center * @param y y position of search center * @throws NotFoundException if image is too small to accommodate {@code initSize} */ public WhiteRectangleDetector(BitMatrix image, int initSize, int x, int y) throws NotFoundException { this.image = image; height = image.getHeight(); width = image.getWidth(); int halfsize = initSize / 2; leftInit = x - halfsize; rightInit = x + halfsize; upInit = y - halfsize; downInit = y + halfsize; if (upInit < 0 || leftInit < 0 || downInit >= height || rightInit >= width) { throw NotFoundException.getNotFoundInstance(); } }
Example 8
Source File: QRCodeEncoder.java From Zom-Android-XMPP with GNU General Public License v3.0 | 5 votes |
public Bitmap encodeAsBitmap() throws WriterException { if (!encoded) return null; Map<EncodeHintType, Object> hints = null; String encoding = guessAppropriateEncoding(contents); if (encoding != null) { hints = new EnumMap<>(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, encoding); } MultiFormatWriter writer = new MultiFormatWriter(); BitMatrix result = writer.encode(contents, format, dimension, dimension, hints); int width = result.getWidth(); int height = result.getHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); int[] pixels = new int[width * height]; // All are 0, or black, by default for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; //bitmap.setPixel(x,y,result.get(x, y) ? BLACK : WHITE); } } bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
Example 9
Source File: QRCodeEncoder.java From zxingfragmentlib with Apache License 2.0 | 5 votes |
Bitmap encodeAsBitmap() throws WriterException { String contentsToEncode = contents; if (contentsToEncode == null) { return null; } Map<EncodeHintType,Object> hints = null; String encoding = guessAppropriateEncoding(contentsToEncode); if (encoding != null) { hints = new EnumMap<>(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, encoding); } BitMatrix result; try { result = new MultiFormatWriter().encode(contentsToEncode, format, dimension, dimension, hints); } catch (IllegalArgumentException iae) { // Unsupported format return null; } int width = result.getWidth(); int height = result.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
Example 10
Source File: QRCodeEncoder.java From android-quick-response-code with Apache License 2.0 | 5 votes |
public Bitmap encodeAsBitmap() throws WriterException { if (!encoded) return null; Map<EncodeHintType, Object> hints = null; String encoding = guessAppropriateEncoding(contents); if (encoding != null) { hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, encoding); } MultiFormatWriter writer = new MultiFormatWriter(); BitMatrix result = writer.encode(contents, format, dimension, dimension, hints); int width = result.getWidth(); int height = result.getHeight(); int[] pixels = new int[width * height]; // All are 0, or black, by default for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
Example 11
Source File: BarcodeEncoder.java From Viewer with Apache License 2.0 | 5 votes |
public Bitmap createBitmap(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
Example 12
Source File: MatrixToImageWriter.java From qart4j with GNU General Public License v3.0 | 5 votes |
/** * As {@link #toBufferedImage(BitMatrix)}, but allows customization of the output. * * @param matrix {@link BitMatrix} to write * @param config output configuration * @return {@link BufferedImage} representation of the input */ public static BufferedImage toBufferedImage(BitMatrix matrix, MatrixToImageConfig config) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, config.getBufferedImageColorModel()); int onColor = config.getPixelOnColor(); int offColor = config.getPixelOffColor(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? onColor : offColor); } } return image; }
Example 13
Source File: BytesUtil.java From react-native-sunmi-inner-printer with MIT License | 5 votes |
private static byte getBitMatrixColor(BitMatrix bits, int x, int y){ int width = bits.getWidth(); int height = bits.getHeight(); if( x >= width || y >= height || x < 0 || y < 0)return 0; if(bits.get(x, y)){ return 1; }else{ return 0; } }
Example 14
Source File: FinderPatternFinder.java From QrCodeScanner with GNU General Public License v3.0 | 4 votes |
/** * <p>Like {@link #crossCheckVertical(int, int, int, int)}, and in fact is basically identical, * except it reads horizontally instead of vertically. This is used to cross-cross * check a vertical cross check and locate the real center of the alignment pattern.</p> */ private float crossCheckHorizontal(int startJ, int centerI, int maxCount, int originalStateCountTotal) { BitMatrix image = this.image; int maxJ = image.getWidth(); int[] stateCount = getCrossCheckStateCount(); int j = startJ; while (j >= 0 && image.get(j, centerI)) { stateCount[2]++; j--; } if (j < 0) { return Float.NaN; } while (j >= 0 && !image.get(j, centerI) && stateCount[1] <= maxCount) { stateCount[1]++; j--; } if (j < 0 || stateCount[1] > maxCount) { return Float.NaN; } while (j >= 0 && image.get(j, centerI) && stateCount[0] <= maxCount) { stateCount[0]++; j--; } if (stateCount[0] > maxCount) { return Float.NaN; } j = startJ + 1; while (j < maxJ && image.get(j, centerI)) { stateCount[2]++; j++; } if (j == maxJ) { return Float.NaN; } while (j < maxJ && !image.get(j, centerI) && stateCount[3] < maxCount) { stateCount[3]++; j++; } if (j == maxJ || stateCount[3] >= maxCount) { return Float.NaN; } while (j < maxJ && image.get(j, centerI) && stateCount[4] < maxCount) { stateCount[4]++; j++; } if (stateCount[4] >= maxCount) { return Float.NaN; } // If we found a finder-pattern-like section, but its size is significantly different than // the original, assume it's a false positive int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4]; if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= originalStateCountTotal) { return Float.NaN; } return foundPatternCross(stateCount) ? centerFromEnd(stateCount, j) : Float.NaN; }
Example 15
Source File: QRCodeReader.java From Telegram-FOSS with GNU General Public License v2.0 | 4 votes |
/** * This method detects a code in a "pure" image -- that is, pure monochrome image * which contains only an unrotated, unskewed, image of a code, with some white border * around it. This is a specialized method that works exceptionally fast in this special * case. */ private static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException { int[] leftTopBlack = image.getTopLeftOnBit(); int[] rightBottomBlack = image.getBottomRightOnBit(); if (leftTopBlack == null || rightBottomBlack == null) { throw NotFoundException.getNotFoundInstance(); } float moduleSize = moduleSize(leftTopBlack, image); int top = leftTopBlack[1]; int bottom = rightBottomBlack[1]; int left = leftTopBlack[0]; int right = rightBottomBlack[0]; // Sanity check! if (left >= right || top >= bottom) { throw NotFoundException.getNotFoundInstance(); } if (bottom - top != right - left) { // Special case, where bottom-right module wasn't black so we found something else in the last row // Assume it's a square, so use height as the width right = left + (bottom - top); if (right >= image.getWidth()) { // Abort if that would not make sense -- off image throw NotFoundException.getNotFoundInstance(); } } int matrixWidth = Math.round((right - left + 1) / moduleSize); int matrixHeight = Math.round((bottom - top + 1) / moduleSize); if (matrixWidth <= 0 || matrixHeight <= 0) { throw NotFoundException.getNotFoundInstance(); } if (matrixHeight != matrixWidth) { // Only possibly decode square regions throw NotFoundException.getNotFoundInstance(); } // Push in the "border" by half the module width so that we start // sampling in the middle of the module. Just in case the image is a // little off, this will help recover. int nudge = (int) (moduleSize / 2.0f); top += nudge; left += nudge; // But careful that this does not sample off the edge // "right" is the farthest-right valid pixel location -- right+1 is not necessarily // This is positive by how much the inner x loop below would be too large int nudgedTooFarRight = left + (int) ((matrixWidth - 1) * moduleSize) - right; if (nudgedTooFarRight > 0) { if (nudgedTooFarRight > nudge) { // Neither way fits; abort throw NotFoundException.getNotFoundInstance(); } left -= nudgedTooFarRight; } // See logic above int nudgedTooFarDown = top + (int) ((matrixHeight - 1) * moduleSize) - bottom; if (nudgedTooFarDown > 0) { if (nudgedTooFarDown > nudge) { // Neither way fits; abort throw NotFoundException.getNotFoundInstance(); } top -= nudgedTooFarDown; } // Now just read off the bits BitMatrix bits = new BitMatrix(matrixWidth, matrixHeight, 1); for (int y = 0; y < matrixHeight; y++) { int iOffset = top + (int) (y * moduleSize); for (int x = 0; x < matrixWidth; x++) { if (image.get(left + (int) (x * moduleSize), iOffset)) { bits.set(x, y); } } } return bits; }
Example 16
Source File: QRCodeReader.java From QrCodeScanner with GNU General Public License v3.0 | 4 votes |
/** * This method detects a code in a "pure" image -- that is, pure monochrome image * which contains only an unrotated, unskewed, image of a code, with some white border * around it. This is a specialized method that works exceptionally fast in this special * case. * * @see com.google.zxing.datamatrix.DataMatrixReader#extractPureBits(BitMatrix) */ private static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException { int[] leftTopBlack = image.getTopLeftOnBit(); int[] rightBottomBlack = image.getBottomRightOnBit(); if (leftTopBlack == null || rightBottomBlack == null) { throw NotFoundException.getNotFoundInstance(); } float moduleSize = moduleSize(leftTopBlack, image); int top = leftTopBlack[1]; int bottom = rightBottomBlack[1]; int left = leftTopBlack[0]; int right = rightBottomBlack[0]; // Sanity check! if (left >= right || top >= bottom) { throw NotFoundException.getNotFoundInstance(); } if (bottom - top != right - left) { // Special case, where bottom-right module wasn't black so we found something else in the last row // Assume it's a square, so use height as the width right = left + (bottom - top); if (right >= image.getWidth()) { // Abort if that would not make sense -- off image throw NotFoundException.getNotFoundInstance(); } } int matrixWidth = Math.round((right - left + 1) / moduleSize); int matrixHeight = Math.round((bottom - top + 1) / moduleSize); if (matrixWidth <= 0 || matrixHeight <= 0) { throw NotFoundException.getNotFoundInstance(); } if (matrixHeight != matrixWidth) { // Only possibly decode square regions throw NotFoundException.getNotFoundInstance(); } // Push in the "border" by half the module width so that we start // sampling in the middle of the module. Just in case the image is a // little off, this will help recover. int nudge = (int) (moduleSize / 2.0f); top += nudge; left += nudge; // But careful that this does not sample off the edge // "right" is the farthest-right valid pixel location -- right+1 is not necessarily // This is positive by how much the inner x loop below would be too large int nudgedTooFarRight = left + (int) ((matrixWidth - 1) * moduleSize) - right; if (nudgedTooFarRight > 0) { if (nudgedTooFarRight > nudge) { // Neither way fits; abort throw NotFoundException.getNotFoundInstance(); } left -= nudgedTooFarRight; } // See logic above int nudgedTooFarDown = top + (int) ((matrixHeight - 1) * moduleSize) - bottom; if (nudgedTooFarDown > 0) { if (nudgedTooFarDown > nudge) { // Neither way fits; abort throw NotFoundException.getNotFoundInstance(); } top -= nudgedTooFarDown; } // Now just read off the bits BitMatrix bits = new BitMatrix(matrixWidth, matrixHeight); for (int y = 0; y < matrixHeight; y++) { int iOffset = top + (int) (y * moduleSize); for (int x = 0; x < matrixWidth; x++) { if (image.get(left + (int) (x * moduleSize), iOffset)) { bits.set(x, y); } } } return bits; }
Example 17
Source File: MultiFinderPatternFinder.java From Telegram-FOSS with GNU General Public License v2.0 | 4 votes |
public FinderPatternInfo[] findMulti(Map<DecodeHintType,?> hints) throws NotFoundException { boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER); BitMatrix image = getImage(); int maxI = image.getHeight(); int maxJ = image.getWidth(); // We are looking for black/white/black/white/black modules in // 1:1:3:1:1 ratio; this tracks the number of such modules seen so far // Let's assume that the maximum version QR Code we support takes up 1/4 the height of the // image, and then account for the center being 3 modules in size. This gives the smallest // number of pixels the center could be, so skip this often. When trying harder, look for all // QR versions regardless of how dense they are. int iSkip = (3 * maxI) / (4 * MAX_MODULES); if (iSkip < MIN_SKIP || tryHarder) { iSkip = MIN_SKIP; } int[] stateCount = new int[5]; for (int i = iSkip - 1; i < maxI; i += iSkip) { // Get a row of black/white values clearCounts(stateCount); int currentState = 0; for (int j = 0; j < maxJ; j++) { if (image.get(j, i)) { // Black pixel if ((currentState & 1) == 1) { // Counting white pixels currentState++; } stateCount[currentState]++; } else { // White pixel if ((currentState & 1) == 0) { // Counting black pixels if (currentState == 4) { // A winner? if (foundPatternCross(stateCount) && handlePossibleCenter(stateCount, i, j)) { // Yes // Clear state to start looking again currentState = 0; clearCounts(stateCount); } else { // No, shift counts back by two shiftCounts2(stateCount); currentState = 3; } } else { stateCount[++currentState]++; } } else { // Counting white pixels stateCount[currentState]++; } } } // for j=... if (foundPatternCross(stateCount)) { handlePossibleCenter(stateCount, i, maxJ); } } // for i=iSkip-1 ... FinderPattern[][] patternInfo = selectMultipleBestPatterns(); List<FinderPatternInfo> result = new ArrayList<>(); for (FinderPattern[] pattern : patternInfo) { ResultPoint.orderBestPatterns(pattern); result.add(new FinderPatternInfo(pattern)); } if (result.isEmpty()) { return EMPTY_RESULT_ARRAY; } else { return result.toArray(EMPTY_RESULT_ARRAY); } }
Example 18
Source File: WhiteRectangleDetector.java From Tesseract-OCR-Scanner with Apache License 2.0 | 4 votes |
public WhiteRectangleDetector(BitMatrix image) throws NotFoundException { this(image, INIT_SIZE, image.getWidth() / 2, image.getHeight() / 2); }
Example 19
Source File: BitMatrixParser.java From ZXing-Orient with Apache License 2.0 | 2 votes |
/** * <p>Creates the version object based on the dimension of the original bit matrix from * the datamatrix code.</p> * * <p>See ISO 16022:2006 Table 7 - ECC 200 symbol attributes</p> * * @param bitMatrix Original {@link BitMatrix} including alignment patterns * @return {@link Version} encapsulating the Data Matrix Code's "version" * @throws FormatException if the dimensions of the mapping matrix are not valid * Data Matrix dimensions. */ private static Version readVersion(BitMatrix bitMatrix) throws FormatException { int numRows = bitMatrix.getHeight(); int numColumns = bitMatrix.getWidth(); return Version.getVersionForDimensions(numRows, numColumns); }
Example 20
Source File: BitMatrixParser.java From RipplePower with Apache License 2.0 | 2 votes |
/** * <p> * Creates the version object based on the dimension of the original bit * matrix from the datamatrix code. * </p> * * <p> * See ISO 16022:2006 Table 7 - ECC 200 symbol attributes * </p> * * @param bitMatrix * Original {@link BitMatrix} including alignment patterns * @return {@link Version} encapsulating the Data Matrix Code's "version" * @throws FormatException * if the dimensions of the mapping matrix are not valid Data * Matrix dimensions. */ private static Version readVersion(BitMatrix bitMatrix) throws FormatException { int numRows = bitMatrix.getHeight(); int numColumns = bitMatrix.getWidth(); return Version.getVersionForDimensions(numRows, numColumns); }