【轉】JAVA生成縮略圖

方法1:[第一種方法比后一種生成的縮略圖要清晰]
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.io.InputStream;
import java.io.File;
import java.io.FileOutputStream;

public class Test {
?public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
?// targetW,targetH分別表示目標長和寬
?int type = source.getType();
?BufferedImage target = null;
?double sx = (double) targetW / source.getWidth();
?double sy = (double) targetH / source.getHeight();
?//這里想實現在targetW,targetH范圍內實現等比縮放。如果不需要等比縮放
?//則將下面的if else語句注釋即可
?if(sx>sy)
?{
?sx = sy;
?targetW = (int)(sx * source.getWidth());
?}else{
?sy = sx;
?targetH = (int)(sy * source.getHeight());
?}
?if (type == BufferedImage.TYPE_CUSTOM) { //handmade
?ColorModel cm = source.getColorModel();
?WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
?boolean alphaPremultiplied = cm.isAlphaPremultiplied();
?target = new BufferedImage(cm, raster, alphaPremultiplied, null);
?} else
?target = new BufferedImage(targetW, targetH, type);
?Graphics2D g = target.createGraphics();
?//smoother than exlax:
?g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
?g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
?g.dispose();
?return target;
?}
?public static void saveImageAsJpg (String fromFileStr,String saveToFileStr,int width,int hight)
?throws Exception {
?BufferedImage srcImage;
// String ex = fromFileStr.substring(fromFileStr.indexOf("."),fromFileStr.length());
?String imgType = "JPEG";
?if (fromFileStr.toLowerCase().endsWith(".png")) {
?imgType = "PNG";
?}
// System.out.println(ex);
?File saveFile=new File(saveToFileStr);
?File fromFile=new File(fromFileStr);
?srcImage = ImageIO.read(fromFile);
?if(width > 0 || hight > 0)
?{
?srcImage = resize(srcImage, width, hight);
?}
?ImageIO.write(srcImage, imgType, saveFile);

?}
?
?public static void main (String argv[]) {
?try{
?//參數1(from),參數2(to),參數3(寬),參數4(高)
?Test.saveImageAsJpg("E:/Document/My Pictures/3.gif","c:/6.gif",50,50);
?} catch(Exception e)
?{
?e.printStackTrace();
?}

?}
}

方法2:
import java.io.*;
?import java.util.*;
?import com.sun.image.codec.jpeg.*;
?import java.awt.image.*;
?import java.awt.*;
?import java.net.*;
?import java.applet.*;
?import java.sql.*;

