缓冲区qrcode图像未加载

时间:2018-03-21 20:12:39

标签: java qr-code

目前我正在尝试在缓冲区中加载qrcode,以便在现场显示。

这是我目前的代码修订版。

https://login.microsoftonline.com/advaiya.com/oauth2/authorize

以上是处理我需要的所有功能的课程。

当我加载下一个内容窗格时,它不会加载存储的qrcode。但是当我关闭程序并加载它时,输入一个新的数据它会从上次读取qrcode。

我用它来称呼它。

package wallettemplate;

import com.google.zxing.*;
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.QRCodeWriter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

import static wallettemplate.MainPage.kit;

public class BackEnd{
    public static final String QR_CODE_IMAGE_PATH = "./MyQRCode.png";
    public static String decodeQRCode(File qrCodeimage) throws IOException {
        BufferedImage bufferedImage = ImageIO.read(qrCodeimage);
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        try {
            Result result = new MultiFormatReader().decode(bitmap);
            return result.getText();
        } catch (NotFoundException e) {
            System.out.println("There is no QR code in the image");
            return null;
        }
    }
    public 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 String gbal(){
        String balance = kit.wallet().getBalance().toFriendlyString();
        return balance;
    }
    public static String purchase(double amount){
        String text;
        try {
            generateQRCodeImage("bitcoin:"+kit.wallet().currentReceiveAddress().toString()+"?amount="+String.format("%.7f",amount), 20, 20, QR_CODE_IMAGE_PATH);
            text = "QR Code generated. - "+kit.wallet().currentReceiveAddress().toString();
        } catch (WriterException e) {
            text = "Could not generate QR Code, WriterException :: " + e.getMessage();
        } catch (IOException e) {
            text = "Could not generate QR Code, IOException :: " + e.getMessage();
        }
        return text;
    }
    public static String refund(){
        String text;
        try {
            File file = new File(QR_CODE_IMAGE_PATH);
            String decodedText = decodeQRCode(file);
            if(decodedText == null) {
                text = "No QR Code found in the image";
            } else {
                //text = "Decoded text = " + decodedText + " and the amount is "+value;
                String[] parts = decodedText.split(":");
                String[] amnt = parts[1].split("\\?");
                String[] finn = amnt[1].split("=");
                text = "Type: " + parts[0] + "\nAddress: " + amnt[0] + "\nAmount: " + finn[1];
                String[] linkparam = {parts[0],amnt[0],finn[1]};
                text = text + "\n" + linkparam[0] + " - " + linkparam[1] + " - " + linkparam[2];

            }
        } catch (IOException e) {
            text = "Could not decode QR Code, IOException :: " + e.getMessage();
        }
        return text;
    }
}

生成的qrcode没问题。

当我用JLabel中的图像读取它时,我使用此代码。

btnConfirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                BackEnd.purchase(Double.parseDouble(lblfinal.getText()));
                parentForm.showPanel(MainPage.PPROCESS);
            }
        });

这两个部分属于不同的类别。我怎样才能读到我真正需要的CURRENT?

1 个答案:

答案 0 :(得分:0)

回答如下。

public static BufferedImage getQRCodeImage(String amount) throws WriterException{
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode("bitcoin:"+kit.wallet().currentReceiveAddress().toString()+"?amount="+String.format("%.7f",Double.parseDouble(amount)), BarcodeFormat.QR_CODE, 200, 200);
        return MatrixToImageWriter.toBufferedImage(bitMatrix);
    }

imageIcon.setImage(BackEnd.getQRCodeImage(cost));
            lblQRCode.setIcon(imageIcon);