buffer qrcode image not loading

2019-08-18 05:50发布

Currently im trying to load a qrcode in buffer so it would display on the spot.

This is my current revision for the code.

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

Above is the class that deals with all functions i need.

When i load the next contentpane it doesnt load up the qrcode that gets stored. But when i shut the program down and load it up, put in a new amount it reads the qrcode from last time.

I call it with this.

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

The qrcode gets generated fine no issues.

When i read it with an image inside a JLabel i use this code.

private void createUIComponents() {
        ImageIcon imageIcon = new ImageIcon("MyQRCode.png");
        Image image = imageIcon.getImage();
        Image newimg = image.getScaledInstance(200, 200,  java.awt.Image.SCALE_SMOOTH);
        imageIcon = new ImageIcon(newimg);
        lblQRCode = new JLabel(imageIcon);
    }

These 2 segments are in different classes. How can i get it to read the CURRENT one i actually need?

标签: java qr-code
1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-18 06:06

Answer as follows.

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);
查看更多
登录 后发表回答