0

I want to create a qr code to transfer data like this:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRCode {
    static int a = 0;
    public static void main(String[] args) throws WriterException,
                                           IOException,NotFoundException
    {
        int qrCodeData = a;
        String filePath = "C://Users/Tommy/QRCode.png";
        String charset = "UTF-8"; // or "ISO-8859-1"
        Map<EncodeHintType, ErrorCorrectionLevel> hintMap = new HashMap<EncodeHintType,ErrorCorrectionLevel>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

        createQRCode(qrCodeData, filePath, charset, hintMap, 200, 200);
        System.out.println("QR Code image created successfully!");

        System.out.println("Data read from QR Code: "
        + readQRCode(filePath, charset, hintMap));

    }

    public static void createQRCode(final int qrCodeData, String filePath,
                                      String charset, Map hintMap, 
                                      final int qrCodeheight, final int qrCodewidth)
                                      throws WriterException, IOException
    {
        BitMatrix matrix = new MultiFormatWriter().encode(
        new String(qrCodeData.getBytes(charset), charset),
        BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight);
        MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath.lastIndexOf('.') + 1), new File(filePath));
    }

    public static String readQRCode(String filePath, String charset, Map hintMap)
      throws FileNotFoundException, IOException, NotFoundException
    {
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
                                      new BufferedImageLuminanceSource(
                                      ImageIO.read(new FileInputStream(filePath)))));
        Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap);
        return qrCodeResult.getText();
    }
}

But in the code i am using getBytes() which isnt working as i am using an integer value instead of a string value, how do i change this so i can use the int data type?

vandale
  • 3,600
  • 3
  • 22
  • 39
  • So you want to know how to get the byte representation of an int? You can do that by casting. Also see: http://stackoverflow.com/questions/7401550/how-to-convert-int-to-unsigned-byte-and-back – Nick Humrich Jun 17 '14 at 21:10
  • 1
    @Humdinger Considering casting an `int` to a `byte` removes 3 quarters of the information available in the `int`, and that you need 4 `byte`s for the full representation of an `int`, there's no way regular casting can give you the byte representation of an `int`. – awksp Jun 17 '14 at 21:13
  • I think QR codes store either Strings or byte[]s. If you want to *encode* an int, how you do it is up to you. Or another way, you're saying there's not ```getMyComplexObject()``` method, but that's a higher layer than you're working with. – David Ehrmann Jun 17 '14 at 21:13
  • You don't actually need to get a byte array at all, you can just convert the integer directly to a String which is what MultiFormatWriter expects for input - matrix = new MultiFormatWriter().encode(Integer.toString(qrCodeData), ...other arguments); – JonathanS Jun 17 '14 at 21:15
  • `qrCodeData.getBytes` does not make any sense. In your code `qrCodeData` is an `int`. – Nivas Jun 17 '14 at 21:16
  • @user3580294 thats correct. I didn't think that one through. I now remember that last time I did this I wrote a function to convert an int to a byte array. Thanks for the reminder. – Nick Humrich Jun 17 '14 at 21:25
  • @Humdinger No problem. We all are human, after all. – awksp Jun 17 '14 at 21:25

0 Answers0