You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.5 KiB
44 lines
1.5 KiB
package com.github.catvod.utils;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import com.google.zxing.BarcodeFormat;
|
|
import com.google.zxing.EncodeHintType;
|
|
import com.google.zxing.MultiFormatWriter;
|
|
import com.google.zxing.common.BitMatrix;
|
|
|
|
import java.util.EnumMap;
|
|
import java.util.Map;
|
|
|
|
public class QRCode {
|
|
|
|
private static final int WHITE = 0xFFFFFFFF;
|
|
private static final int BLACK = 0xFF000000;
|
|
|
|
public static 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;
|
|
}
|
|
|
|
public static Bitmap getBitmap(String contents, int size, int margin) {
|
|
try {
|
|
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
|
|
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
|
hints.put(EncodeHintType.MARGIN, margin);
|
|
return createBitmap(new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, Misc.dp2px(size), Misc.dp2px(size), hints));
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|