2015-05-09 39 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代碼,這是一個不同的主題則需要點。