简体   繁体   English

QR码编译错误

[英]QR code compilation error

I am trying to make aq code to hold a number to transfer it to others so i made a test 我正在尝试使aq代码保留一个数字以将其转移给其他人,所以我进行了测试

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 when i compile it, i get: 但是当我编译它时,我得到:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:           Cannot invoke getBytes(String) on the primitive type int

at QRCode.createQRCode(QRCode.java:46)
at QRCode.main(QRCode.java:34)

How can i fix this? 我怎样才能解决这个问题?

Easy. 简单。 You are calling getBytes() on an int. 您正在int上调用getBytes() This is not possible. 这是不可能的。

Perhaps this question can be helpful tool. 也许这个问题可能是有用的工具。

Perhaps something like so. 也许是这样的。

public static final byte[] intToByteArray(int value) {
    return new byte[] {
        (byte)(value >>> 24),
        (byte)(value >>> 16),
        (byte)(value >>> 8),
        (byte)value};
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM