Java Code Examples for com.google.zxing.qrcode.encoder.ByteMatrix#getWidth()

The following examples show how to use com.google.zxing.qrcode.encoder.ByteMatrix#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: DataMatrixWriter.java    From barcodescanner-lib-aar with MIT License 6 votes vote down vote up
/**
 * Convert the ByteMatrix to BitMatrix.
 *
 * @param matrix The input matrix.
 * @return The output matrix.
 */
private static BitMatrix convertByteMatrixToBitMatrix(ByteMatrix matrix) {
  int matrixWidgth = matrix.getWidth();
  int matrixHeight = matrix.getHeight();

  BitMatrix output = new BitMatrix(matrixWidgth, matrixHeight);
  output.clear();
  for (int i = 0; i < matrixWidgth; i++) {
    for (int j = 0; j < matrixHeight; j++) {
      // Zero is white in the bytematrix
      if (matrix.get(i, j) == 1) {
        output.set(i, j);
      }
    }
  }

  return output;
}
 
Example 2
Source File: UI.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
public static Bitmap getQRCode(final String data) {
    final ByteMatrix matrix;
    try {
        matrix = Encoder.encode(data, ErrorCorrectionLevel.M).getMatrix();
    } catch (final WriterException e) {
        throw new RuntimeException(e);
    }

    final int height = matrix.getHeight() * SCALE;
    final int width = matrix.getWidth() * SCALE;
    final int min = height < width ? height : width;

    final Bitmap mQRCode = Bitmap.createBitmap(min, min, Bitmap.Config.ARGB_8888);
    for (int x = 0; x < min; ++x)
        for (int y = 0; y < min; ++y)
            mQRCode.setPixel(x, y, matrix.get(x / SCALE, y / SCALE) == 1 ? Color.BLACK : Color.TRANSPARENT);
    return mQRCode;
}
 
Example 3
Source File: QrBitmap.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
public Bitmap getQRCode() {
    if (mQRCode == null) {
        final ByteMatrix matrix;
        try {
            matrix = Encoder.encode(mData, ErrorCorrectionLevel.M).getMatrix();
        } catch (final WriterException e) {
            throw new RuntimeException(e);
        }
        final int height = matrix.getHeight() * SCALE;
        final int width = matrix.getWidth() * SCALE;
        mQRCode = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        for (int x = 0; x < width; ++x)
            for (int y = 0; y < height; ++y)
                mQRCode.setPixel(x, y, matrix.get(x / SCALE, y / SCALE) == 1 ? Color.BLACK : mBackgroundColor);
    }
    return mQRCode;
}
 
Example 4
Source File: DataMatrixWriter.java    From weex with Apache License 2.0 6 votes vote down vote up
/**
 * Convert the ByteMatrix to BitMatrix.
 *
 * @param matrix The input matrix.
 * @return The output matrix.
 */
private static BitMatrix convertByteMatrixToBitMatrix(ByteMatrix matrix) {
  int matrixWidgth = matrix.getWidth();
  int matrixHeight = matrix.getHeight();

  BitMatrix output = new BitMatrix(matrixWidgth, matrixHeight);
  output.clear();
  for (int i = 0; i < matrixWidgth; i++) {
    for (int j = 0; j < matrixHeight; j++) {
      // Zero is white in the bytematrix
      if (matrix.get(i, j) == 1) {
        output.set(i, j);
      }
    }
  }

  return output;
}
 
Example 5
Source File: DataMatrixWriter.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 6 votes vote down vote up
/**
 * Convert the ByteMatrix to BitMatrix.
 *
 * @param matrix The input matrix.
 * @return The output matrix.
 */
private static BitMatrix convertByteMatrixToBitMatrix(ByteMatrix matrix) {
  int matrixWidgth = matrix.getWidth();
  int matrixHeight = matrix.getHeight();

  BitMatrix output = new BitMatrix(matrixWidgth, matrixHeight);
  output.clear();
  for (int i = 0; i < matrixWidgth; i++) {
    for (int j = 0; j < matrixHeight; j++) {
      // Zero is white in the bytematrix
      if (matrix.get(i, j) == 1) {
        output.set(i, j);
      }
    }
  }

  return output;
}
 
