Java實(shí)現(xiàn)的圖片高質(zhì)量縮放類定義與用法示例
本文實(shí)例講述了Java實(shí)現(xiàn)的圖片高質(zhì)量縮放類定義與用法。分享給大家供大家參考,具體如下:
找了很多都不理想,最后找個(gè)到老外寫的,不得不承認(rèn)老外寫的確實(shí)牛B。
package com.test;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.Kernel;
import java.awt.image.ConvolveOp;
public class ImageUtil {
public static void resize(File originalFile, File resizedFile,
int newWidth, float quality) throws IOException {
if (quality > 1) {
throw new IllegalArgumentException(
"Quality has to be between 0 and 1");
}
ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
Image i = ii.getImage();
Image resizedImage = null;
int iWidth = i.getWidth(null);
int iHeight = i.getHeight(null);
if (iWidth > iHeight) {
resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
/ iWidth, Image.SCALE_SMOOTH);
} else {
resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight,
newWidth, Image.SCALE_SMOOTH);
}
// This code ensures that all the pixels in the image are loaded.
Image temp = new ImageIcon(resizedImage).getImage();
// Create the buffered image.
BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
// Copy image to buffered image.
Graphics g = bufferedImage.createGraphics();
// Clear background and paint the image.
g.setColor(Color.white);
g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose();
// Soften.
float softenFactor = 0.05f;
float[] softenArray = { 0, softenFactor, 0, softenFactor,
1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
Kernel kernel = new Kernel(3, 3, softenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
bufferedImage = cOp.filter(bufferedImage, null);
// Write the jpeg to a file.
FileOutputStream out = new FileOutputStream(resizedFile);
// Encodes image as a JPEG data stream
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder
.getDefaultJPEGEncodeParam(bufferedImage);
param.setQuality(quality, true);
encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);
} // Example usage
public static void main(String[] args) throws IOException {
// File originalImage = new File("C:\\11.jpg");
// resize(originalImage, new File("c:\\11-0.jpg"),150, 0.7f);
// resize(originalImage, new File("c:\\11-1.jpg"),150, 1f);
File originalImage = new File("C:\\1207.gif");
resize(originalImage, new File("c:\\1207-0.jpg"),150, 0.7f);
resize(originalImage, new File("c:\\1207-1.jpg"),150, 1f);
}
}
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java圖片操作技巧匯總》、《java日期與時(shí)間操作技巧匯總》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》及《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》。
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
- Java實(shí)現(xiàn)圖片比率縮放
- java 實(shí)現(xiàn)圖片像素質(zhì)量壓縮與圖片長寬縮放
- java高質(zhì)量縮放圖片的示例代碼
- java實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)和馬賽克化
- Java實(shí)現(xiàn)的微信圖片處理工具類【裁剪,合并,等比例縮放等】
- java對圖片進(jìn)行壓縮和resize縮放的方法
- java圖片縮放實(shí)現(xiàn)圖片填充整個(gè)屏幕
- Java圖片處理 (文字水印、圖片水印、縮放、補(bǔ)白)代碼實(shí)例
- 簡單的java圖片處理類(圖片水印 圖片縮放)
- java項(xiàng)目實(shí)現(xiàn)圖片等比縮放
相關(guān)文章
java后端+前端使用WebSocket實(shí)現(xiàn)消息推送的詳細(xì)流程
后端向前端推送消息就需要長連接,首先想到的就是websocket,下面這篇文章主要給大家介紹了關(guān)于java后端+前端使用WebSocket實(shí)現(xiàn)消息推送的詳細(xì)流程,需要的朋友可以參考下2022-10-10
Spring源碼之事件監(jiān)聽機(jī)制詳解(@EventListener實(shí)現(xiàn)方式)
這篇文章主要介紹了Spring源碼之事件監(jiān)聽機(jī)制(@EventListener實(shí)現(xiàn)方式),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Java面向?qū)ο髮?shí)現(xiàn)汽車租賃系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java面向?qū)ο髮?shí)現(xiàn)汽車租賃系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

