我无法在新的URL Java上生成QR(Zxing)代码

时间:2018-09-26 19:06:18

标签: java zxing

我正在使用ZXing库创建QR码,当将其保存到计算机上的项目文件夹中时,它确实非常有用,但是现在我需要直接在新的“ URL”上创建它而不保存在项目中。我的代码是:

public class QRCodeGenerator {

public static String generateQRCodeImageToBase64(String text, int width, int height, String filePath, String value)
        throws WriterException, IOException, URISyntaxException {
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);

    /*
        URL pathURL = new URL("http://localhost:8079/TrackPetService/QRcodes/MyQRCode_"+value+".png");
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pathURL.openConnection().getOutputStream()); 

        The third parameter manage the path and need to be a Path class or OutputStream,
        but i cannot find the way to save it or print on the browser without saving on a
        folder on the project.
    */

    Path path = FileSystems.getDefault().getPath(filePath); //This works perfect but takes the root the file system
    MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); // Works perct but saves on the project

      File file = new File("/QRcodes/MyQRCode_"+value+".png");
      FileInputStream fileInputStreamReader = new FileInputStream(file);
      byte[] bytes = new byte[(int)file.length()];
      fileInputStreamReader.read(bytes);
      String encodedFile = Base64.getEncoder().encodeToString(bytes);
      System.out.println(encodedFile);

      return encodedFile;
}
}

0 个答案:

没有答案