Type mismatch: cannot convert from ByteMatrix to BitMatrix

感情迁移 提交于 2019-12-02 19:47:53

问题


I am creating a QR code generator program in JAVA using ZXING library. The program is

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());
        }
    }

}

While compiling this program i'm getting a Type Mismatch Error,

Type mismatch: cannot convert from ByteMatrix to BitMatrix

in this line

BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);

Please help!!!


回答1:


Never used this library myself, but reading the errormessage I would assume that you have to problem that you want to store byte in bits. The problem will be that a byte is build up by multiple bits so you cannot represent a byte via only one bit.

Store your encode data into an ByteMatrix and then read this post:

  • QR Code encoding and decoding using zxing

to finish things off.




回答2:


According to the javadoc here and the source code here, the QRCodeWriter.encode methods return a BitMatrix, not a ByteMatrix. So, that compilation error should not occur.

Unless ....

... you are using some version1 of the com.google.zxing library that is incompatible with those javadocs. Check where you got your zxing JAR file from.

Despite searching, I haven't been able to find the version with this incompatibility. However:

  1. I note that the official library has been forked many times on GitHub, and any one of those could be the source of the incompatibility.

  2. I found this which clearly a fork, and clearly has tweaked encode. However, the people who did this have at least had the good sense to change the package name!


I don't know if this is relevant, but it appears that in the C# version of this API, the QRCodeWriter.encode method does return a ByteMatrix; see here for evidence. Maybe you've stumbled across someone's misguided attempt to "fix" the Java API to match the C# API.




回答3:


I got the same Issues and i solve it by downloading compatible zxing-javase.jar and zxing

http://www.java2s.com/Code/Jar/z/Downloadzxingjavasejar.htm http://www.java2s.com/Code/Jar/z/Downloadzxingjar.htm

the latest version are compatible and use ByteMatrix not BitMatrix. download them from original www.java2s.com



来源:https://stackoverflow.com/questions/51763384/type-mismatch-cannot-convert-from-bytematrix-to-bitmatrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!