Java實現(xiàn)的圖片高質(zhì)量縮放類定義與用法示例
本文實例講述了Java實現(xiàn)的圖片高質(zhì)量縮放類定義與用法。分享給大家供大家參考,具體如下:
找了很多都不理想,最后找個到老外寫的,不得不承認老外寫的確實牛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日期與時間操作技巧匯總》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》及《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》。
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
java后端+前端使用WebSocket實現(xiàn)消息推送的詳細流程
后端向前端推送消息就需要長連接,首先想到的就是websocket,下面這篇文章主要給大家介紹了關(guān)于java后端+前端使用WebSocket實現(xiàn)消息推送的詳細流程,需要的朋友可以參考下2022-10-10Spring源碼之事件監(jiān)聽機制詳解(@EventListener實現(xiàn)方式)
這篇文章主要介紹了Spring源碼之事件監(jiān)聽機制(@EventListener實現(xiàn)方式),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08Java面向?qū)ο髮崿F(xiàn)汽車租賃系統(tǒng)
這篇文章主要為大家詳細介紹了Java面向?qū)ο髮崿F(xiàn)汽車租賃系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02