前言
这个是在逛csdn上面看见其他人的,有点兴趣,故于此记录,同时分享给大家。至于其他人的博客我也没有记一下网站,罪过罪过。。。。本来现在也没有这样的需求,但是感觉有点意思,所以记了下来。
java实现截屏
效果截图
注意事项
一个简单得工具类,这个不用导入任何的东西,代码复制直接就可以用,但是得注意改一下包名(^U^)ノ~YO
代码
/** FileName: ScreenCapture.java
* @Description: TODO
* @Author: 秦杰
* @Createdate: 2019年6月25日下午12:58:42
*/
package function.java.utils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/** @ClassName; ScreenCapture
* @Description: TODO
* @Author: 秦杰
* @Date: 2019年6月25日
*/
public class ScreenCapture {
/**
* @Title: captureScreen 截屏操作
* @Description: TODO 实现屏幕截图功能
* @Param: @param filePathAndName 想要保存到的路径,比如说截屏,然后保存到G:/1.png中, ze
* @Param: @param format 生成的文件的格式,比如生成png文件,则该值就是填 png
* @Param: @return
* @Param: @throws AWTException
* @Param: @throws IOException
* @Return: String
* @Throws
*/
public static String captureScreen(String filePathAndName, String format) throws AWTException, IOException {
// 获取屏幕尺寸
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// 创建需要截取的矩形区域
Rectangle rect = new Rectangle(0, 0, screenSize.width, screenSize.height);
// 截屏操作
BufferedImage bufImage = new Robot().createScreenCapture(rect);
// 保存截取的图片
ImageIO.write(bufImage, format, new File(filePathAndName));
return filePathAndName;
}
public static void main(String[] args) throws AWTException, IOException {
System.out.println(ScreenCapture.captureScreen("G:/1.png", "png"));
}
}
小总结
这个核心4句代码都是人家得的,但是看着这个代码,真的很好理解,确实有点意思
java 生成解析简单二维码
原文
转载的下面网站的
https://blog.csdn.net/qq_40950957/article/details/81430141#commentBox
需要导入的包如下
需要的jar包,下载导入就可以了
https://github.com/seriouszyx/Mooc-and-more/tree/master/Java生成二维码/二维码所需资源/zxing
/** FileName: CreateQRCode.java
* @Description: TODO
* @Author: 秦杰
* @Createdate: 2019年6月25日下午12:23:01
*/
package function.java.utils;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
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.decoder.ErrorCorrectionLevel;
/** @ClassName; CreateQRCode
* @Description: TODO
* @Author: 秦杰
* @Date: 2019年6月25日
*/
/** 生成二维码 */
public class QRCode {
/**
* @Title: createQRCode 生成二维码
* @Description: TODO 生成二维码
* @Param: @param content 二维码的内容,一般是网址
* @Param: @param width 二维码宽度
* @Param: @param height 二维码高度
* @Param: @param format 二维码文件的后缀,比如生成jpg文件 则就是 jpg 生成png文件 则值就是 png
* @Param: @param filePathAndName 生成文件的路径和名称 比如: G:/QRCode.png
* @Param: @return 生成成功返回生成的文件的路径
* @Param: @throws IOException
* @Param: @throws WriterException
* @Return: String
* @Throws
*/
public static String createQRCode(String content, int width, int height, String format, String filePathAndName) throws IOException, WriterException {
/**
* 定义二维码参数
*
* CHARACTER_SET 编码类型
* ERROR_CORRECTION 纠错等级
* L < M < Q < H 纠错能力越高,可存储的越少
* MARGIN 边距
**/
@SuppressWarnings("rawtypes")
HashMap<EncodeHintType, Comparable> hints = new HashMap<EncodeHintType, Comparable>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 2);
/** 生成二维码 */
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
Path file = new File(filePathAndName).toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
return filePathAndName;
}
/**
* @Title: readQRCode 解析二维码
* @Description: TODO 解析二维码
* @Param: @param filePathAndName 二维码文件所在路 比如 G:/1.png
* @Param: @return 目前返回二维码的文本内容
* @Param: @throws NotFoundException
* @Param: @throws IOException
* @Return: String
* @Throws
*/
@SuppressWarnings("unchecked")
public static String readQRCode(String filePathAndName) throws NotFoundException, IOException {
MultiFormatReader formatReader = new MultiFormatReader();
File file = new File(filePathAndName);
BufferedImage image = ImageIO.read(file);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
@SuppressWarnings("rawtypes")
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
Result result = formatReader.decode(binaryBitmap, hints);
// System.out.println("解析结果: " + result.toString());
// System.out.println("二维码的格式类型:" + result.getBarcodeFormat());
// System.out.println("二维码文本内容: " + result.getText() );
return result.getText();
}
public static void main(String[] args) {
/** 二维码大小 */
int width = 300;
int height = 300;
/** 二维码格式 */
String format = "png";
/** 二维码内容 */
String content = "https://github.com/seriouszyx";
try {
System.out.println(QRCode.createQRCode(content, width, height, format, "G:/1.png"));//生成二维码到指定路径中去
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
System.out.println(QRCode.readQRCode("G:/1.png"))//通过路径读取二维码内容数据;
} catch (NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
注意事项
复制代码,然后导入包,换一下首行的包名,就可以了,主要可以看一下main测试方法的代码,然后提供相关数据就可生成二维码了,同样输入相关二维码存放路径即可读取二维码内容
总结
现在主要是生成二维码的一个功能太简单了,后面在学习生成更复杂的二维码吧。这是逛csdn上感兴趣便于此记录。