2014-05-22 113 views
2

使用android可以在QR碼圖像的中間添加徽標圖像嗎?如何在中間創建帶有徽標的QR碼

我已經生成了QR碼,但現在我需要的是需要在QRcode中插入徽標圖像。

有什麼辦法可以達到這個目的。

這是我的QR碼生成代碼:

位圖myLogo = BitmapFactory.decodeResource(getResources(),R.drawable.image); 公共無效的onClick(視圖v){

EditText qrInput = (EditText) findViewById(R.id.qrInput); 
String qrInputText = qrInput.getText().toString(); 
Log.v(LOG_TAG, qrInputText); 

//Find screen size 
WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE); 
Display display = manager.getDefaultDisplay(); 
Point point = new Point(); 
// display.getSize(point); 
int width = point.x; 
int height = point.y; 
int smallerDimension = width < height ? width : height; 
smallerDimension = smallerDimension * 3/4; 

//Encode with a QR Code image 
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(qrInputText,null,Contents.Type.TEXT,BarcodeFormat.QR_CODE.toString(),smallerDimension); 

try { 
Bitmap bitmap = qrCodeEncoder.encodeAsBitmap(); 
ImageView myImage = (ImageView) findViewById(R.id.imageView1); 
myImage.setImageBitmap(bitmap); 

} catch (WriterException e) { 
e.printStackTrace(); 
} 

} 

我沒有想過這可能有人請指引我前進一步的想法。

在此先感謝幫助的心。

我這是怎麼實現的:

Bitmap myLogo = BitmapFactory.decodeResource(getResources(), R.drawable.image); 
public void onClick(View v) { 

// switch (v.getId()) { 
// case R.id.button1: 
EditText qrInput = (EditText) findViewById(R.id.qrInput); 
String qrInputText = qrInput.getText().toString(); 
Log.v(LOG_TAG, qrInputText); 

//Find screen size 
WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE); 
Display display = manager.getDefaultDisplay(); 
Point point = new Point(); 
// display.getSize(point); 
int width = point.x; 
int height = point.y; 
int smallerDimension = width < height ? width : height; 
smallerDimension = smallerDimension * 3/4; 

//Encode with a QR Code image 
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(qrInputText, 
null, 
Contents.Type.TEXT, 
BarcodeFormat.QR_CODE.toString(), 
smallerDimension); 



try { 
Bitmap bitmap = qrCodeEncoder.encodeAsBitmap(); 


Bitmap mergeBitmaps(Bitmap bmp1; Bitmap bmp2) 
{ 
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig()); 
Canvas canvas = new Canvas(bmOverlay); 
canvas.drawBitmap(bmp1, new Matrix(), null); 
canvas.drawBitmap(bmp2, 0, 0, null); 
return; 
} 


ImageView myImage = (ImageView) findViewById(R.id.imageView1); 
myImage.setImageBitmap(mergeBitmaps); 

} catch (WriterException e) { 
e.printStackTrace(); 
} 
+0

我已經更新了答案。看看 –

+0

你可以幫助AppDev。我添加了logo.but,它增加了左上角。你可以安排它在中心 – harikrishnan

+0

請檢查我的答案在http://stackoverflow.com/questions/13247701/how-to-add-a-logo-to-qr-code-in-android/43197496#43197496,I已經解決了:) –

回答

3

你有你的QR碼的位圖?正確

現在創建您的徽標的其他位圖。

如果您的徽標位於您的drawable-xxx文件夾中,則使用下面的代碼將您的drawable轉換爲位圖。

Bitmap myLogo = BitmapFactory.decodeResource(getResources(), R.drawable.myLogo); 

現在你有2個位圖圖像。使用以下代碼合併它們

public static Bitmap mergeBitmaps(Bitmap bmp1, Bitmap bmp2) { 
     Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig()); 
     Canvas canvas = new Canvas(bmOverlay); 
     canvas.drawBitmap(bmp1, new Matrix(), null); 
     canvas.drawBitmap(bmp2, 0, 0, null); 
     return bmOverlay; 
    } 

之後。位圖設置爲您的圖像視圖

希望這有助於

更新

對下面的代碼看看。我已經對這個

package com.mergebitmaps; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Matrix; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 

import com.example.mergebitmap.R; 

public class MergeBitmaps extends Activity { 
private Button btnMerge; 
private ImageView imgTest; 

private Bitmap bitLogo, bitQrCode, bitMerged; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.merge_bitmap); 

    btnMerge = (Button) findViewById(R.id.button1); 
    imgTest = (ImageView) findViewById(R.id.imageView1); 

} 

@Override 
protected void onStart() { 
    super.onStart(); 

    bitQrCode = BitmapFactory.decodeResource(getResources(), 
      R.drawable.qr_code); 
    bitLogo = BitmapFactory.decodeResource(getResources(), 
      R.drawable.my_logo); 

    btnMerge.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      bitMerged = mergeBitmaps(bitLogo, bitQrCode); 
      imgTest.setImageBitmap(bitMerged); 

     } 
    }); 

} 

public static Bitmap mergeBitmaps(Bitmap bmp1, Bitmap bmp2) { 
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), 
      bmp1.getHeight(), bmp1.getConfig()); 
    Canvas canvas = new Canvas(bmOverlay); 
    canvas.drawBitmap(bmp1, new Matrix(), null); 
    canvas.drawBitmap(bmp2, 0, 0, null); 
    return bmOverlay; 
} 

} 

下面的示例項目是我的佈局文件

<?xml version="1.0" encoding="utf-8"?> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="Button" /> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/button1" 
    android:scaleType="centerInside" 
    android:src="@drawable/ic_launcher" /> 

</RelativeLayout> 

,這裏是輸出

enter image description here

^h ERE是圖片鏈接,我已經拿到了PNG圖像

QR代碼圖片鏈接https://cdn1.iconfinder.com/data/icons/ios7-line/512/QR_code.png

Chrome標誌圖片鏈接http://www.html5rocks.com/static/images/tutorials/easy-hidpi/chrome2x-8bit.png

+0

嗨,感謝您的回覆。我會試一試,讓你知道 – Appdev

+0

嗨對不起,我已經使用您建議的代碼更新了我的代碼,但得到了「令牌上的語法錯誤」(「 ;預計「接近」位圖mergeBitmaps(位圖bmp1,位圖bmp2);「但我已經交叉檢查兩次沒有任何遺漏 – Appdev

+0

可能是你缺少任何大括號,請注意,我在我的eclipse中粘貼了代碼,沒有任何錯誤。嘗試複製並再次過去 –