Example 6
Source File: DataMatrixWriter.java    From ZXing-Orient with Apache License 2.0 6 votes vote down vote up
/**
 * Convert the ByteMatrix to BitMatrix.
 *
 * @param matrix The input matrix.
 * @return The output matrix.
 */
private static BitMatrix convertByteMatrixToBitMatrix(ByteMatrix matrix) {
  int matrixWidgth = matrix.getWidth();
  int matrixHeight = matrix.getHeight();

  BitMatrix output = new BitMatrix(matrixWidgth, matrixHeight);
  output.clear();
  for (int i = 0; i < matrixWidgth; i++) {
    for (int j = 0; j < matrixHeight; j++) {
      // Zero is white in the bytematrix
      if (matrix.get(i, j) == 1) {
        output.set(i, j);
      }
    }
  }

  return output;
}
 
Example 7
Source File: QRCodeWriter.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
private static BitMatrix a(QRCode qrcode, int i, int j)
{
    ByteMatrix bytematrix = qrcode.getMatrix();
    if (bytematrix == null)
    {
        throw new IllegalStateException();
    }
    int k = bytematrix.getWidth();
    int l = bytematrix.getHeight();
    int i1 = k + 8;
    int j1 = l + 8;
    int k1 = Math.max(i, i1);
    int l1 = Math.max(j, j1);
    int i2 = Math.min(k1 / i1, l1 / j1);
    int j2 = (k1 - k * i2) / 2;
    int k2 = (l1 - l * i2) / 2;
    BitMatrix bitmatrix = new BitMatrix(k1, l1);
    int l2 = k2;
    int l3;
    for (int i3 = 0; i3 < l; i3 = l3)
    {
        int j3 = j2;
        for (int k3 = 0; k3 < k;)
        {
            if (bytematrix.get(k3, i3) == 1)
            {
                bitmatrix.setRegion(j3, l2, i2, i2);
            }
            k3++;
            j3 += i2;
        }

        l3 = i3 + 1;
        l2 += i2;
    }

    return bitmatrix;
}
 
Example 8
Source File: QRCodeWriter.java    From reacteu-app with MIT License 5 votes vote down vote up
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {
  ByteMatrix input = code.getMatrix();
  if (input == null) {
    throw new IllegalStateException();
  }
  int inputWidth = input.getWidth();
  int inputHeight = input.getHeight();
  int qrWidth = inputWidth + (quietZone << 1);
  int qrHeight = inputHeight + (quietZone << 1);
  int outputWidth = Math.max(width, qrWidth);
  int outputHeight = Math.max(height, qrHeight);

  int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
  // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  // handle all the padding from 100x100 (the actual QR) up to 200x160.
  int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  int topPadding = (outputHeight - (inputHeight * multiple)) / 2;

  BitMatrix output = new BitMatrix(outputWidth, outputHeight);

  for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
    // Write the contents of this row of the barcode
    for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
      if (input.get(inputX, inputY) == 1) {
        output.setRegion(outputX, outputY, multiple, multiple);
      }
    }
  }

  return output;
}
 
Example 9
Source File: QRCodeWriter.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {
	ByteMatrix input = code.getMatrix();
	if (input == null) {
		throw new IllegalStateException();
	}
	int inputWidth = input.getWidth();
	int inputHeight = input.getHeight();
	int qrWidth = inputWidth + (quietZone * 2);
	int qrHeight = inputHeight + (quietZone * 2);
	int outputWidth = Math.max(width, qrWidth);
	int outputHeight = Math.max(height, qrHeight);

	int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
	// Padding includes both the quiet zone and the extra white pixels to
	// accommodate the requested
	// dimensions. For example, if input is 25x25 the QR will be 33x33
	// including the quiet zone.
	// If the requested size is 200x160, the multiple will be 4, for a QR of
	// 132x132. These will
	// handle all the padding from 100x100 (the actual QR) up to 200x160.
	int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
	int topPadding = (outputHeight - (inputHeight * multiple)) / 2;

	BitMatrix output = new BitMatrix(outputWidth, outputHeight);

	for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
		// Write the contents of this row of the barcode
		for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
			if (input.get(inputX, inputY) == 1) {
				output.setRegion(outputX, outputY, multiple, multiple);
			}
		}
	}

	return output;
}
 
