本文整理匯總了Java中com.google.zxing.common.BitMatrix類的典型用法代碼示例。如果您正苦於以下問題:Java BitMatrix類的具體用法?Java BitMatrix怎麽用?Java BitMatrix使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
BitMatrix類屬於com.google.zxing.common包,在下文中一共展示了BitMatrix類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: syncEncodeQRCode
import com.google.zxing.common.BitMatrix; //導入依賴的package包/類
/**
* 同步創建指定前景色、指定背景色、帶logo的二維碼圖片。該方法是耗時操作,請在子線程中調用。
*
* @param content 要生成的二維碼圖片內容
* @param size 圖片寬高,單位為px
* @param foregroundColor 二維碼圖片的前景色
* @param backgroundColor 二維碼圖片的背景色
* @param logo 二維碼圖片的logo
*/
public static Bitmap syncEncodeQRCode(String content, int size, int foregroundColor, int backgroundColor, Bitmap logo) {
try {
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, HINTS);
int[] pixels = new int[size * size];
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (matrix.get(x, y)) {
pixels[y * size + x] = foregroundColor;
} else {
pixels[y * size + x] = backgroundColor;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
return addLogoToQRCode(bitmap, logo);
} catch (Exception e) {
return null;
}
}
示例2: Create2DCode
import com.google.zxing.common.BitMatrix; //導入依賴的package包/類
/**
* 傳入字符串生成二維碼
*
* @param str
* @return
* @throws WriterException
*/
public static Bitmap Create2DCode(String str) throws WriterException {
// 生成二維矩陣,編碼時指定大小,不要生成了圖片以後再進行縮放,這樣會模糊導致識別失敗
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, 300, 300);
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] = 0xff000000;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
// 通過像素數組生成bitmap,具體參考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
示例3: create
import com.google.zxing.common.BitMatrix; //導入依賴的package包/類
public static @NonNull Bitmap create(String data) {
try {
BitMatrix result = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, 512, 512);
Bitmap bitmap = Bitmap.createBitmap(result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888);
for (int y = 0; y < result.getHeight(); y++) {
for (int x = 0; x < result.getWidth(); x++) {
if (result.get(x, y)) {
bitmap.setPixel(x, y, Color.BLACK);
}
}
}
return bitmap;
} catch (WriterException e) {
Log.w(TAG, e);
return Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
}
}
示例4: encodeAsBitmap
import com.google.zxing.common.BitMatrix; //導入依賴的package包/類
public Bitmap encodeAsBitmap() throws WriterException {
if (!encoded) return null;
Map<EncodeHintType, Object> hints = null;
String encoding = guessAppropriateEncoding(contents);
hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
if (encoding != null) {
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
hints.put(EncodeHintType.MARGIN, 2); /* default = 4 */
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;
}
示例5: createQRCode
import com.google.zxing.common.BitMatrix; //導入依賴的package包/類
/**
* 生成二維碼
*
* @param content 二維碼內容
* @param size 二維碼內容大小
* @param color 二維碼顏色
*/
public static Bitmap createQRCode(String content, int size,int bgColor, int color) {
try {
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, CHARACTER_SET);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints);
int[] pixels = new int[size * size];
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * size + x] = color;
} else {
pixels[y * size + x] = bgColor;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
return bitmap;
} catch (WriterException e) {
return null;
}
}
示例6: encode
import com.google.zxing.common.BitMatrix; //導入依賴的package包/類
/**
* 生成條形二維碼
*/
public static byte[] encode(String contents, int width, int height, String imgPath) {
int codeWidth = 3 + // start guard
(7 * 6) + // left bars
5 + // middle guard
(7 * 6) + // right bars
3; // end guard
codeWidth = Math.max(codeWidth, width);
byte[] buf = null;
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
BarcodeFormat.CODE_128, codeWidth, height, null);
BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, FORMAT_NAME, out);
out.close();
buf = out.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return buf;
}
示例7: createQRCode
import com.google.zxing.common.BitMatrix; //導入依賴的package包/類
public static Bitmap createQRCode(String content, int widthAndHeight) {
if (TextUtils.isEmpty(content)) return null;
try {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//Fault tolerance level. MAX
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//Set the width of the blank margin.
hints.put(EncodeHintType.MARGIN, 0);
BitMatrix matrix = new MultiFormatWriter().encode(content, 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] = 0xff000000;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
示例8: createQRCode
import com.google.zxing.common.BitMatrix; //導入依賴的package包/類
/**
* Generate a two-dimensional code image
*
* @param content An incoming string, usually a URL
* @param widthAndHeight widthAndHeight
* @param openErrorCorrectionLevel Does fault tolerance open?
* @return A two-dimensional code image [Bitmap]
*/
public static Bitmap createQRCode(String content, int widthAndHeight, boolean openErrorCorrectionLevel) {
try {
if (TextUtils.isEmpty(content) || TextUtils.equals("null", content) || "".equals(content)) {
return null;
}
//Processing Chinese characters, if you do not need to change the source code, convert the string into ISO-8859-1 code.
Map hints = openErrorCorrectionLevel(openErrorCorrectionLevel);
BitMatrix matrix = new MultiFormatWriter().encode(new String(content.getBytes("UTF-8"), "ISO-8859-1"), BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, hints);
matrix = updateBit(matrix, 8);
Bitmap bitmap = generateQRBitmap(matrix);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例9: endcode
import com.google.zxing.common.BitMatrix; //導入依賴的package包/類
private Bitmap endcode(String input) {
BarcodeFormat format = BarcodeFormat.QR_CODE;
EnumMap<EncodeHintType, Object> hint = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix result = null;
try {
result = new MultiFormatWriter().encode(input, BarcodeFormat.QR_CODE, 500, 500, hint);
} catch (WriterException e) {
e.printStackTrace();
}
int w = result.getWidth();
int h = result.getHeight();
int[] pixels = new int[w * h];
for (int y = 0; y < h; y++) {
int offset = y * w;
for (int x = 0; x < w; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bit = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
bit.setPixels(pixels, 0, w, 0, 0, w, h);
ImageView imageView = (ImageView) findViewById(R.id.qr_code_id);
imageView.setImageBitmap(bit);
return bit;
}
示例10: createQrCode
import com.google.zxing.common.BitMatrix; //導入依賴的package包/類
@Nullable
static Bitmap createQrCode(DisplayMetrics dm, String input) {
int smallestDimen = Math.min(dm.widthPixels, dm.heightPixels);
try {
// Generate QR code
final BitMatrix encoded = new QRCodeWriter().encode(
input, QR_CODE, smallestDimen, smallestDimen);
// Convert QR code to Bitmap
int width = encoded.getWidth();
int height = encoded.getHeight();
int[] pixels = new int[width * height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
pixels[y * width + x] = encoded.get(x, y) ? BLACK : WHITE;
}
}
Bitmap qr = Bitmap.createBitmap(width, height, ARGB_8888);
qr.setPixels(pixels, 0, width, 0, 0, width, height);
return qr;
} catch (WriterException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
return null;
}
}
示例11: moduleSize
import com.google.zxing.common.BitMatrix; //導入依賴的package包/類
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;
}
示例12: drawBullsEye
import com.google.zxing.common.BitMatrix; //導入依賴的package包/類
private static void drawBullsEye(BitMatrix matrix, int center, int size) {
for (int i = 0; i < size; i += 2) {
for (int j = center - i; j <= center + i; j++) {
matrix.set(j, center - i);
matrix.set(j, center + i);
matrix.set(center - i, j);
matrix.set(center + i, j);
}
}
matrix.set(center - size, center - size);
matrix.set(center - size + 1, center - size);
matrix.set(center - size, center - size + 1);
matrix.set(center + size, center - size);
matrix.set(center + size, center - size + 1);
matrix.set(center + size, center + size - 1);
}
示例13: decode
import com.google.zxing.common.BitMatrix; //導入依賴的package包/類
@Override
public Result decode(BinaryBitmap image, Map<DecodeHintType,?> hints)
throws NotFoundException, ChecksumException, FormatException {
DecoderResult decoderResult;
if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
BitMatrix bits = extractPureBits(image.getBlackMatrix());
decoderResult = decoder.decode(bits, hints);
} else {
throw NotFoundException.getNotFoundInstance();
}
ResultPoint[] points = NO_POINTS;
Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.MAXICODE);
String ecLevel = decoderResult.getECLevel();
if (ecLevel != null) {
result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, ecLevel);
}
return result;
}
示例14: convertByteMatrixToBitMatrix
import com.google.zxing.common.BitMatrix; //導入依賴的package包/類
/**
* 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;
}
示例15: createQRCode
import com.google.zxing.common.BitMatrix; //導入依賴的package包/類
public static Bitmap createQRCode(String str, int size) 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, size, size);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for(int x = 0; x < width; x ++){
for(int y = 0; y < height; y ++){
if(matrix.get(x, y)){
pixels[y * width + x] = BLACK;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}