Java--二维码生成&图片和流转化
2024-10-08 17:49:52
package test; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Hashtable; import javax.imageio.ImageIO; 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.sun.org.apache.xerces.internal.impl.dv.util.Base64; public class Test { public static void main(String[] args) throws Exception {
String text = "你好"; int width = 100;
int height = 100;
String format = "png";
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
File outputFile = new File("new.png");
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile); byte[] b = toByteArray(new File("new.png"));
new Base64();
String s = Base64.encode(b);
// String s = new String(b, "utf-8");
System.out.println(s);
// new Base64();
ByteArrayInputStream in = new ByteArrayInputStream(Base64.decode(s));
BufferedImage image = ImageIO.read(in);
File newFile = new File("new2.png");
ImageIO.write(image, "png", newFile); } public static byte[] toByteArray(File imageFile) throws Exception {
BufferedImage img = ImageIO.read(imageFile);
ByteArrayOutputStream buf = new ByteArrayOutputStream((int) imageFile.length());
try {
ImageIO.write(img, "jpg", buf);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return buf.toByteArray();
} }
最新文章
- 是时候 UWP 了 !
- jQuery的$.ajax
- 15个来自 CodePen 的酷炫 CSS 动画效果【下篇】
- TextBoxFor控件的扩展---Bootstrap在mvc上的应用
- Eclipse调试方法及快捷键
- α发布后的感想(组长作业)
- 如何在redhat下安装办公软件(openoffice)
- footer居底
- NET Core驱动已出,支持EF Core
- PHP基础设计模式——工厂模式
- spring.NET的依赖注入
- jquery-easyUI第一篇【介绍、入门、使用常用的组件】
- Oracle常用的数值函数,日期函数
- Haproxy 配置 ACL 处理不同的 URL 请求
- cemtos7.2搭建samba
- Android O seLinux 编译错误
- lambdas vs. method groups
- c++各种排序的简单实现
- 解决uc浏览器不支持vw单位的方法
- photoshop CS5制作具有立体感的按钮