2

It is Possible to add logo image in the middle of QR code image using android?

I have generated the QR code but now what i need is need to insert the logo image in the middle of QRcode.

Is there any way to achieve this.

Here is my QR code Generation code:

Bitmap myLogo = BitmapFactory.decodeResource(getResources(), R.drawable.image); public void onClick(View 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();
}

}

I don't have idea about that could someone please guide me to step forward.

Thanks in advance for the helping hearts.

This is how i implemented:

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();
}
Appdev
  • 189
  • 2
  • 6
  • 28

1 Answers1

2

You have the bitmap of your QR Code? right

now create an other bitmap of your logo.

if your logo is in one of your drawable-xxx folder then use below code to covert your drawable to a bitmap.

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

now you have 2 bitmap images . Use below code to merge them

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;
    }

after that. set your bitmap to your image view

Hope this helps

Update

have a look on below code. I have made a sample project on this

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;
}

}

Here is my layout file

<?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>

and here is the output

enter image description here

here is the image links where I have got the png images

QR Code Image link https://cdn1.iconfinder.com/data/icons/ios7-line/512/QR_code.png

Chrome Logo Image Link http://www.html5rocks.com/static/images/tutorials/easy-hidpi/chrome2x-8bit.png

Qadir Hussain
  • 8,721
  • 13
  • 89
  • 124
  • Hi thanks for your Response..will give a try and let you know – Appdev May 22 '14 at 07:39
  • Hi Sorry, I have updated my code with the code which you suggested but got the error as "Syntax error on token "(", ; expected" near "Bitmap mergeBitmaps (Bitmap bmp1, Bitmap bmp2);" but i have cross checked twice nothing is missing. – Appdev May 22 '14 at 07:53
  • may be you are missing any braces. please look careful. I have pasted code in my eclipse without any error. try to copy and past again – Qadir Hussain May 22 '14 at 07:56
  • Is it so??i have cross checked twice but no issues will check one more time and get back to you. – Appdev May 22 '14 at 07:57
  • If you dont mind May i know where to insert the above mention code..because im keep on getting same error couldn't rectify it.I copied same code and paste it in my button on click event but throws an error – Appdev May 22 '14 at 10:38
  • @Appdev paste this code in your activity's class {} block. where are u placing this code? can u update ur code in your question – Qadir Hussain May 22 '14 at 11:00
  • ok thanks let me check sorry for the trouble..s pls will update now – Appdev May 22 '14 at 11:07
  • 1
    @Appdev place the mergeBitmaps in your acticity class block I said. and then call the method in your onclick method. like this mergeBitmaps(qrcodeBitmapObjet, myLogoBitmapObject); you are placing this in try catch block i think – Qadir Hussain May 22 '14 at 11:25
  • Yup!!! sorry i am new to android so only this much problems.....i will change it...Thanks @Qadir Hussain – Appdev May 22 '14 at 11:27
  • Hi i have implemented the code as u said ...no error but the logo is not updated in the QR code..can u please help me – Appdev May 22 '14 at 13:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54218/discussion-between-qadir-hussain-and-appdev). – Qadir Hussain May 22 '14 at 15:04
  • can u help Qadir. i added the logo.but, its adding top left side. can u arrange it on center yar – harikrishnan Jan 05 '17 at 11:15
  • @harikrishnan your logo image size and QR code Image size should be same. – Qadir Hussain Jan 05 '17 at 12:44
  • thank you for reply.but, i want inside small size logo only yar.then,finally, i tried this one. canvas.drawBitmap(bmp2, 240, 240, null);.. its worked out. If anyother method ,please let me know. – harikrishnan Jan 05 '17 at 12:46
  • @harikrishnan make it more dynamic. don't hardcode the center value, get it from your bitmap like this http://stackoverflow.com/questions/8096454/get-centerpoint-of-bitmap-on-an-imageview – Qadir Hussain Jan 05 '17 at 13:17