在QR码前添加徽标

时间:2015-05-09 11:54:15

标签: android zxing qr-code

我使用 zxing library 为Android制作QR码生成器。该应用程序工作正常。但是,我需要在生成的 QR码的中心添加徽标​​。我读过a tutorial from this web,但它并不接近我正在寻找的东西。

以下是我的代码示例:

private void generateQRCode_general(String data, ImageView img) throws WriterException {

    com.google.zxing.MultiFormatWriter writer = new MultiFormatWriter();

    String finaldata = Uri.encode(data, "utf-8");

    BitMatrix bm = writer.encode(finaldata, BarcodeFormat.CODE_128, 150, 150);
    Bitmap ImageBitmap = Bitmap.createBitmap(180, 40, Config.ARGB_8888);

    for (int i = 0; i < 180; i++) {//width
        for (int j = 0; j < 40; j++) {//height
            ImageBitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK : Color.WHITE);
        }
    }

    if (ImageBitmap != null) {
        qrcode.setImageBitmap(ImageBitmap);
    } else {
        Toast.makeText(getApplicationContext(), getResources().getString(R.string.userInputError),
                Toast.LENGTH_SHORT).show();
    }
}

1 个答案:

答案 0 :(得分:0)

要在另一个图像上添加图像,您可以使用此方法:

public Bitmap combineImages(Bitmap top, Bitmap bottom) 
{ 
    Bitmap combined = null; 

    int width, height = 0; 

    if(top.getWidth() > bottom.getWidth()) { 
      width = top.getWidth() + bottom.getWidth(); 
      height = top.getHeight(); 
    } else { 
      width = bottom.getWidth() + bottom.getWidth(); 
      height = top.getHeight(); 
    } 

    combined = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

    Canvas comboImage = new Canvas(combined); 

    comboImage.drawBitmap(top, 0f, 0f, null); 
    comboImage.drawBitmap(bottom, top.getWidth(), 0f, null); 

    return combined; 
}  

这将两个位图合并在一起,你可以使用一个新的结果位图。 Source(还有很多其他来源)

如果您不想要新的位图而只需要图像高于另一个图像,则可以使用<RelativeLayout>并将两个<ImageView>放入其中。

作为附加说明,我假设您还必须检查图像是否与QR码所需的点重叠,这是一个不同的主题。

相关问题