收藏 分销(赏)

base64图片解码及压缩.doc

上传人:pc****0 文档编号:7434898 上传时间:2025-01-04 格式:DOC 页数:4 大小:24.50KB 下载积分:10 金币
下载 相关 举报
base64图片解码及压缩.doc_第1页
第1页 / 共4页
base64图片解码及压缩.doc_第2页
第2页 / 共4页


点击查看更多>>
资源描述
package poi; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Random; import javax.imageio.ImageIO; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class Base64 { /** * @param args * @throws IOException */ public static void main(String[] args){ String imgFile = "E:\\1.png";//待处理的图片 InputStream in = null; byte[] data = null; //读取图片字节数组 try { in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } //对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); String imgStr = encoder.encode(data); BASE64Decoder decoder = new BASE64Decoder(); //Base64解码 try { byte[] b = decoder.decodeBuffer(imgStr); for(int i=0;i<b.length;++i){ //调整异常数据 if(b[i]<0){ b[i]+=256; } } Random random = new Random(); String sjs = ""; for(int i=0; i<10; i++){ String rand = String.valueOf(random.nextInt(10)); sjs += rand; } String imgFilePath = "E:/" + sjs + ".jpg";//新生成的图片 OutputStream out = new FileOutputStream(imgFilePath); out.write(b); out.flush(); out.close(); InputStream file = new FileInputStream(imgFilePath); if(file.available() > 200000){ Base64.doCompress(imgFilePath, 300, 300, 1, "S_", true); //Base64.doCompress(imgFilePath, 300, 300, 1, "_S", true); //固定值,没有前缀后缀 //Base64.doCompress(imgFilePath, 300, 300, 1, "", false); } file.close(); }catch (Exception e){ e.printStackTrace(); } } public static String doCompress(String oldFile, int width, int height, float quality, String smallIcon, boolean percentage) { if (oldFile != null && width > 0 && height > 0) { String newImage = null; try { File file = new File(oldFile); // 文件不存在 if (!file.exists()) { return null; } /*读取图片信息*/ Image srcFile = ImageIO.read(file); int new_w = width; int new_h = height; if (percentage) { // 为等比缩放计算输出的图片宽度及高度 //接收固定值等比 //double rate1 = ((double) srcFile.getWidth(null)) / (double) width + 0.1; //double rate2 = ((double) srcFile.getHeight(null)) / (double) height + 0.1; //double rate = rate1 > rate2 ? rate1 : rate2; //new_w = (int) (((double) srcFile.getWidth(null)) / rate); //new_h = (int) (((double) srcFile.getHeight(null)) / rate); //原图/1.1 new_w = (int) (((double) srcFile.getWidth(null)) / 1.1); new_h = (int) (((double) srcFile.getHeight(null)) / 1.1); } /*宽高设定*/ BufferedImage tag = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(srcFile, 0, 0, new_w, new_h, null); /*压缩后的文件名 */ //加前缀 String pst = oldFile.substring(0,oldFile.lastIndexOf('/')+1); String filePrex = oldFile.substring(oldFile.indexOf('/')+1, oldFile.lastIndexOf('.')); newImage = pst + smallIcon + filePrex + oldFile.substring(oldFile.lastIndexOf('.')); //加后缀 //String filePrex = oldFile.substring(0, oldFile.lastIndexOf('.')); //newImage = filePrex + smallIcon + oldFile.substring(filePrex.length()); /*压缩之后临时存放位置*/ FileOutputStream out = new FileOutputStream(newImage); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag); /*压缩质量 */ jep.setQuality(quality, true); encoder.encode(tag, jep); out.close(); srcFile.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return newImage; } else { return null; } } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服