将64位图像编码转成JPEG图片



import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.plexus.util.Base64;
/**
 * 将64位图像编码转成图像
 * @author Administrator
 *
 */
public class Base64GenerateImapge {
	/**
	 * 生成图像
	 * @param imgStr
	 * @param imgFilePath
	 * @param suffix  图像的后缀
	 * @return
	 */
	public String GenerateImage(String imgStr,String imgFilePath,String suffix){
		if (imgStr.length()<1) // 图像数据为空
			return "无图像数据";
		if (imgFilePath.length()<1)//图像路径
			return "请传入文件路径";
		if (suffix.length()<1) //图像格式后缀
			return "请传入图像文件的后缀";
		Base64 base64=new Base64();
		byte[] b=base64.decodeBase64(imgStr.getBytes());
		for (int i = 0; i < b.length; ++i) {
			if (b[i] < 0) {// 调整异常数据
			b[i] += 256;
			}
		}
		// 生成jpeg图片
		String imgPath=imgFilePath+"."+suffix;
		try {
			OutputStream out = new FileOutputStream(imgPath);
			out.write(b);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			return "图像生成失败";
		}
		return "图片生成成功,保存在"+imgPath;
	}
	/**
	 * 生成jpeg图像,返回图像路径
	 * @param imgStr
	 * @param imgFilePath
	 * @return
	 */
	public Map<String, String> GenerateImageJpeg(String imgStr,String imgFilePath){
		Map<String, String> map=new HashMap<String, String>();
		if (imgStr.length()<1){ // 图像数据为空
			map.put("错误", "无图像数据");
			return map;
		}	
		if (imgFilePath.length()<1){//图像路径
			map.put("错误", "请传入要保存图像的路径");
			return map;
		}	
		Base64 base64=new Base64();
		byte[] b=base64.decodeBase64(imgStr.getBytes());
		for (int i = 0; i < b.length; ++i) {
			if (b[i] < 0) {// 调整异常数据
				b[i] += 256;
			}
		}
		// 生成jpeg图片
		try {
			Date date=new Date();
			long time=date.getTime();
			String imgPath=imgFilePath+time+".jpeg";
			OutputStream out = new FileOutputStream(imgPath);
			out.write(b);
			out.flush();
			out.close();
			map.put("imgPath", imgPath);
		} catch (IOException e) {
			e.printStackTrace();
			map.put("错误", "请重新请求");
			return map;
		}
		map.put("成功", "图片路径在imgPath");
		return map;
	}

}

 

转载于:https://my.oschina.net/2892328252/blog/719376

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值