Tuesday, April 24, 2012

Bitmap in ZXing (how to encode QR)

// this is from QREncoder.java from ZXing

  static Bitmap encodeAsBitmap(String contents,
                               BarcodeFormat format,
                               int desiredWidth,
                               int desiredHeight) throws WriterException {
    Hashtable<EncodeHintType,Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
      hints = new Hashtable<EncodeHintType,Object>(2);
      hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();   
    BitMatrix result = writer.encode(contents, format, desiredWidth, desiredHeight, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    for (int y = 0; y < height; y++) {
      int offset = y * width;
      for (int x = 0; x < width; x++) {
        pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
      }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
  }

//This is how you can generate normally:
private Bitmap generateQRCode2(String data)
    {
        //Size of the image generated.
        int h = 100;
        int w = 100;
        Config conf = Bitmap.Config.RGB_565;
        Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
       
        Charset charset = Charset.forName("UTF-8");
        CharsetEncoder encoder = charset.newEncoder();
        byte[] b = null;
        try {
            // Convert a string to UTF-8 bytes in a ByteBuffer
            ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(data));
            b = bbuf.array();
        } catch (CharacterCodingException e) {
            System.out.println(e.getMessage());
        }

        String data1;
        try {
            data1 = new String(b, "UTF-8");
            // get a byte matrix for the data
            BitMatrix matrix = null;
            // Size of the QR code
           
            com.google.zxing.Writer writer = new QRCodeWriter();
            try {
                Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2);
                hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
                matrix = writer.encode(data1, com.google.zxing.BarcodeFormat.QR_CODE,w, h);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
           
            //generate an image from the bit matrix
            int width = matrix.getWidth();
            int height = matrix.getHeight();
           
            try {     
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++) {
                        bmp.setPixel(x, y, matrix.get(x, y) ? BLACK : WHITE);
                      }
                    }
                           
            } catch (Exception e) {
                System.out.println(e.getMessage());
            } 
        } catch (UnsupportedEncodingException e) {
            System.out.println(e.getMessage());
        }
       
        return bmp;

            // change this path to match yours (this is my mac home folder, you can use: c:\\qr_png.png if you are on windows)
            //String filePath = "/Users/shaybc/Desktop/OutlookQR/qr_png.png";
            /*
            String filePath;
            File file = new File(filePath);
            try {
                MatrixToImageWriter.writeToFile(matrix, "PNG", file);
                System.out.println("printing to " + file.getAbsolutePath());
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        } catch (UnsupportedEncodingException e) {
            System.out.println(e.getMessage());
        }*/
    //}




1 comment:

  1. I want to generate qr code of a text in my application i have to zxing library but I have no idea to implement this. how can I implement this?any help.
    QR Code encoder

    ReplyDelete