Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a logo to QR code in android

Tags:

android

zxing

I have seen few QR codes with company logo at the center. Is it possible to generate a QR code with a any logo in android? If possible kindly explain the way for doing it. Currently I am using Zxing for generating QR codes.

like image 206
roy mathew Avatar asked Nov 06 '12 09:11

roy mathew


2 Answers

A QR code is a quick response code, You can use zxing to make the QR codes. but by default there are no company logos present there at center or any other part. What you can do is create a QR code and on top of it draw the logo image of the company

like image 94
Girish Nair Avatar answered Oct 12 '22 11:10

Girish Nair


With reference to the guide and source code provided at Generating a qr code with a logo please find the sample Android code that I use to achieve similar result on Android.

I am sure that this code can be optimised, specifically with regards to image overlay opacity by making use of the Paint class but this code works effectively in this regard.

    /**
 * Writes the given Overlay on a new Bitmap object.
 * @param Bitmap the Bitmap to overlay.
 * @return the new {@link Bitmap}-object.
 */ 
public static Bitmap overlayBitmap(Bitmap overlay) {        
    BitMatrix matrix = null;
    QRCodeWriter writer = new QRCodeWriter();

    //Error correction

    //Sometimes your QRCode will get damaged or covered up by something – like an image overlay for instance – 
    //therefore the designers of the QRCode has added four levels; 7% (L), 15 % (M), 25% (Q), 30% (H) of error 
    //correction were a error correction of level H should result in a QRCode that are still valid even when it’s 
    //30% obscured – for more info on error correction check this       

    Map<EncodeHintType,  Object> hints; 

    hints = new HashMap<EncodeHintType, Object>();
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);             

    //create qr code matrix
    writer = new  QRCodeWriter();
    try {
        matrix = writer.encode(redirectUrl, 
                                 BarcodeFormat.QR_CODE,
                                 QRCODE_IMAGE_WIDTH,
                                 QRCODE_IMAGE_HEIGHT,
                                 hints);
    } catch (WriterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

    Bitmap image = toBitmap(matrix);
    int height = image.getHeight();
    int width = image.getWidth();

    Bitmap combined = Bitmap.createBitmap(width, height, image.getConfig());

    Canvas canvas = new Canvas(combined);
    int canvasWidth = canvas.getWidth();
    int canvasHeight = canvas.getHeight();

    canvas.drawBitmap(image, new Matrix(), null);

    int centreX = (canvasWidth  - overlay.getWidth()) /2;
    int centreY = (canvasHeight - overlay.getHeight()) /2 ; 

    //http://stackoverflow.com/a/12235235/1635441
    //http://stackoverflow.com/a/5119093/1635441        
    //Paint p = new Paint();
    //p.setXfermode(new PorterDuffXfermode(Mode.DST_ATOP)); //http://stackoverflow.com/a/17553502/1635441
    //p.setAlpha(180);
    //p.setARGB(a, r, g, b);

    //canvas.drawBitmap(bitmapToBeOverlay, 0, 0, p);        

    //canvas.drawBitmap(overlay, new Matrix(), null);
    canvas.drawBitmap(overlay, centreX, centreY, null);

    return combined;
}

    /**
 * Writes the given Matrix to a new colour Bitmap object.
 * @param matrix the matrix to write.
 * @param Color the Color to be added.
 * @return the new {@link Bitmap}-object.
 */
public static Bitmap toBitmapColour(BitMatrix matrix, int colour){
    int height = matrix.getHeight();
    int width = matrix.getWidth();
    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    for (int x = 0; x < width; x++){
        for (int y = 0; y < height; y++){
            bmp.setPixel(x, y, matrix.get(x,y) ? colour : Color.WHITE);
        }
    }
    return bmp;
}

HTH

like image 26
ramizmoh Avatar answered Oct 12 '22 10:10

ramizmoh