第一范文网 - 专业文章范例文档资料分享平台

base64图片解码及压缩

来源:用户分享 时间:2025/5/23 15:43:01 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

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 = \待处理的图片

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

Random random = new Random(); String sjs = \

for(int i=0; i<10; i++){ String rand = String.valueOf(random.nextInt(10)); sjs += rand; }

String imgFilePath = \新生成的图片

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, \

//Base64.doCompress(imgFilePath, 300, 300, 1, \ //固定值,没有前缀后缀 //Base64.doCompress(imgFilePath, 300, 300, 1, \

}

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; }

/*读取图片信息*/ 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

Image srcFile = ImageIO.read(file);

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); /*压缩后的文件名 */

//加前缀

filePrex =

pst

= +

oldFile.substring(oldFile.indexOf('/')+1, smallIcon

+

filePrex

+

String pst = oldFile.substring(0,oldFile.lastIndexOf('/')+1);

String oldFile.lastIndexOf('.')); newImage

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(); e.printStackTrace(); } catch (IOException e) {

return newImage;

} else { return null; }

搜索更多关于: base64图片解码及压缩 的文档
base64图片解码及压缩.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c2ua557x7oi3h0qq03obg_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top