//縮略圖類,
//本java類能將jpg圖片文件,進行等比或非等比的大小轉換。
//具體使用方法
//s_pic(大圖片路徑,生成小圖片路徑,大圖片文件名,生成小圖片文名,生成小圖片寬度,生成小圖片高度,是否等比縮放(默認為true))
?public class Tes {
?String InputDir; //輸入圖路徑
?String OutputDir; //輸出圖路徑
?String InputFileName; //輸入圖文件名
?String OutputFileName; //輸出圖文件名
?int OutputWidth = 80; //默認輸出圖片寬
?int OutputHeight = 80; //默認輸出圖片高
?int rate = 0;
?boolean proportion = true; //是否等比縮放標記(默認為等比縮放)

?public Tes() {
//初始化變量
?InputDir = "";
?OutputDir = "";
?InputFileName = "";
?OutputFileName = "";
?OutputWidth = 80;
?OutputHeight = 80;
?rate = 0;
?}

?public void setInputDir(String InputDir) {
?this.InputDir = InputDir;
?}

?public void setOutputDir(String OutputDir) {
?this.OutputDir = OutputDir;
?}

?public void setInputFileName(String InputFileName) {
?this.InputFileName = InputFileName;
?}

?public void setOutputFileName(String OutputFileName) {
?this.OutputFileName = OutputFileName;
?}

?public void setOutputWidth(int OutputWidth) {
?this.OutputWidth = OutputWidth;
?}

?public void setOutputHeight(int OutputHeight) {
?this.OutputHeight = OutputHeight;
?}

?public void setW_H(int width, int height) {
?this.OutputWidth = width;
?this.OutputHeight = height;
?}

?public String s_pic() {
?BufferedImage image;
?String NewFileName;
//建立輸出文件對象
?File file = new File(OutputDir + OutputFileName);
?FileOutputStream tempout = null;
?try {
?tempout = new FileOutputStream(file);
?} catch (Exception ex) {
?System.out.println(ex.toString());
?}
?Image img = null;
?Toolkit tk = Toolkit.getDefaultToolkit();
?Applet app = new Applet();
?MediaTracker mt = new MediaTracker(app);
?try {
?img = tk.getImage(InputDir + InputFileName);
?mt.addImage(img, 0);
?mt.waitForID(0);
?} catch (Exception e) {
?e.printStackTrace();
?}

?if (img.getWidth(null) == -1) {
?System.out.println(" can't read,retry!" + "<BR>");
?return "no";
?} else {
?int new_w;
?int new_h;
?if (this.proportion == true) { //判斷是否是等比縮放.
//為等比縮放計算輸出的圖片寬度及高度
?double rate1 = ((double) img.getWidth(null)) /
?(double) OutputWidth + 0.1;
?double rate2 = ((double) img.getHeight(null)) /
?(double) OutputHeight + 0.1;
?double rate = rate1 > rate2 ? rate1 : rate2;
?new_w = (int) (((double) img.getWidth(null)) / rate);
?new_h = (int) (((double) img.getHeight(null)) / rate);
?} else {
?new_w = OutputWidth; //輸出的圖片寬度
?new_h = OutputHeight; //輸出的圖片高度
?}
?BufferedImage buffImg = new BufferedImage(new_w, new_h,
?BufferedImage.TYPE_INT_RGB);

?Graphics g = buffImg.createGraphics();

?g.setColor(Color.white);
?g.fillRect(0, 0, new_w, new_h);

?g.drawImage(img, 0, 0, new_w, new_h, null);
?g.dispose();

?JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout);
?try {
?encoder.encode(buffImg);
?tempout.close();
?} catch (IOException ex) {
?System.out.println(ex.toString());
?}
?}
?return "ok";
?}

?public String s_pic(String InputDir, String OutputDir, String InputFileName,
?String OutputFileName) {
//輸入圖路徑
?this.InputDir = InputDir;
//輸出圖路徑
?this.OutputDir = OutputDir;
//輸入圖文件名
?this.InputFileName = InputFileName;
//輸出圖文件名
?this.OutputFileName = OutputFileName;
?return s_pic();
?}

?public String s_pic(String InputDir, String OutputDir, String InputFileName,
?String OutputFileName, int width, int height,
?boolean gp) {
//輸入圖路徑
?this.InputDir = InputDir;
//輸出圖路徑
?this.OutputDir = OutputDir;
//輸入圖文件名
?this.InputFileName = InputFileName;
//輸出圖文件名
?this.OutputFileName = OutputFileName;
//設置圖片長寬
?setW_H(width, height);
//是否是等比縮放 標記
?this.proportion = gp;
?return s_pic();
?}

?public static void main(String[] a) {
//s_pic(大圖片路徑,生成小圖片路徑,大圖片文件名,生成小圖片文名,生成小圖片寬度,生成小圖片高度)
?Tes mypic = new Tes();
?System.out.println(
?mypic.s_pic("E:\\Document\\My Pictures\\",
?"E:\\Document\\My Pictures\\",
?"topbg-3.gif", "3.gif", 400, 400, true)
?);

?}
?}

3.jsp方式
java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,

