自己實現圖片壓縮技術,直接上程式碼,絕對好用(優化效果不錯)
package photo.zipcode;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.BufferedImage;
/**
*
* @author zcw
*/
public class Img_Middle {
public void img_change(String url, String name) {
Tosmallerpic(url, new File(url + name), "_middle", name, 188, 165,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_small", name, 45, 45,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_smaller", name, 116, 100,
(float) 0.7);
}
/**
* 將檔案放到一個資料夾,自動生成三種不同的大小壓縮圖片
*
* @param url
* @param name
*/
private static void img_change11(String url, String name) {
File f = new File(url + name);
float per = (300 * 1024) / f.length();
if (f.length() > 300 * 1024) {
per = (float) (300 * 1024) / f.length();
per = (float) (per + (1 - per) / 5);
} else {
per = (float) 1.0;
}
System.out.println("圖片大小:" + f.length() / 1024 + "K");
System.out.println("壓宿比例:" + per);
// Tosmallerpic(url,new
// File(url+name),"_middle",name,1440,900,(float)0.7);
Tosmallerpic(url, new File(url + name), "_small", name, 1024, 1024,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_smaller", name, 1024, 500,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_middle", name, 1024, 640,
(float) per);
}
private static void img_changeOne(String url, String name) {
File f = new File(url + name);
float per = (300 * 1024) / f.length();
if (f.length() > 300 * 1024) {
per = (float) (300 * 1024) / f.length();
per = (float) (per + (1 - per) / 5);
} else {
per = (float) 1.0;
}
System.out.println("圖片大小:" + f.length() / 1024 + "K");
System.out.println("壓宿比例:" + per);
// Tosmallerpic(url,new
// File(url+name),"_middle",name,1440,900,(float)0.7);
Tosmallerpic(url, new File(url + name), "_small", name, 500, 375,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_smaller", name, 500, 375,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_middle", name, 500, 375,
(float) per);
}
/**
* 壓縮圖片將圖片寬高和路徑名字傳入
*
* @param url
* @param name
* @param w
* @param h
*/
private static void img_changeMyself(String url, String name, int w, int h) {
File f = new File(url + name);
float per = (300 * 1024) / f.length();
if (f.length() > 300 * 1024) {
per = (float) (300 * 1024) / f.length();
per = (float) (per + (1 - per) / 5);
} else {
per = (float) 1.0;
}
System.out.println("圖片大小:" + f.length() / 1024 + "K");
System.out.println("壓宿比例:" + per);
// Tosmallerpic(url,new
// File(url+name),"_middle",name,1440,900,(float)0.7);
Tosmallerpic(url, new File(url + name), "_small", name, w, h,
(float) 0.7);
// Tosmallerpic(url,new
// File(url+name),"_smaller",name,500,375,(float)0.7);
// Tosmallerpic(url, new File(url + name), "", name, w, h, (float) per);
}
/**
* 生成新的圖片江河源圖片檔名字一致,放在當前資料夾的test臨時目錄
*
* @param f
* 圖片所在的資料夾路徑
* @param filelist
* 圖片路徑
* @param ext
* 副檔名
* @param n
* 圖片名
* @param w
* 目標寬
* @param h
* 目標高
* @param per
* 百分比
*/
private static void Tosmallerpic(String f, File filelist, String ext,
String n, int w, int h, float per) {
Image src;
try {
src = javax.imageio.ImageIO.read(filelist); // 構造Image物件
String img_midname = f + "test/" + n.substring(0, n.indexOf("."))
+ n.substring(n.indexOf("."));
int old_w = src.getWidth(null); // 得到源圖寬
int old_h = src.getHeight(null);
int new_w = 0;
int new_h = 0; // 得到源圖長
double w2 = (old_w * 1.00) / (w * 1.00);
double h2 = (old_h * 1.00) / (h * 1.00);
// 圖片跟據長寬留白,成一個正方形圖。
BufferedImage oldpic;
/*
* if(old_w>old_h) { oldpic=new
* BufferedImage(old_w,old_w,BufferedImage.TYPE_INT_RGB); }else{
* if(old_w<old_h){ oldpic=new
* BufferedImage(old_h,old_h,BufferedImage.TYPE_INT_RGB); }else{
* oldpic=new BufferedImage(old_w,old_h,BufferedImage.TYPE_INT_RGB);
* } }
*/
oldpic = new BufferedImage(old_w, old_h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = oldpic.createGraphics();
g.setColor(Color.white);
if (old_w > old_h) {
g.fillRect(0, 0, old_w, old_w);
g.drawImage(src, 0, 0, old_w, old_h, Color.white, null);
// g.fillRect(0, 0, old_w, old_w);
// g.drawImage(src, 0, (old_w - old_h) / 2, old_w, old_h,
// Color.white, null);
} else {
if (old_w < old_h) {
g.fillRect(0, 0, old_h, old_h);
g.drawImage(src, 0, 0, old_w, old_h, Color.white, null);
// g.fillRect(0,0,old_h,old_h);
// g.drawImage(src, (old_h - old_w) / 2, 0, old_w, old_h,
// Color.white, null);
} else {
// g.fillRect(0,0,old_h,old_h);
g.drawImage(src.getScaledInstance(old_w, old_h,
Image.SCALE_SMOOTH), 0, 0, null);
}
}
g.fillRect(0, 0, old_w, old_h);
g.drawImage(src, 0, 0, old_w, old_h, Color.white, null);
g.dispose();
src = oldpic;
// 圖片調整為方形結束
if (old_w > w)
new_w = (int) Math.round(old_w / w2);
else
new_w = old_w;
if (old_h > h)
new_h = (int) Math.round(old_h / h2);// 計算新圖長寬
else
new_h = old_h;
new_h = h;
new_w = w;
BufferedImage tag = new BufferedImage(new_w, new_h,
BufferedImage.TYPE_INT_RGB);
// tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //繪製縮小後的圖
tag.getGraphics().drawImage(
src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0,
0, null);
FileOutputStream newimage = new FileOutputStream(img_midname); // 輸出到檔案流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
/* 壓縮質量 */
jep.setQuality(per, true);
encoder.encode(tag, jep);
// encoder.encode(tag); //近JPEG編碼
newimage.close();
} catch (IOException ex) {
Logger.getLogger(Img_Middle.class.getName()).log(Level.SEVERE,
null, ex);
}
}
/**/public static void main(String args[]) {
// String n = "xiangshou1.png";
String f = "G:/map/";
File file = new File(f);
// img_changeOne(f, n);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].exists() && files[i].isFile()) {
System.err.println(files[i].getName());
img_changeMyself(f, files[i].getName(), 1202, 765);
}
}
// img_changeMyself(f, n);
/*
* if(file.exists()) { File[] filelist=file.listFiles(); for(int
* i=0;i<filelist.length;i++) { String n=filelist[i].getName();
* Tosmallerpic(f,filelist[i],"_middle",n,185,160,(float)0.7);
* Tosmallerpic(f,filelist[i],"_small",n,45,45,(float)0.7);
* Tosmallerpic(f,filelist[i],"_smaller",n,116,100,(float)0.7); } }
*/
}
}
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.BufferedImage;
/**
*
* @author zcw
*/
public class Img_Middle {
public void img_change(String url, String name) {
Tosmallerpic(url, new File(url + name), "_middle", name, 188, 165,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_small", name, 45, 45,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_smaller", name, 116, 100,
(float) 0.7);
}
/**
* 將檔案放到一個資料夾,自動生成三種不同的大小壓縮圖片
*
* @param url
* @param name
*/
private static void img_change11(String url, String name) {
File f = new File(url + name);
float per = (300 * 1024) / f.length();
if (f.length() > 300 * 1024) {
per = (float) (300 * 1024) / f.length();
per = (float) (per + (1 - per) / 5);
} else {
per = (float) 1.0;
}
System.out.println("圖片大小:" + f.length() / 1024 + "K");
System.out.println("壓宿比例:" + per);
// Tosmallerpic(url,new
// File(url+name),"_middle",name,1440,900,(float)0.7);
Tosmallerpic(url, new File(url + name), "_small", name, 1024, 1024,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_smaller", name, 1024, 500,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_middle", name, 1024, 640,
(float) per);
}
private static void img_changeOne(String url, String name) {
File f = new File(url + name);
float per = (300 * 1024) / f.length();
if (f.length() > 300 * 1024) {
per = (float) (300 * 1024) / f.length();
per = (float) (per + (1 - per) / 5);
} else {
per = (float) 1.0;
}
System.out.println("圖片大小:" + f.length() / 1024 + "K");
System.out.println("壓宿比例:" + per);
// Tosmallerpic(url,new
// File(url+name),"_middle",name,1440,900,(float)0.7);
Tosmallerpic(url, new File(url + name), "_small", name, 500, 375,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_smaller", name, 500, 375,
(float) 0.7);
Tosmallerpic(url, new File(url + name), "_middle", name, 500, 375,
(float) per);
}
/**
* 壓縮圖片將圖片寬高和路徑名字傳入
*
* @param url
* @param name
* @param w
* @param h
*/
private static void img_changeMyself(String url, String name, int w, int h) {
File f = new File(url + name);
float per = (300 * 1024) / f.length();
if (f.length() > 300 * 1024) {
per = (float) (300 * 1024) / f.length();
per = (float) (per + (1 - per) / 5);
} else {
per = (float) 1.0;
}
System.out.println("圖片大小:" + f.length() / 1024 + "K");
System.out.println("壓宿比例:" + per);
// Tosmallerpic(url,new
// File(url+name),"_middle",name,1440,900,(float)0.7);
Tosmallerpic(url, new File(url + name), "_small", name, w, h,
(float) 0.7);
// Tosmallerpic(url,new
// File(url+name),"_smaller",name,500,375,(float)0.7);
// Tosmallerpic(url, new File(url + name), "", name, w, h, (float) per);
}
/**
* 生成新的圖片江河源圖片檔名字一致,放在當前資料夾的test臨時目錄
*
* @param f
* 圖片所在的資料夾路徑
* @param filelist
* 圖片路徑
* @param ext
* 副檔名
* @param n
* 圖片名
* @param w
* 目標寬
* @param h
* 目標高
* @param per
* 百分比
*/
private static void Tosmallerpic(String f, File filelist, String ext,
String n, int w, int h, float per) {
Image src;
try {
src = javax.imageio.ImageIO.read(filelist); // 構造Image物件
String img_midname = f + "test/" + n.substring(0, n.indexOf("."))
+ n.substring(n.indexOf("."));
int old_w = src.getWidth(null); // 得到源圖寬
int old_h = src.getHeight(null);
int new_w = 0;
int new_h = 0; // 得到源圖長
double w2 = (old_w * 1.00) / (w * 1.00);
double h2 = (old_h * 1.00) / (h * 1.00);
// 圖片跟據長寬留白,成一個正方形圖。
BufferedImage oldpic;
/*
* if(old_w>old_h) { oldpic=new
* BufferedImage(old_w,old_w,BufferedImage.TYPE_INT_RGB); }else{
* if(old_w<old_h){ oldpic=new
* BufferedImage(old_h,old_h,BufferedImage.TYPE_INT_RGB); }else{
* oldpic=new BufferedImage(old_w,old_h,BufferedImage.TYPE_INT_RGB);
* } }
*/
oldpic = new BufferedImage(old_w, old_h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = oldpic.createGraphics();
g.setColor(Color.white);
if (old_w > old_h) {
g.fillRect(0, 0, old_w, old_w);
g.drawImage(src, 0, 0, old_w, old_h, Color.white, null);
// g.fillRect(0, 0, old_w, old_w);
// g.drawImage(src, 0, (old_w - old_h) / 2, old_w, old_h,
// Color.white, null);
} else {
if (old_w < old_h) {
g.fillRect(0, 0, old_h, old_h);
g.drawImage(src, 0, 0, old_w, old_h, Color.white, null);
// g.fillRect(0,0,old_h,old_h);
// g.drawImage(src, (old_h - old_w) / 2, 0, old_w, old_h,
// Color.white, null);
} else {
// g.fillRect(0,0,old_h,old_h);
g.drawImage(src.getScaledInstance(old_w, old_h,
Image.SCALE_SMOOTH), 0, 0, null);
}
}
g.fillRect(0, 0, old_w, old_h);
g.drawImage(src, 0, 0, old_w, old_h, Color.white, null);
g.dispose();
src = oldpic;
// 圖片調整為方形結束
if (old_w > w)
new_w = (int) Math.round(old_w / w2);
else
new_w = old_w;
if (old_h > h)
new_h = (int) Math.round(old_h / h2);// 計算新圖長寬
else
new_h = old_h;
new_h = h;
new_w = w;
BufferedImage tag = new BufferedImage(new_w, new_h,
BufferedImage.TYPE_INT_RGB);
// tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //繪製縮小後的圖
tag.getGraphics().drawImage(
src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0,
0, null);
FileOutputStream newimage = new FileOutputStream(img_midname); // 輸出到檔案流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
/* 壓縮質量 */
jep.setQuality(per, true);
encoder.encode(tag, jep);
// encoder.encode(tag); //近JPEG編碼
newimage.close();
} catch (IOException ex) {
Logger.getLogger(Img_Middle.class.getName()).log(Level.SEVERE,
null, ex);
}
}
/**/public static void main(String args[]) {
// String n = "xiangshou1.png";
String f = "G:/map/";
File file = new File(f);
// img_changeOne(f, n);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].exists() && files[i].isFile()) {
System.err.println(files[i].getName());
img_changeMyself(f, files[i].getName(), 1202, 765);
}
}
// img_changeMyself(f, n);
/*
* if(file.exists()) { File[] filelist=file.listFiles(); for(int
* i=0;i<filelist.length;i++) { String n=filelist[i].getName();
* Tosmallerpic(f,filelist[i],"_middle",n,185,160,(float)0.7);
* Tosmallerpic(f,filelist[i],"_small",n,45,45,(float)0.7);
* Tosmallerpic(f,filelist[i],"_smaller",n,116,100,(float)0.7); } }
*/
}
}
相關文章
- JNI實現圖片壓縮
- 利用 canvas 實現圖片壓縮Canvas
- 前端的圖片壓縮image-compressor(可在圖片上傳前實現圖片壓縮)前端
- 效能優化 (十一) ProGuard 對程式碼和資源壓縮優化
- 滑鼠懸浮圖片實現縮放效果
- 純前端實現 JPG 圖片壓縮 | canvas前端Canvas
- 圖片純前端JS壓縮的實現前端JS
- layui中實現上傳圖片壓縮UI
- 10 行 Python 程式碼,批量壓縮圖片 500 張,簡直太強大了Python
- Linux下的好用的圖片壓縮軟體Linux
- PHP實現高畫質晰度無損圖片壓縮功能的程式碼PHP
- Java 實現壓縮圖片,影片,音訊案例Java音訊
- 前端圖片壓縮 - H5&Uni-App圖片壓縮前端H5APP
- Nginx網路壓縮 CSS壓縮 圖片壓縮 JSON壓縮NginxCSSJSON
- 圖片壓縮怎樣操作?分享幾種實用的批次圖片壓縮技巧
- canvas 壓縮圖片Canvas
- 圖片壓縮20201109
- ??圖片壓縮CanvasCanvas
- 純前端實現 PNG 圖片壓縮 | UPNG.js前端JS
- Flutter實現Luban圖片壓縮庫演算法Flutter演算法
- 【Java技術專題】「效能優化系列」針對Java物件壓縮及序列化技術的探索之路Java優化物件
- Glide 原始碼分析(一):圖片壓縮IDE原始碼
- 用ASP實現線上壓縮與解壓縮功能程式碼
- 前端圖片壓縮方案前端
- Photoshop壓縮png圖片
- iOS 圖片壓縮方法iOS
- 影像壓縮編碼碼matlab實現——算術編碼Matlab
- 兩行 CSS 程式碼實現圖片任意顏色賦色技術CSS
- JPEG圖片的解碼和壓縮流程
- 觸屏 H5 如何實現壓縮並上傳多張圖片,同時還可以進行圖片壓縮H5
- HarmonyOS NEXT應用開發之圖片縮放效果實現
- Vue使用cube-ui的upload元件實現圖片壓縮VueUI元件
- JavaScript實現手機拍攝圖片的旋轉、壓縮JavaScript
- 【Node】簡單快捷的圖片壓縮指令碼指令碼
- CSS實現圖片等比例縮小不變形CSS
- SmallImage for Mac(圖片壓縮工具)Mac
- js圖片壓縮推薦JS
- js上傳圖片壓縮JS
- vue 上傳圖片進行壓縮圖片Vue