您的位置:首页 > 素材教程 > 详情

java压缩图片大小不改变图片分辨率-求一个Java无损压缩图片的示例,把原图片复制到指定目录,按原

原创:素材网 2 年前

java如何实现把一个大图片压缩到指定大小的图片且长宽比不变

也就是图片压缩,可以不修改任何大小的压缩(速度快),也可等比例修改大小压缩(较慢)

下面这是一段等比例缩小图片的方法。

public String compressPic(String inputDir, String outputDir,

String inputFileName, String outputFileName, int width,

int height, boolean gp,String hzm) {

try {

if (!()) {

return "";

}

Image img = (image);

// 判断图片格式是否正确

if ((null) == -1) {

return "no";

} else {

int newWidth; int newHeight;

// 判断是否是等比缩放

if (gp == true) {

// 为等比缩放计算输出的图片宽度及高度

double rate1 = ((double) (null)) / (double) width ;

double rate2 = ((double) (null)) / (double) height ;

// 根据缩放比率大的进行缩放控制

double rate = rate1 > rate2 ? rate1 : rate2;

newWidth = (int) (((double) (null)) / rate);

newHeight = (int) (((double) (null)) / rate);

} else {

newWidth = (null); // 输出的图片宽度

newHeight = (null); // 输出的图片高度

}

BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, _INT_RGB);

/*

* _SMOOTH 的缩略算法 生成缩略图片的平滑度的

* 优先级比速度高 生成的图片质量比较好 但速度慢

*/

Image im = (newWidth, newHeight, _SMOOTH);

().drawImage(im, 0, 0, null);

FileOutputStream out = new FileOutputStream(outputDir + outputFileName);

//JPEGImageEncoder可适用于其他图片类型的转换

JPEGImageEncoder encoder = (out);

(tag);

();

}

} catch (IOException ex) {

();

}

return "ok";

}

求一个Java无损压缩图片的示例,把原图片复制到指定目录,按原图比例改变尺寸,不影响图片质量。

一般都是大尺寸变小尺寸,肯定有损的。

不知道你的无损是什么意思,意思是变小尺寸了还能变大也像以前一样清晰?

好像有种“矢量图片”就是可以随意放大缩小并且无损,缺点就是图片很占空间。

java压缩png图片

您转换的是图片的后缀名吧?您这样的方式已经把图片的信息删除了!

您去下载一个java图片压缩器吧

或者直接在Java下编辑代码来实习转换

package ;

import ;

import ;

import ;

import ;

import ;

import ;

import ;

import ;

import ;

/**

* 图片工具类

* 压缩图片大小

* @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) {

= autoBuildFileName;

}

public String getInPutFilePath() {

return inPutFilePath;

}

public void setInPutFilePath(String inPutFilePath) {

= inPutFilePath;

}

public String getOutPutFileName() {

return outPutFileName;

}

public void setOutPutFileName(String outPutFileName) {

= outPutFileName;

}

public String getOutPutFilePath() {

return outPutFilePath;

}

public void setOutPutFilePath(String outPutFilePath) {

= outPutFilePath;

}

public int getOutPutFileHeight() {

return outPutFileHeight;

}

public void setOutPutFileHeight(int outPutFileHeight) {

= outPutFileHeight;

}

public int getOutPutFileWidth() {

return outPutFileWidth;

}

public void setOutPutFileWidth(int outPutFileWidth) {

= outPutFileWidth;

}

public boolean isScaleZoom() {

return isScaleZoom;

}

public void setScaleZoom(boolean isScaleZoom) {

= isScaleZoom;

}

public String getInPutFileName() {

return inPutFileName;

}

public void setInPutFileName(String inPutFileName) {

= inPutFileName;

}

/**

* 压缩图片大小

*

* @return boolean

*/

public boolean compressImage() {

boolean flag = false;

try {

if (().equals("")) {

throw new NullPointerException("源文件夹路径不存在。");

}

if (().equals("")) {

throw new NullPointerException("图片文件路径不存在。");

}

if (().equals("")) {

throw new NullPointerException("目标文件夹路径地址为空。");

} else {

if (!(outPutFilePath)) {

throw new FileNotFoundException(outPutFilePath

+ " 文件夹创建失败!");

}

}

if () { // 自动生成文件名

String tempFile[] = getFileNameAndExtName(inPutFileName);

outPutFileName = tempFile[0] + "_cyw." + tempFile[1];

compressPic();

} else {

if (().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 (!()) {

throw new FileNotFoundException(inPutFilePath + inPutFileName

+ " 文件不存在!");

}

Image img = (file);

// 判断图片格式是否正确

if ((null) == -1) {

throw new Exception("文件不可读!");

} else {

int newWidth;

int newHeight;

// 判断是否是等比缩放

if ( == true) {

// 为等比缩放计算输出的图片宽度及高度

double rate1 = ((double) (null))

/ (double) outPutFileWidth + 0.1;

double rate2 = ((double) (null))

/ (double) outPutFileHeight + 0.1;

// 根据缩放比率大的进行缩放控制

double rate = rate1 > rate2 ? rate1 : rate2;

newWidth = (int) (((double) (null)) / rate);

newHeight = (int) (((double) (null)) / rate);

} else {

newWidth = outPutFileWidth; // 输出的图片宽度

newHeight = outPutFileHeight; // 输出的图片高度

}

BufferedImage tag = new BufferedImage((int) newWidth,

(int) newHeight, _INT_RGB);

/*

* _SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢

*/

().drawImage(

(newWidth, newHeight,

_SMOOTH), 0, 0, null);

FileOutputStream out = new FileOutputStream(outPutFilePath

+ outPutFileName);

JPEGImageEncoder encoder = (out);

(tag);

();

}

} catch (IOException ex) {

();

}

}

/**

* 创建文件夹目录

*

* @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 ((".") == -1) {

throw new Exception("源文件名不正确!");

} else {

fileNames[0] = (0, fullFileName

.lastIndexOf("."));

fileNames[1] = fullFileName

.substring((".") + 1);

}

return fileNames;

}

public Image getSourceImage() throws IOException{

//获得源文件

file = new File(inPutFilePath + inPutFileName);

if (!()) {

throw new FileNotFoundException(inPutFilePath + inPutFileName

+ " 文件不存在!");

}

Image img = (file);

return img;

}

/*

* 获得图片大小

* @path :图片路径

*/

public long getPicSize(String path) {

File file = new File(path);

return ();

}

}

//下面是测试程序

package ;

import ;

import ;

public class ImageTest {

public static void main(String[] args) throws IOException {

ZIPImage zip=new ZIPImage("d:\\","1.jpg","d:\\test\\","处理后的图片.jpg",false);

(1000);

(1000);

Image image=();

long size=("d:\\1.jpg");

("处理前的图片大小 width:"+(null));

("处理前的图片大小 height:"+(null));

("处理前的图片容量:"+ size +" bit");

();

}

}

< 上一篇 java压缩图片thumbnails-thumbnails图像处理库的使用 下一篇 > java 图像压缩-求助java压缩图片存储大小的方法
相关推荐
独特的公益海报-公益海报有哪些风格
儿童海报制作简单-六一儿童节的宣传海报怎么做啊
春节海报-春节海报设计理念有哪些?
广府特色美食海报小学生怎么画?小学生海报怎么画
有没有制作海报的手机软件-在手机上做海报用什么软件好
蛋糕店宣传海报手绘-蛋糕活动海报怎么设计图片
最新模板
最新素材