??try
????{
?java.io.File file = new java.io.File("E:\\Document\\My Pictures\\3.gif");
?String newurl="E:\\Document\\My Pictures\\32.gif"; //新的縮略圖保存地址
?Image src = javax.imageio.ImageIO.read(file); //構造Image對象
?float tagsize=200;
?int old_w=src.getWidth(null); //得到源圖寬
?int old_h=src.getHeight(null);
?int new_w=0;
?int new_h=0; //得到源圖長
?int tempsize;
?float tempdouble;
?if(old_w>old_h){
?tempdouble=old_w/tagsize;
?}else{
?tempdouble=old_h/tagsize;
?}
?new_w=Math.round(old_w/tempdouble);
?new_h=Math.round(old_h/tempdouble);//計算新圖長寬
?BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
?tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //繪制縮小后的圖
?FileOutputStream newimage=new FileOutputStream(newurl); //輸出到文件流
?JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
?encoder.encode(tag); //近JPEG編碼
?newimage.close();

}catch (Exception e){

e.toString();

}
?

轉載于:https://www.cnblogs.com/leic2000/archive/2008/12/31/1366191.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379794.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379794.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379794.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

javascript寫入_如何在JavaScript中寫入HTML元素?

javascript寫入寫入HTML元素 (Writing into an HTML element) To write string/text into an HTML element, we use the following things: 要將字符串/文本寫入HTML元素&#xff0c;我們使用以下內容&#xff1a; There must be an HTML element like paragraph, span, div e…

大話設計模式之設計模式遵循的七大原則

最近幾年來&#xff0c;人們踴躍的提倡和使用設計模式&#xff0c;其根本原因就是為了實現代碼的復用性&#xff0c;增加代碼的可維護性。設計模式的實現遵循了一些原則&#xff0c;從而達到代碼的復用性及增加可維護性的目的&#xff0c;設計模式對理解面向對象的三大特征有很…

IIC通信---EEPROM24C02---STMF4

IIC通信協議 IIC是同步半雙工通信&#xff0c;一個數據線SDA和一個時鐘SCL線&#xff0c;可以接受和發送數據。在CPU與被控IC之間、IC與IC之間進行雙向傳送。 空閑狀態 IIC總線的SDA和SCL兩條信號線同時處于高電平時&#xff0c;規定為總線的空閑狀態。 起始信號 當SCL為高…

實訓09.08:簡單的算法練習

/*final 關鍵字 修飾的變量值 后期不可更改 相當于定義常量常量 &#xff1a;不可更改*/final int a 10;//a 20; 報錯的值不可更改&#xff01;/*輸入函數* */System.out.println("請輸入數字&#xff1a;");Scanner scanner new Scanner(System.in);int b…

讓自己閃亮

轉載于:https://www.cnblogs.com/Gigabyte/archive/2009/01/03/you_can_shine.html

Java中的wait()和sleep()方法之間的區別

Java中的wait()和sleep()方法 (wait() and sleep() methods in Java) First, we will see how wait() method differs from sleep() method in Java? 首先&#xff0c;我們將看到wait()方法與Java中的sleep()方法有何不同&#xff1f; wait()方法 (wait() Method) This metho…

離線使用iPhone SDK文檔的方法

在使用Xcode進行iPhone編程時&#xff0c;有時需要參考iPhone SDK的文檔&#xff0c;不過每次ControlClick后&#xff0c;Xcode都會試圖連接Internet&#xff0c;進行在線讀取。有什么方法能夠把資料下載到硬盤上進行離線閱讀嗎&#xff1f; 答案是肯定的。首先去Xcode的Prefer…

遠程連接sql server 2000服務器的解決方案

遠程連接sql server 2000服務器的解決方案2007-04-07 11:29遠程連接sql server 2000服務器的解決方案   一 看ping 服務器IP能否ping通。   這個實際上是看和遠程sql server 2000服務器的物理連接是否存在。如果不行&#xff0c;請檢查網絡&#xff0c;查看配置&#xff0c…

實訓09.10:HTML簡單表格設計

<!DOCTYPE html> <html><head><meta charset"UTF-8"><title>燕雨簡歷</title></head><body><table border"" cellspacing"" cellpadding"" width"400px" height"6…

LCD顯示實驗----STM32f4--HAL

步驟 LCD初始化 LCD_Init(); //LCD初始化此函數在lcd.c文件里面 2. 設置LCD背景顏色 LCD_Clear(WHITE);此函數在lcd.c文件里面 3. 設置字體顏色 POINT_COLORRED; 寫入要顯示的字體 LCD_ShowString(10,80,240,24,24,"LTDC TEST");LCD_ShowSt…

