缓冲区Qrcode图像未加载

编程入门 行业动态 更新时间:2024-10-27 04:27:07
本文介绍了缓冲区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.zxingmon.BitMatrix; import com.google.zxingmon.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] + " Address: " + amnt[0] + " Amount: " + finn[1]; String[] linkparam = {parts[0],amnt[0],finn[1]}; text = text + " " + linkparam[0] + " - " + linkparam[1] + " - " + linkparam[2]; } } catch (IOException e) { text = "Could not decode QR Code, IOException :: " + e.getMessage(); } return text; } }

上面是处理我需要的所有函数的类。

当我加载下一个内容面板时,它不会加载存储的qrcode。但当我关闭程序并加载它时,放入一个新的量,它会读取上次的二维码。

我用这个来称呼它。

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

生成Qrcode时没有任何问题。

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

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

这两个细分市场属于不同的类别。我如何才能让它读取我实际需要的当前文档?

推荐答案

回答如下。

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

更多推荐

缓冲区Qrcode图像未加载

本文发布于:2023-07-27 02:46:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓冲区   图像   加载   Qrcode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!