buffer qrcode image not loading(缓冲区Qrcode图像未加载)
本文介绍了缓冲区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] + "
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图像未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:缓冲区Qrcode图像未加载
基础教程推荐
猜你喜欢
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01