0

"I want to display the image on screen." After scanning the QR code I need to show that as image in my application how to go about it.


I have tried this but not getting output

   BarcodeFormat barcodeFormat = BarcodeFormat.QR_CODE;
        int width0 = 500;
        int height0 = 500;

        int colorBack = 0xFF000000;
        int colorFront = 0xFFFFFFFF;

        QRCodeWriter writer = new QRCodeWriter();
        try
        {
            Hashtable<EncodeHintType, Object> hint = new Hashtable<EncodeHintType,Object>(2);
            hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            ByteMatrix bitMatrix = writer.encode(fName, barcodeFormat, width0, height0, hint);
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            int[] pixels = new int[width * height];
            for (int y = 0; y < height; y++)
            {
                int offset = y * width;
                for (int x = 0; x < width; x++)
                {

                   // if (bitMatrix.get(x, y))
                        pixels[offset + x] = colorBack;
                    //else
                        //pixels[offset + x] = colorFront;
                }
            }

            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            ImageView imageview = (ImageView)findViewById(R.id.imageView1);
            imageview.setImageBitmap(bitmap);
        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
Rick
  • 33
  • 7
  • [this](http://stackoverflow.com/q/14321095/1777090) should help. – MysticMagicϡ Sep 04 '14 at 10:29
  • do you mean u first scan the qr code as well as take a picture of that qr code and show it in your android view ? – Sagar Pilkhwal Sep 04 '14 at 10:30
  • [Zxing](https://code.google.com/p/zxing/) is an excellent library for QR-codes. You will find what you need there, including an android sample project. you can use it to decode the qr code and later encode it and display it in your android view. – Sagar Pilkhwal Sep 04 '14 at 10:34
  • Zxing is the way to go, will do what you ask.. BUT I'm not entirely sure what you want. You want to display an image of the QR code, OR an image that is linked / embedded **within** the QR code? – RossC Sep 04 '14 at 10:41
  • thanx @SagarPilkhwal for reply. I wanted to display the scanned image into my app. – Rick Sep 04 '14 at 12:54
  • you can take a picture using camera intent – Sagar Pilkhwal Sep 04 '14 at 12:56
  • thanx @Dhruti for reply, But i alredy done the QR scanner , but i wanted to display the image as well. – Rick Sep 04 '14 at 12:57
  • @SagarPilkhwal Yeah i have done that but i am not able to access display the QR code image – Rick Sep 04 '14 at 13:07

1 Answers1

1

Decode qrcode:

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");  
startActivityForResult(intent, REQUEST_BARCODE);
Toast toast = Toast.makeText(this, "Start scanning QR code", Toast.LENGTH_SHORT);
toast.show();



and then encode it to get the image in outputstream:

public void generateQRCode(int width, int height, String content, OutputStream outputStream) throws Exception { 
    String imageFormat = IMAGE_FORMAT; 
    BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE,width, height);
    MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, outputStream);     
}


OR

byte[] b = {0x48, 0x45, 0x4C, 0x4C, 0x4F};
//convert the byte array into a ISO-8859-1 string
String data;
try {
    data = new String(b, "ISO-8859-1");
}
catch (UnsupportedEncodingException e) {
 //the program shouldn't be able to get here
 return;
}

//get a byte matrix for the data
ByteMatrix matrix;
com.google.zxing.Writer writer = new QRCodeWriter();
try {
 matrix = writer.encode(data, com.google.zxing.BarcodeFormat.QR_CODE, width, height);
}
catch (com.google.zxing.WriterException e) {
 //exit the method
 return;
}

//generate an image from the byte matrix
int width = matrix.getWidth(); 
int height = matrix.getHeight(); 

byte[][] array = matrix.getArray();

//create buffered image to draw to
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

//iterate through the matrix and draw the pixels to the image
for (int y = 0; y < height; y++) { 
 for (int x = 0; x < width; x++) { 
  int grayValue = array[y][x] & 0xff; 
  image.setRGB(x, y, (grayValue == 0 ? 0 : 0xFFFFFF));
 }
}

//write the image to the output stream
ImageIO.write(image, "png", outputStream);



Edit: refer post for "Capture Image from Camera and Display in Activity".

Community
  • 1
  • 1
Sagar Pilkhwal
  • 5,914
  • 2
  • 27
  • 78
  • @Rick - i have edited the answer and added a link to a post which talks about how to use a camera intent and capture image. – Sagar Pilkhwal Sep 04 '14 at 13:08