做最后一个任务,新建文件,放入二维码并且输出:
首先讲一下怎样就可以让java生成二维码,这里其实是有很多开源的工具的,我这里推荐使用Zxing,首先讲解一下怎样使用这个Zxing:
首先访问https://github.com/zxing,并且下载:
下载完之后,解压,如下图所示。:
进入zxing-master目录下,可以看到所有的源码文件,其中core和javase两个目录最重要。我们用Java生成二维码就用到这两个目录下的文件。:
下面我们把core和javase两个目录下的文件打成一个jar包,方法是新建一个Java工程zxing,如下图所示:
然后我们把core目录下的com目录整个拷贝到src目录下:
拷贝完后src目录如下图所示:
接着我们再把javase目录下的com目录拷贝过来,会提示是否需要覆盖,我们点击”Yes”即可:
拷贝完后会报错,但不用管它。
我们下面来打包,在工程上右键,在右键菜单中点击”Export…”:
在弹出的对话框中选择”JAR file”,点击”Next”:
选择打包的路径以及打包的名称,如下图所示,点击”Finish”:
会有警告,我们不用管它,点击”OK”。
我们下面到E:\Zxing目录下查看,发现已经生成zxing-3.2.1.jar包了。以后我们便可以使用这个jar包来生成二维码了。
奖励:要是不想自己去实现上面的步骤,我已经自己生成好了:
链接:https://pan.baidu.com/s/1301kKQjdqpRZEnOmolaRWg
提取码:xzii
--来自百度网盘超级会员V3的分享
分割线————————————————————-
接下来代码讲解:(自行将zxing.jar导入IDEA):
首先我们需要生成一个新文件:
File file = new File("G:\\number.txt");
FileWriter writer = null;
writer = new FileWriter(file,false);
if (file.exists()){
System.out.println("创建成功!");
}
然后我们将值写入:
String result = "100011720988";
writer.append(result);
writer.flush();
读取文件内容并打印:
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader br = new BufferedReader(isr);
String lineTxt = null;
String line1 = br.readLine();
System.out.println(line1);
修改文件内容:
String str = "https://item.csdn.com/"+result+".html";
writer.append(str);
writer.flush();
String line2 = br.readLine();
System.out.println(line2);
生成二维码:
int width = 300; //二维码图片的宽度
int height = 300; //二维码图片的高度
String format = "png"; //二维码格式
String content = line2;
//定义二维码内容参数
HashMap hints = new HashMap();
//设置字符集编码格式
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//设置容错等级,在这里我们使用M级别
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
//设置边框距
hints.put(EncodeHintType.MARGIN, 2);
try {
//指定二维码内容
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
//指定生成图片的保存路径
Path newfile = new File("H:\\imooc.png").toPath();
//生成二维码
MatrixToImageWriter.writeToPath(bitMatrix, format, newfile);
} catch (Exception e) {
e.printStackTrace();
}
全部代码:
package 任务11IO流;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.io.*;
import java.nio.file.Path;
import java.util.HashMap;
/**
* @author ${范涛之}
* @Description
* @create 2021-11-26 10:56
*/
public class Test9 {
public static void main(String[] args) throws IOException {
/**
* 生成新文件并且添加内容
*/
File file = new File("G:\\number.txt");
FileWriter writer = null;
writer = new FileWriter(file,false);
if (file.exists()){
System.out.println("创建成功!");
}
String result = "100011720988";
writer.append(result);
writer.flush();
/**
* 读取文件内容并打印
*/
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader br = new BufferedReader(isr);
String lineTxt = null;
// while ((lineTxt = br.readLine()) != null) {
// System.out.println(lineTxt);
// }
String line1 = br.readLine();
System.out.println(line1);
/**
* 修改文件内容
*/
String str = "https://item.csdn.com/"+result+".html";
writer.append(str);
writer.flush();
// while ((lineTxt = br.readLine()) != null) {
// System.out.println(lineTxt);
// }
String line2 = br.readLine();
System.out.println(line2);
/**
* 生成二维码
*/
int width = 300; //二维码图片的宽度
int height = 300; //二维码图片的高度
String format = "png"; //二维码格式
String content = line2;
//定义二维码内容参数
HashMap hints = new HashMap();
//设置字符集编码格式
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//设置容错等级,在这里我们使用M级别
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
//设置边框距
hints.put(EncodeHintType.MARGIN, 2);
try {
//指定二维码内容
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
//指定生成图片的保存路径
Path newfile = new File("H:\\imooc.png").toPath();
//生成二维码
MatrixToImageWriter.writeToPath(bitMatrix, format, newfile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/81042.html