Example 10
Source File: QRCodeWriter.java    From weex with Apache License 2.0 5 votes vote down vote up
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {
  ByteMatrix input = code.getMatrix();
  if (input == null) {
    throw new IllegalStateException();
  }
  int inputWidth = input.getWidth();
  int inputHeight = input.getHeight();
  int qrWidth = inputWidth + (quietZone * 2);
  int qrHeight = inputHeight + (quietZone * 2);
  int outputWidth = Math.max(width, qrWidth);
  int outputHeight = Math.max(height, qrHeight);

  int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
  // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  // handle all the padding from 100x100 (the actual QR) up to 200x160.
  int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  int topPadding = (outputHeight - (inputHeight * multiple)) / 2;

  BitMatrix output = new BitMatrix(outputWidth, outputHeight);

  for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
    // Write the contents of this row of the barcode
    for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
      if (input.get(inputX, inputY) == 1) {
        output.setRegion(outputX, outputY, multiple, multiple);
      }
    }
  }

  return output;
}
 
Example 11
Source File: QRCodeWriter.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 5 votes vote down vote up
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {
  ByteMatrix input = code.getMatrix();
  if (input == null) {
    throw new IllegalStateException();
  }
  int inputWidth = input.getWidth();
  int inputHeight = input.getHeight();
  int qrWidth = inputWidth + (quietZone * 2);
  int qrHeight = inputHeight + (quietZone * 2);
  int outputWidth = Math.max(width, qrWidth);
  int outputHeight = Math.max(height, qrHeight);

  int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
  // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  // handle all the padding from 100x100 (the actual QR) up to 200x160.
  int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  int topPadding = (outputHeight - (inputHeight * multiple)) / 2;

  BitMatrix output = new BitMatrix(outputWidth, outputHeight);

  for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
    // Write the contents of this row of the barcode
    for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
      if (input.get(inputX, inputY) == 1) {
        output.setRegion(outputX, outputY, multiple, multiple);
      }
    }
  }

  return output;
}
 
Example 12
Source File: QRCodeWriter.java    From ZXing-Orient with Apache License 2.0 5 votes vote down vote up
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {
  ByteMatrix input = code.getMatrix();
  if (input == null) {
    throw new IllegalStateException();
  }
  int inputWidth = input.getWidth();
  int inputHeight = input.getHeight();
  int qrWidth = inputWidth + (quietZone * 2);
  int qrHeight = inputHeight + (quietZone * 2);
  int outputWidth = Math.max(width, qrWidth);
  int outputHeight = Math.max(height, qrHeight);

  int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
  // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  // handle all the padding from 100x100 (the actual QR) up to 200x160.
  int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  int topPadding = (outputHeight - (inputHeight * multiple)) / 2;

  BitMatrix output = new BitMatrix(outputWidth, outputHeight);

  for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
    // Write the contents of this row of the barcode
    for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
      if (input.get(inputX, inputY) == 1) {
        output.setRegion(outputX, outputY, multiple, multiple);
      }
    }
  }

  return output;
}
 
Example 13
Source File: QRCodeWriter.java    From QrCodeScanner with GNU General Public License v3.0 5 votes vote down vote up
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {
  ByteMatrix input = code.getMatrix();
  if (input == null) {
    throw new IllegalStateException();
  }
  int inputWidth = input.getWidth();
  int inputHeight = input.getHeight();
  int qrWidth = inputWidth + (quietZone * 2);
  int qrHeight = inputHeight + (quietZone * 2);
  int outputWidth = Math.max(width, qrWidth);
  int outputHeight = Math.max(height, qrHeight);

  int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
  // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  // handle all the padding from 100x100 (the actual QR) up to 200x160.
  int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  int topPadding = (outputHeight - (inputHeight * multiple)) / 2;

  BitMatrix output = new BitMatrix(outputWidth, outputHeight);

  for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
    // Write the contents of this row of the barcode
    for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
      if (input.get(inputX, inputY) == 1) {
        output.setRegion(outputX, outputY, multiple, multiple);
      }
    }
  }

  return output;
}
 
