我正在使用ZXING库在JAVA中创建QR码生成器程序。该程序是
import com.google.zxing.BarcodeFormat; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; public class QR_Gen { private static final String QR_CODE_IMAGE_PATH = "./MyCode.png"; private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); Path path = FileSystems.getDefault().getPath(filePath); MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); } public static void main(String[] args) { try { generateQRCodeImage("This is my first QR Code", 350, 350, QR_CODE_IMAGE_PATH); System.out.println("QR Code generated successfully"); } catch (WriterException e) { System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage()); } catch (IOException e) { System.out.println("Could not generate QR Code, IOException :: " + e.getMessage()); } } }
编译该程序时,出现类型不匹配错误,
Type mismatch: cannot convert from ByteMatrix to BitMatrix
在这条线
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
请帮忙!!!
我自己从未使用过该库,但是阅读错误消息时,我会假设您必须要以字节为单位存储字节的问题。问题将是一个字节由多个位组成,因此您不能仅通过一位表示一个字节。
将编码数据存储到ByteMatrix中,然后阅读以下内容:
完成事情。