本文介绍了缓冲区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图像未加载
发布评论