Search code examples
androidqr-codezxing

Merging QR Code with an image


I'm trying to merge QR Code with an image but no luck. It keeps giving me: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference.

Here is the class to resize and merge:

    public Bitmap mergeBitmaps(Bitmap myLogo, Bitmap bitmap){
    Bitmap combined = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    Canvas canvas = new Canvas(combined);
    int canvasWidth = canvas.getWidth();
    int canvasHeight = canvas.getHeight();
    canvas.drawBitmap(bitmap, new Matrix(), null);

    Bitmap resizeLogo = Bitmap.createScaledBitmap(myLogo, canvasWidth / 5, canvasHeight / 5, true);
    int centreX = (canvasWidth - resizeLogo.getWidth()) /2;
    int centreY = (canvasHeight - resizeLogo.getHeight()) / 2;
    canvas.drawBitmap(resizeLogo, centreX, centreY, null);
    return combined;
}

And here is how I push it into the ImageView:

    Bitmap myLogo = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background);
    Bitmap merge = mergeBitmaps(myLogo, bitmap);

    imageView.setImageBitmap(merge);

Please help :(

Edit: The nullpointerexcepion is pointing to Bitmap myLogo = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background);


Solution

  • So it seems like I tried all the drawable images that I have and some works, some don't, I'm not sure yet what's the requirement of limitation :/