使用ZXing库生成二维码
-
-
-
-
-
-
-
-
-
-
public Bitmap createCode(String string,Bitmap mBitmap, BarcodeFormat format)
-
throws WriterException {
-
Matrix m = new Matrix();
-
float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth();
-
float sy = (float) 2 * IMAGE_HALFWIDTH
-
/ mBitmap.getHeight();
-
m.setScale(sx, sy);
-
-
mBitmap = Bitmap.createBitmap(mBitmap, 0, 0,
-
mBitmap.getWidth(), mBitmap.getHeight(), m, false);
-
MultiFormatWriter writer = new MultiFormatWriter();
-
Hashtable<EncodeHintType, String> hst = new Hashtable<EncodeHintType, String>();
-
hst.put(EncodeHintType.CHARACTER_SET, "UTF-8");
-
BitMatrix matrix = writer.encode(string, format, 400, 400, hst);
-
int width = matrix.getWidth();
-
int height = matrix.getHeight();
-
int halfW = width / 2;
-
int halfH = height / 2;
-
int[] pixels = new int[width * height];
-
for (int y = 0; y < height; y++) {
-
for (int x = 0; x < width; x++) {
-
if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH
-
&& y > halfH - IMAGE_HALFWIDTH
-
&& y < halfH + IMAGE_HALFWIDTH) {
-
-
pixels[y * width + x] = mBitmap.getPixel(x - halfW
-
+ IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH); } else {
-
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;
-
}
原文地址:https://www.cnblogs.com/vegetate/p/9997336.html