2015-05-09 69 views
0

我正在制作一个QR码生成器,使用zxing库为Android。该应用程序工作得很好。但是,我需要在生成的QR代码的中心添加徽标​​。我已阅读a tutorial from this web,但它并不接近我正在寻找的东西。在QR码前添加Logo

这里是我的代码示例:

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(); 
    } 
} 
+0

你可以添加一些更多的细节。也许它应该是什么样子。添加一些截图。 – mikepenz

+0

我知道你的repu是不够的上传图片,你可以使用http://tinypic.com/添加图片,并在这里添加链接 –

+0

看看这个项目https://github.com/skrymer/qrbuilder –

回答

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代码,这是一个不同的主题则需要点。