我在中心看到很少有带公司徽标的二维码.是否可以在Android中生成带有任何徽标的QR码?如果可能,请详细解释这样做的方式.目前我正在使用Zxing生成QR码.
小智 9
这是一个技巧,实际上你的 QR 码生成器(如果你使用 zxing)返回一个位图值,因此你可以将QR 码位图与徽标位图合并合并,这是一个示例:
首先,您必须有 void 来合并两个位图并调整徽标大小,以便可以将其放置在 QR 码的中心
public Bitmap mergeBitmaps(Bitmap logo, Bitmap qrcode) {
Bitmap combined = Bitmap.createBitmap(qrcode.getWidth(), qrcode.getHeight(), qrcode.getConfig());
Canvas canvas = new Canvas(combined);
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();
canvas.drawBitmap(qrcode, new Matrix(), null);
Bitmap resizeLogo = Bitmap.createScaledBitmap(logo, canvasWidth / 5, canvasHeight / 5, true);
int centreX = (canvasWidth - resizeLogo.getWidth()) /2;
int centreY = (canvasHeight - resizeLogo.getHeight()) / 2;
canvas.drawBitmap(resizeLogo, centreX, centreY, null);
return combined;
}
Run Code Online (Sandbox Code Playgroud)
然后,您使用该空白来合并 QR 码位图和徽标位图,并将其推送到图像视图中
Bitmap yourLogo = BitmapFactory.decodeResource(getResources(), R.drawable.your_logo);
Bitmap merge = mergeBitmaps(yourLogo, qrcode_bitmap);
yourImageView.setImageBitmap(merge);
Run Code Online (Sandbox Code Playgroud)
参考生成带徽标的qr代码时提供的指南和源代码, 请找到我用来在Android上获得类似结果的示例Android代码.
我确信这个代码可以通过使用Paint类来优化,特别是关于图像叠加不透明度,但是这个代码在这方面有效.
/**
* Writes the given Overlay on a new Bitmap object.
* @param Bitmap the Bitmap to overlay.
* @return the new {@link Bitmap}-object.
*/
public static Bitmap overlayBitmap(Bitmap overlay) {
BitMatrix matrix = null;
QRCodeWriter writer = new QRCodeWriter();
//Error correction
//Sometimes your QRCode will get damaged or covered up by something – like an image overlay for instance –
//therefore the designers of the QRCode has added four levels; 7% (L), 15 % (M), 25% (Q), 30% (H) of error
//correction were a error correction of level H should result in a QRCode that are still valid even when it’s
//30% obscured – for more info on error correction check this
Map<EncodeHintType, Object> hints;
hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//create qr code matrix
writer = new QRCodeWriter();
try {
matrix = writer.encode(redirectUrl,
BarcodeFormat.QR_CODE,
QRCODE_IMAGE_WIDTH,
QRCODE_IMAGE_HEIGHT,
hints);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap image = toBitmap(matrix);
int height = image.getHeight();
int width = image.getWidth();
Bitmap combined = Bitmap.createBitmap(width, height, image.getConfig());
Canvas canvas = new Canvas(combined);
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();
canvas.drawBitmap(image, new Matrix(), null);
int centreX = (canvasWidth - overlay.getWidth()) /2;
int centreY = (canvasHeight - overlay.getHeight()) /2 ;
//http://stackoverflow.com/a/12235235/1635441
//http://stackoverflow.com/a/5119093/1635441
//Paint p = new Paint();
//p.setXfermode(new PorterDuffXfermode(Mode.DST_ATOP)); //http://stackoverflow.com/a/17553502/1635441
//p.setAlpha(180);
//p.setARGB(a, r, g, b);
//canvas.drawBitmap(bitmapToBeOverlay, 0, 0, p);
//canvas.drawBitmap(overlay, new Matrix(), null);
canvas.drawBitmap(overlay, centreX, centreY, null);
return combined;
}
/**
* Writes the given Matrix to a new colour Bitmap object.
* @param matrix the matrix to write.
* @param Color the Color to be added.
* @return the new {@link Bitmap}-object.
*/
public static Bitmap toBitmapColour(BitMatrix matrix, int colour){
int height = matrix.getHeight();
int width = matrix.getWidth();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++){
for (int y = 0; y < height; y++){
bmp.setPixel(x, y, matrix.get(x,y) ? colour : Color.WHITE);
}
}
return bmp;
}
Run Code Online (Sandbox Code Playgroud)
HTH
| 归档时间: |
|
| 查看次数: |
9023 次 |
| 最近记录: |