本文目錄一覽:
- 1、java如何實現把一個大圖片壓縮到指定大小的圖片且長寬比不變
- 2、java 可以壓縮gif圖片嗎?
- 3、求助java壓縮圖片存儲大小的方法
- 4、java如何實現把一個大圖片壓縮到指定大小的圖片且長寬比不變?
- 5、java如何實現壓縮圖片且保留圖片原文件屬性,比如拍攝日期
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 可以壓縮gif圖片嗎?
不可以,目前只有用java轉化jpg、png、jpeg格式的文件,gif內部是用幀實現的,不能被壓縮。
求助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如何實現把一個大圖片壓縮到指定大小的圖片且長寬比不變?
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如何實現壓縮圖片且保留圖片原文件屬性,比如拍攝日期
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;public class Zip {
// 壓縮
public static void zip(String zipFileName, String inputFile)
throws Exception {
File f = new File(inputFile);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
zip(out, f, null);
System.out.println(“zip done”);
out.close();
} private static void zip(ZipOutputStream out, File f, String base)
throws Exception {
System.out.println(“zipping ” + f.getAbsolutePath());
if (f != null f.isDirectory()) {
File[] fc = f.listFiles();
if (base != null)
out.putNextEntry(new ZipEntry(base + “/”));
base = base == null ? “” : base + “/”;
for (int i = 0; i fc.length; i++) {
zip(out, fc[i], base + fc[i].getName());
}
} else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
while ((b = in.read()) != -1)
out.write(b);
in.close();
}
} // 解壓
public static void unzip(String zipFileName, String outputDirectory)
throws Exception {
ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
ZipEntry z;
while ((z = in.getNextEntry()) != null) {
String name = z.getName();
if (z.isDirectory()) {
name = name.substring(0, name.length() – 1);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
System.out.println(“MD ” + outputDirectory + File.separator
+ name);
} else {
System.out.println(“unziping ” + z.getName());
File f = new File(outputDirectory + File.separator + name);
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
int b;
while ((b = in.read()) != -1)
out.write(b);
out.close();
}
}
in.close();
} public void deleteFolder(File dir) {
File filelist[] = dir.listFiles();
int listlen = filelist.length;
for (int i = 0; i listlen; i++) {
if (filelist[i].isDirectory()) {
deleteFolder(filelist[i]);
} else {
filelist[i].delete();
}
}
dir.delete();// 刪除當前目錄
} public static void main(String[] args) {
try {
// TestZip t = new TestZip();
// t.zip(“c:\\test.zip”,”c:\\test”);
// t.unzip(“c:\\test.zip”,”c:\\test2″);
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/206974.html