java 图片webp转其他格式实现

转载请标明出处!

https://blog.csdn.net/yixin605691235/article/details/80939055


需求:接到webp图片的base64码转存为其他格式图片(如:png)的base64码上传服务器。其实就是将webp图片转为png图片而已,只是使用base64编码来体现。

根据需求网上寻找到两个资源:
1、Java VP8 Decoder
这种方法未能实现,主要原因是采用本样例转出来的图片变成黑白色,具体原因也不太清楚。

2、直接通过ImageIO来转图片格式
不过需要额外的两个jar包,WebPImageDecoderPlugin-0_2.jar、WebPImageIoPlugin-0.1.jar
没有这两个包转图片会报错image=null;

这种方式成功解决我的问题。

完整例子和jar包下载:


以下是部分需求测试时代码的实现:
public static void fun() {
try {
BASE64Encoder base64Encoder = new BASE64Encoder();
webptest4 server = new webptest4();
byte[] b=server.stringToByte(aa);
InputStream stream = new ByteArrayInputStream(b);
BufferedImage im = ImageIO.read(stream);
ByteArrayOutputStream outStream =new ByteArrayOutputStream();
ImageIO.write(im, "png", outStream);
byte [] data = outStream.toByteArray();
String str = base64Encoder.encode(data);
generateImage(str,"D:/test/2/444.png");
//	BASE64Decoder base64Decoder = new BASE64Decoder();
//	//解密字符串转字节数组
//	byte [] bs = base64Decoder.decodeBuffer(str);
//	//创建字节写入流
//	OutputStream imgOut = new FileOutputStream("D:/test/2/444.png");
//	imgOut.write(bs);
//	//关闭流
//	imgOut.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

后面测试部分是转成图片,进行验证。
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值