本文整理汇总了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;
}