Example 14
Source File: QRCodeWriter.java    From Tesseract-OCR-Scanner with Apache License 2.0 5 votes vote down vote up
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {
  ByteMatrix input = code.getMatrix();
  if (input == null) {
    throw new IllegalStateException();
  }
  int inputWidth = input.getWidth();
  int inputHeight = input.getHeight();
  int qrWidth = inputWidth + (quietZone * 2);
  int qrHeight = inputHeight + (quietZone * 2);
  int outputWidth = Math.max(width, qrWidth);
  int outputHeight = Math.max(height, qrHeight);

  int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
  // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  // handle all the padding from 100x100 (the actual QR) up to 200x160.
  int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  int topPadding = (outputHeight - (inputHeight * multiple)) / 2;

  BitMatrix output = new BitMatrix(outputWidth, outputHeight);

  for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
    // Write the contents of this row of the barcode
    for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
      if (input.get(inputX, inputY) == 1) {
        output.setRegion(outputX, outputY, multiple, multiple);
      }
    }
  }

  return output;
}
 
Example 15
Source File: QRCodeWriter.java    From letv with Apache License 2.0 5 votes vote down vote up
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {
    ByteMatrix input = code.getMatrix();
    if (input == null) {
        throw new IllegalStateException();
    }
    int inputWidth = input.getWidth();
    int inputHeight = input.getHeight();
    int qrWidth = inputWidth + (quietZone << 1);
    int qrHeight = inputHeight + (quietZone << 1);
    int outputWidth = Math.max(width, qrWidth);
    int outputHeight = Math.max(height, qrHeight);
    int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
    int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
    int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
    BitMatrix output = new BitMatrix(outputWidth, outputHeight);
    int inputY = 0;
    int outputY = topPadding;
    while (inputY < inputHeight) {
        int inputX = 0;
        int outputX = leftPadding;
        while (inputX < inputWidth) {
            if (input.get(inputX, inputY) == (byte) 1) {
                output.setRegion(outputX, outputY, multiple, multiple);
            }
            inputX++;
            outputX += multiple;
        }
        inputY++;
        outputY += multiple;
    }
    return output;
}
 
Example 16
Source File: QRCodeWriter.java    From ScreenCapture with MIT License 5 votes vote down vote up
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {
  ByteMatrix input = code.getMatrix();
  if (input == null) {
    throw new IllegalStateException();
  }
  int inputWidth = input.getWidth();
  int inputHeight = input.getHeight();
  int qrWidth = inputWidth + (quietZone * 2);
  int qrHeight = inputHeight + (quietZone * 2);
  int outputWidth = Math.max(width, qrWidth);
  int outputHeight = Math.max(height, qrHeight);

  int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
  // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  // handle all the padding from 100x100 (the actual QR) up to 200x160.
  int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  int topPadding = (outputHeight - (inputHeight * multiple)) / 2;

  BitMatrix output = new BitMatrix(outputWidth, outputHeight);

  for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
    // Write the contents of this row of the barcode
    for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
      if (input.get(inputX, inputY) == 1) {
        output.setRegion(outputX, outputY, multiple, multiple);
      }
    }
  }

  return output;
}
 
Example 17
Source File: QrBitmap.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
public Bitmap getQRCode() throws Exception {
    if (mQRCode == null) {
        final ByteMatrix matrix;
        matrix = Encoder.encode(mData, ErrorCorrectionLevel.M).getMatrix();
        final int height = matrix.getHeight() * SCALE;
        final int width = matrix.getWidth() * SCALE;
        mQRCode = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        for (int x = 0; x < width; ++x)
            for (int y = 0; y < height; ++y)
                mQRCode.setPixel(x, y, matrix.get(x / SCALE, y / SCALE) == 1 ? Color.BLACK : mBackgroundColor);
    }
    return mQRCode;
}
 
