「java图像压缩算法」图像数据压缩方法
本篇文章给大家谈谈java图像压缩算法,以及图像数据压缩方法对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、求助java压缩图片存储大小的方法
- 2、java压缩png图片
- 3、java如何实现把一个大图片压缩到指定大小的图片且长宽比不变?
- 4、java 实现图片zip压缩后,图片容量越来越大
- 5、java如何实现把一个大图片压缩到指定大小的图片且长宽比不变
- 6、java中的压缩原理是什么?
求助java压缩图片存储大小的方法
可以使用Draw这个类,通过改变像素来改变存储大小,实例如下:
public static boolean compressPic(String srcFilePath, String descFilePath) throws IOException {
File file = null;
BufferedImage src = null;
FileOutputStream out = null;
ImageWriter imgWrier;
ImageWriteParam imgWriteParams;
// 指定写图片的方式为 jpg
imgWrier = ImageIO.getImageWritersByFormatName("jpg").next();
imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(
null);
// 要使用压缩,必须指定压缩方式为MODE_EXPLICIT
imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT);
// 这里指定压缩的程度,参数qality是取值0~1范围内,
imgWriteParams.setCompressionQuality((float) 1);
imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED);
ColorModel colorModel =ImageIO.read(new File(srcFilePath)).getColorModel();// ColorModel.getRGBdefault();
// 指定压缩时使用的色彩模式
// imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(
// colorModel, colorModel.createCompatibleSampleModel(16, 16)));
imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(
colorModel, colorModel.createCompatibleSampleModel(16, 16)));
try {
if (isBlank(srcFilePath)) {
return false;
} else {
file = new File(srcFilePath);System.out.println(file.length());
src = ImageIO.read(file);
out = new FileOutputStream(descFilePath);
imgWrier.reset();
// 必须先指定 out值,才能调用write方法, ImageOutputStream可以通过任何
// OutputStream构造
imgWrier.setOutput(ImageIO.createImageOutputStream(out));
// 调用write方法,就可以向输入流写图片
imgWrier.write(null, new IIOImage(src, null, null),
imgWriteParams);
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static boolean isBlank(String string) {
if (string == null || string.length() == 0 || string.trim().equals("")) {
return true;
}
return false;
}
java压缩png图片
您转换的是图片的后缀名吧?您这样的方式已经把图片的信息删除了!
您去下载一个java图片压缩器吧
或者直接在Java下编辑代码来实习转换
package com.sun.util.cyw;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 图片工具类
* 压缩图片大小
* @author Cyw
* @version 1.0
*/
public class ZIPImage {
private File file = null;
private String outPutFilePath;
private String inPutFilePath;
private String inPutFileName;
private boolean autoBuildFileName;
private String outPutFileName;
private int outPutFileWidth = 100; // 默认输出图片宽
private int outPutFileHeight = 100; // 默认输出图片高
private static boolean isScaleZoom = true; // 是否按比例缩放
public ZIPImage() {
outPutFilePath = "";
inPutFilePath = "";
inPutFileName = "";
autoBuildFileName = true;
outPutFileName = "";
}
/**
*
* @param ipfp
* 源文件夹路径
* @param ipfn
* 源文件名
* @param opfp
* 目标文件路径
* @param opfn
* 目标文件名
*/
public ZIPImage(String ipfp, String ipfn, String opfp, String opfn) {
outPutFilePath = opfp;
inPutFilePath = ipfp;
inPutFileName = ipfn;
autoBuildFileName = true;
outPutFileName = opfn;
}
/**
*
* @param ipfp
* 源文件夹路径
* @param ipfn
* 源文件名
* @param opfp
* 目标文件路径
* @param opfn
* 目标文件名
* @param aBFN
* 是否自动生成目标文件名
*/
public ZIPImage(String ipfp, String ipfn, String opfp, String opfn,
boolean aBFN) {
outPutFilePath = opfp;
inPutFilePath = ipfp;
inPutFileName = ipfn;
autoBuildFileName = aBFN;
outPutFileName = opfn;
}
public boolean isAutoBuildFileName() {
return autoBuildFileName;
}
public void setAutoBuildFileName(boolean autoBuildFileName) {
this.autoBuildFileName = autoBuildFileName;
}
public String getInPutFilePath() {
return inPutFilePath;
}
public void setInPutFilePath(String inPutFilePath) {
this.inPutFilePath = inPutFilePath;
}
public String getOutPutFileName() {
return outPutFileName;
}
public void setOutPutFileName(String outPutFileName) {
this.outPutFileName = outPutFileName;
}
public String getOutPutFilePath() {
return outPutFilePath;
}
public void setOutPutFilePath(String outPutFilePath) {
this.outPutFilePath = outPutFilePath;
}
public int getOutPutFileHeight() {
return outPutFileHeight;
}
public void setOutPutFileHeight(int outPutFileHeight) {
this.outPutFileHeight = outPutFileHeight;
}
public int getOutPutFileWidth() {
return outPutFileWidth;
}
public void setOutPutFileWidth(int outPutFileWidth) {
this.outPutFileWidth = outPutFileWidth;
}
public boolean isScaleZoom() {
return isScaleZoom;
}
public void setScaleZoom(boolean isScaleZoom) {
this.isScaleZoom = isScaleZoom;
}
public String getInPutFileName() {
return inPutFileName;
}
public void setInPutFileName(String inPutFileName) {
this.inPutFileName = inPutFileName;
}
/**
* 压缩图片大小
*
* @return boolean
*/
public boolean compressImage() {
boolean flag = false;
try {
if (inPutFilePath.trim().equals("")) {
throw new NullPointerException("源文件夹路径不存在。");
}
if (inPutFileName.trim().equals("")) {
throw new NullPointerException("图片文件路径不存在。");
}
if (outPutFilePath.trim().equals("")) {
throw new NullPointerException("目标文件夹路径地址为空。");
} else {
if (!ZIPImage.mddir(outPutFilePath)) {
throw new FileNotFoundException(outPutFilePath
+ " 文件夹创建失败!");
}
}
if (this.autoBuildFileName) { // 自动生成文件名
String tempFile[] = getFileNameAndExtName(inPutFileName);
outPutFileName = tempFile[0] + "_cyw." + tempFile[1];
compressPic();
} else {
if (outPutFileName.trim().equals("")) {
throw new NullPointerException("目标文件名为空。");
}
compressPic();
}
} catch (Exception e) {
flag = false;
e.printStackTrace();
return flag;
}
return flag;
}
// 图片处理
private void compressPic() throws Exception {
try {
// 获得源文件
file = new File(inPutFilePath + inPutFileName);
if (!file.exists()) {
throw new FileNotFoundException(inPutFilePath + inPutFileName
+ " 文件不存在!");
}
Image img = ImageIO.read(file);
// 判断图片格式是否正确
if (img.getWidth(null) == -1) {
throw new Exception("文件不可读!");
} else {
int newWidth;
int newHeight;
// 判断是否是等比缩放
if (ZIPImage.isScaleZoom == true) {
// 为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) img.getWidth(null))
/ (double) outPutFileWidth + 0.1;
double rate2 = ((double) img.getHeight(null))
/ (double) outPutFileHeight + 0.1;
// 根据缩放比率大的进行缩放控制
double rate = rate1 rate2 ? rate1 : rate2;
newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);
} else {
newWidth = outPutFileWidth; // 输出的图片宽度
newHeight = outPutFileHeight; // 输出的图片高度
}
BufferedImage tag = new BufferedImage((int) newWidth,
(int) newHeight, BufferedImage.TYPE_INT_RGB);
/*
* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
*/
tag.getGraphics().drawImage(
img.getScaledInstance(newWidth, newHeight,
Image.SCALE_SMOOTH), 0, 0, null);
FileOutputStream out = new FileOutputStream(outPutFilePath
+ outPutFileName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* 创建文件夹目录
*
* @param filePath
* @return
* @throws Exception
*/
@SuppressWarnings("unused")
private static boolean mddir(String filePath) throws Exception {
boolean flag = false;
File f = new File(filePath);
if (!f.exists()) {
flag = f.mkdirs();
} else {
flag = true;
}
return flag;
}
/**
* 获得文件名和扩展名
*
* @param fullFileName
* @return
* @throws Exception
*/
private String[] getFileNameAndExtName(String fullFileName)
throws Exception {
String[] fileNames = new String[2];
if (fullFileName.indexOf(".") == -1) {
throw new Exception("源文件名不正确!");
} else {
fileNames[0] = fullFileName.substring(0, fullFileName
.lastIndexOf("."));
fileNames[1] = fullFileName
.substring(fullFileName.lastIndexOf(".") + 1);
}
return fileNames;
}
public Image getSourceImage() throws IOException{
//获得源文件
file = new File(inPutFilePath + inPutFileName);
if (!file.exists()) {
throw new FileNotFoundException(inPutFilePath + inPutFileName
+ " 文件不存在!");
}
Image img = ImageIO.read(file);
return img;
}
/*
* 获得图片大小
* @path :图片路径
*/
public long getPicSize(String path) {
File file = new File(path);
return file.length();
}
}
//下面是测试程序
package com.sun.util.cyw;
import java.awt.Image;
import java.io.IOException;
public class ImageTest {
public static void main(String[] args) throws IOException {
ZIPImage zip=new ZIPImage("d:\\","1.jpg","d:\\test\\","处理后的图片.jpg",false);
zip.setOutPutFileWidth(1000);
zip.setOutPutFileHeight(1000);
Image image=zip.getSourceImage();
long size=zip.getPicSize("d:\\1.jpg");
System.out.println("处理前的图片大小 width:"+image.getWidth(null));
System.out.println("处理前的图片大小 height:"+image.getHeight(null));
System.out.println("处理前的图片容量:"+ size +" bit");
zip.compressImage();
}
}
java如何实现把一个大图片压缩到指定大小的图片且长宽比不变?
java要实现把一个大图片压缩到指定大小的图片且长宽比不变可以尝试以下操作:
建立一个AffineTransform
AffineTransform(double m00, double m10, double m01, double m11, double m02, double m12)
转换矩阵,缩放比较简单(矩阵可以干很多事情,想做图像处理软件可以研究下)
[ x'] [ m00 m01 m02 ] [ x ] [ m00x + m01y + m02 ]
[ y'] = [ m10 m11 m12 ] [ y ] = [ m10x + m11y + m12 ]
[ 1 ] [ 0 0 1 ] [ 1 ] [ 1 ]
10倍比较难算(根号10啊,当然你想算也行),9倍好点(9的开方是3),m00为1/3,m01为0,m02为0,m10为0,m11为1/3,m12为0。
再建一个AffineTransformOp,把上面的转换传进去
AffineTransformOp(AffineTransform xform, int interpolationType)
最后调用AffineTransformOp的BufferedImage filter(BufferedImage src, BufferedImage dst) ,src传原图片,返回值就是想要的Image,注意是返回值,不是dst,不明白可以看下Java API
java 实现图片zip压缩后,图片容量越来越大
这个跟压缩算法有关系,其中有一些小体积文件确实会产生压缩后体积增大的现象。
java如何实现把一个大图片压缩到指定大小的图片且长宽比不变
也就是图片压缩,可以不修改任何大小的压缩(速度快),也可等比例修改大小压缩(较慢)
下面这是一段等比例缩小图片的方法。
public String compressPic(String inputDir, String outputDir,
String inputFileName, String outputFileName, int width,
int height, boolean gp,String hzm) {
try {
if (!image.exists()) {
return "";
}
Image img = ImageIO.read(image);
// 判断图片格式是否正确
if (img.getWidth(null) == -1) {
return "no";
} else {
int newWidth; int newHeight;
// 判断是否是等比缩放
if (gp == true) {
// 为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) img.getWidth(null)) / (double) width ;
double rate2 = ((double) img.getHeight(null)) / (double) height ;
// 根据缩放比率大的进行缩放控制
double rate = rate1 rate2 ? rate1 : rate2;
newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);
} else {
newWidth = img.getWidth(null); // 输出的图片宽度
newHeight = img.getHeight(null); // 输出的图片高度
}
BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
/*
* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的
* 优先级比速度高 生成的图片质量比较好 但速度慢
*/
Image im = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
tag.getGraphics().drawImage(im, 0, 0, null);
FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
//JPEGImageEncoder可适用于其他图片类型的转换
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return "ok";
}
java中的压缩原理是什么?
什么是压缩文件? 简单的说,就是经过压缩软件压缩的文件叫压缩文件,压缩的原理是把文件的二进制代码压缩,把相邻的0,1代码减少,比如有000000,可以把它变成6个0 的写法60,来减少该文件的空间。 ■怎么压缩文件? 首先要安装压缩软件,现在比较流行的是WinRAR「一种高效快速的文件压缩软件(中文版)」。 其次是建立一个压缩包:选择你要制作成压缩包的文件或文件夹,当然你也可也多选,方法同资源管理器,也就是按住Ctrl或Shift再选择文件(文件夹)。 选取完毕之后,就可以单击工具栏上的“压缩”按钮,在这里你可以选择压缩格式:RAR和ZIP。 如果你想得到较大的压缩率,建议选择RAR格式。 各个选项选择好以后,单击确定按钮就开始制作压缩包了,非常方便。有时候大家会遇到这个问题,就是你在一个论坛里要上传一些文件压缩包,压缩包大小有3M,但是论坛限制会员上传大小只有2M,怎么办呢? 其实办法很简单,就是在你压缩这个文件时,分成几个带分卷压缩包,分卷包大小设置为2M即可,比如:原来文件名为123.rar(3M),压缩成分卷包后为123.part1.rar(2M)与123.part2.rar(1M)两个文件,这样你就可以上传了。 具体方法如下: 1、在要压缩的文件上点右键 2、添加到压缩文件.... 3、选常规 4、压缩方式选最好 5、批定压缩分卷大小(按字节计算),1M = 1024K,1K = 1024字节,填写数字即可 当你下载了带有分卷的压缩包后,如何解压文件呢? 具体方法如下: 1、把所有的压缩分卷全部下载完整 2、所有分卷必须在同一个文件夹内 3、然后双击解压第一个分卷,即可 注:分卷解压的文件必须是连续的,若分卷未下载完整,则解压时自然会提示需要下一压缩分卷
java图像压缩算法的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于图像数据压缩方法、java图像压缩算法的信息别忘了在本站进行查找喔。
发布于:2022-11-25,除非注明,否则均为
原创文章,转载请注明出处。