JavaScript | 使用提示從用戶輸入值

Example 1) Input name and print 示例1)輸入名稱和打印 Code (JS & HTML): 代碼(JS和HTML)&#xff1a; <!DOCTYPE html><HTML><HEAD><SCRIPT>var name prompt("Enter Your name:");var msg "Welcome "name;//alert(msg)…

一個游戲程序員的學習資料 (zz)

一個游戲程序員的學習資料//z 2012-4-19 14:39:51 PM IS2120CSDN想起寫這篇文章是在看侯杰先生的《深入淺出MFC》時, 突然覺得自己在大學這幾年關于游戲編程方面還算是有些心得&#xff0c;因此寫出這篇小文,介紹我眼中的游戲程序 員的書單與源代碼參考。一則是作為自己今后兩年…

項目管理中工作分解結構模型(WBSM)的應用

摘要 本文根據工作分解結構(WBS)的工作特點&#xff0c;運用系統工程的思想理論方法&#xff0c;構建了工作分解結構模型&#xff0c;并提出了模型算法;該模型方法的建立使得WBS工作更加簡單可靠、思路清晰、基于更加可靠的科學基礎之上。 1、工作分解結構模型(WBSM)方法工作程…

實訓09.11:java重點內容介紹

package test;/** * OP:面向對象的簡稱* 類&#xff1a;同一特征的屬性* * 類的定義&#xff1a;具有相同特征和行為的事物的抽象。&#xff08;不具體化&#xff09;* 對象&#xff08;實例對象&#xff09;&#xff1a;具體真實存在的實例。* 類是對象的實例&#xff1a;* *…

SPI通信原理---STM32F4--HAL

SPI接口原理 SPI是一種高速全雙工同步通信&#xff0c;在芯片管腳上占用四根線&#xff0c;主要應用在EEPROM、FLASH、實時時鐘、AD轉換器&#xff0c;還有數字信號處理器和數字信號解碼器之間。 SPI接口使用4根線通信。 MISO&#xff1a;主設備數據輸入&#xff0c;從設備數…

Linux 引導管理器 grub2 使用簡介

轉自&#xff1a;杜昌彬的空間 首先向其致敬&#xff01;有改動。 grub是Linux系統即其他類unix系統的主流bootloder&#xff0c;由于grub原來版本的設計存在很大缺陷&#xff0c;與以前的grub很不相同&#xff0c;其使用和配置也發生很大變化。現在很多Linux發行版本都使用了…

pata1015_ATA / PATA的完整形式是什么?

pata1015ATA / PATA&#xff1a;高級技術附件/并行高級技術附件 (ATA/PATA: Advanced Technology Attachment/Parallel Advanced Technology Attachment) ATA is an abbreviation of Advanced Technology Attachment. ATA has existed for a long time with the name PATA. Whe…

產品

總結一下&#xff1a;  1、核心功能要做透&#xff0c;做的人家追不上&#xff0c;自己的優勢要盡量的發揮&#xff1b;  2、產品口碑要建立&#xff0c;要關注高端用戶&#xff0c;要調整自己心態&#xff1b;  3、敏捷、快&#xff0c;產品迭代要快&#xff0c;快速實現…

FreeRTOS在STM32F429上移植

準備工作 FreeRTOS系統源碼基礎工程&#xff0c;這里我們用跑馬燈實驗 1.在工程里面添加FreeRTOS源碼 在工程里面新建一個名為FreeROTS的文件夾 將FreeRTOS源碼添加到這個文件夾里面 protable里面只需留下Keil、MemMang、RVDS文件夾 2、向工程分組中添加文件 FreeRTOS_C…

C++中的指針與引用(轉)

原文地址&#xff1a;http://www.cnblogs.com/skynet/archive/2010/09/22/1832911.html寫在前面 指針和引用形式上很好區別&#xff0c;但是他們似乎有相同的功能,都能夠直接引用對象&#xff0c;對其進行直接的操作。但是什么時候使用指針&#xff1f;什么時候使用引用呢&…