Example 18
Source File: QRCodeWriter.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public BitMatrix renderResult(int width, int height, int quietZone) {
    ByteMatrix input = code.getMatrix();
    if (input == null) {
        throw new IllegalStateException();
    }
    int inputWidth = input.getWidth();
    int inputHeight = input.getHeight();
    int qrWidth = inputWidth + (quietZone * 2);
    int qrHeight = inputHeight + (quietZone * 2);
    int outputWidth = Math.max(width, qrWidth);
    int outputHeight = Math.max(height, qrHeight);

    int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
    // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
    // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
    // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
    // handle all the padding from 100x100 (the actual QR) up to 200x160.
    leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
    topPadding = (outputHeight - (inputHeight * multiple)) / 2;

    BitMatrix output = new BitMatrix(outputWidth, outputHeight);

    for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
        // Write the contents of this row of the barcode
        for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
            if (input.get(inputX, inputY) == 1) {
                output.setRegion(outputX, outputY, multiple, multiple);
            }
        }
    }

    return output;
}
 
Example 19
Source File: QRCodeWriter.java    From ShareBox with Apache License 2.0 4 votes vote down vote up
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {
    ByteMatrix input = code.getMatrix();
    if (input == null) {
        throw new IllegalStateException();
    }
    int inputWidth = input.getWidth();
    int inputHeight = input.getHeight();
    int qrWidth = inputWidth;
    int qrHeight = inputHeight;
    int outputWidth = Math.max(width, qrWidth);
    int outputHeight = Math.max(height, qrHeight);

    int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
    // Padding includes both the quiet zone and the extra white pixels to
    // accommodate the requested
    // dimensions. For example, if input is 25x25 the QR will be 33x33
    // including the quiet zone.
    // If the requested size is 200x160, the multiple will be 4, for a QR of
    // 132x132. These will
    // handle all the padding from 100x100 (the actual QR) up to 200x160.

    int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
    int topPadding = (outputHeight - (inputHeight * multiple)) / 2;

    if (leftPadding >= 0) {
        outputWidth = outputWidth - 2 * leftPadding;
        leftPadding = 0;
    }
    if (topPadding >= 0) {
        outputHeight = outputHeight - 2 * topPadding;
        topPadding = 0;
    }

    BitMatrix output = new BitMatrix(outputWidth, outputHeight);

    for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
        // Write the contents of this row of the barcode
        for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
            if (input.get(inputX, inputY) == 1) {
                output.setRegion(outputX, outputY, multiple, multiple);
            }
        }
    }

    return output;
}
 
Example 20
Source File: PrinterCommands.java    From PrinterThermal-ESCPOS-Android with MIT License 4 votes vote down vote up
/**
 * Convert a string to QR Code byte array compatible with ESC/POS printer.
 *
 * @param data String data to convert in QR Code
 * @return Bytes contain the image in ESC/POS command
 */
public static byte[] QRCodeDataToBytes(String data, int size) {

    ByteMatrix byteMatrix = null;

    try {
        EnumMap<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        QRCode code = Encoder.encode(data, ErrorCorrectionLevel.L, hints);
        byteMatrix = code.getMatrix();

    } catch (WriterException e) {
        e.printStackTrace();
    }

    if (byteMatrix == null) {
        return PrinterCommands.initImageCommand(0, 0);
    }

    int
            width = byteMatrix.getWidth(),
            height = byteMatrix.getHeight(),
            coefficient = Math.round((float) size / (float) width),
            imageWidth = width * coefficient,
            imageHeight = height * coefficient,
            bytesByLine = (int) Math.ceil(((float) imageWidth) / 8f),
            i = 8;

    if (coefficient < 1) {
        return PrinterCommands.initImageCommand(0, 0);
    }

    byte[] imageBytes = PrinterCommands.initImageCommand(bytesByLine, imageHeight);

    for (int y = 0; y < height; y++) {
        byte[] lineBytes = new byte[bytesByLine];
        int j = 0, multipleX = coefficient;
        boolean isBlack = false;
        for (int x = -1; x < width;) {
            StringBuilder stringBinary = new StringBuilder();
            for (int k = 0; k < 8; k++) {
                if(multipleX == coefficient) {
                    isBlack = ++x < width && byteMatrix.get(x, y) == 1;
                    multipleX = 0;
                }
                stringBinary.append(isBlack ? "1" : "0");
                ++multipleX;
            }
            lineBytes[j++] = (byte) Integer.parseInt(stringBinary.toString(), 2);
        }

        for (int multipleY = 0; multipleY < coefficient; ++multipleY) {
            System.arraycopy(lineBytes, 0, imageBytes, i, lineBytes.length);
            i += lineBytes.length;
        }
    }

    return imageBytes;
}