62,614
社区成员
发帖
与我相关
我的任务
分享
package com.blz.channel;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 由原始图片来生产其缩略图
* @author zongb
* @date 2009-10-27
*
*/
public class BuildSmallImage {
String fileFullName;
String newFullName ;
public static void main(String [] args){
String oldFileName = "F:\\a.jpg" ;
String newFileName = "F:\\b.jpg" ;
BuildSmallImage bsi = new BuildSmallImage() ;
if(bsi.build(oldFileName, newFileName)){
System.out.println("生成缩略图成功!") ;
}else{
System.out.println("生成缩略图失败!") ;
}
}
public boolean build(String oldFileName,String newFileName) {
this.fileFullName = oldFileName ;//原始文件的全路径
this.newFullName = newFileName ;//新生成文件的全路径
FileOutputStream newimage = null;
try {
File file = new File(fileFullName); // 读入刚才上传的文件
Image src = ImageIO.read(file); // 构造Image对象
//float tagsize = 200; //限制宽度为200
int old_w = src.getWidth(null); // 得到源图宽
int old_h = src.getHeight(null);// 得到源图高
System.out.println("old_w = "+old_w) ;
System.out.println("old_h = "+old_h) ;
float flag_w = 200f ; //限制宽度为200
int new_w = 200; //缩略图宽,
int new_h = 0; //缩略图高
float tempdouble;
if (old_w > new_w) {
tempdouble = old_w / flag_w;
new_w = Math.round(old_w / tempdouble);// 计算缩略图高
new_h = Math.round(old_h / tempdouble);// 计算缩略图高
System.out.println("tempdouble = "+tempdouble) ;
} else {
new_w = old_w ;
new_h = old_h ;
}
System.out.println("new_w = "+new_w) ;
System.out.println("new_h = "+new_h) ;
BufferedImage tag = new BufferedImage(new_w, new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, new_w, new_h, null); // 绘制缩小后的图
newimage = new FileOutputStream(newFullName);// 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag); // 近JPEG编码
file.delete() ;//删除原始的图片
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false ;
}finally{
if(newimage != null){
try {
newimage.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
return true ;
}
}
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 由原始图片来生产其缩略图
* @author zongb
* @date 2009-10-27
*
*/
public class BuildSmallImage {
String fileFullName;
String newFullName ;
public static void main(String [] args){
String oldFileName = "C://Documents and Settings/nzhai/My Documents/My Pictures/11.jpg" ;
String newFileName = "C://Documents and Settings/nzhai/My Documents/My Pictures/13.jpg" ;
BuildSmallImage bsi = new BuildSmallImage() ;
if(bsi.build(oldFileName, newFileName)){
System.out.println("生成缩略图成功!") ;
}else{
System.out.println("生成缩略图失败!") ;
}
}
public boolean build(String oldFileName,String newFileName) {
this.fileFullName = oldFileName ;//原始文件的全路径
this.newFullName = newFileName ;//新生成文件的全路径
FileOutputStream newimage = null;
try {
File file = new File(fileFullName); // 读入刚才上传的文件
Image src = ImageIO.read(file); // 构造Image对象
//float tagsize = 200; //限制宽度为200
int old_w = src.getWidth(null); // 得到源图宽
int old_h = src.getHeight(null);// 得到源图高
System.out.println("old_w = "+old_w) ;
System.out.println("old_h = "+old_h) ;
int image_H = 140; //生成缩略图的最大高度是 image_H
int image_W = 200; //生成缩略图的最大宽度是 image_W
// float flag_w = 200f ; //限制宽度为200
int new_w = 0; //缩略图宽,
int new_h = 0; //缩略图高
float proportion = (float)image_W/(float)image_H; //缩略图的比例
System.out.println("proportion = "+proportion);
float oldProportion = (float)old_w/(float)old_h; //原图片的比例
System.out.println("oldProportion = "+oldProportion);
if(proportion >= oldProportion){
if(old_h >image_H){
new_h = image_H;
new_w = (int)(new_h*oldProportion);
}else{
new_h = old_h;
new_w = old_w;
}
}else{
if(old_w >image_W){
new_w = image_W;
new_h = (int)(new_w/oldProportion);
}else{
new_h = old_h;
new_w = old_w;
}
}
System.out.println("new_w = "+new_w) ;
System.out.println("new_h = "+new_h) ;
BufferedImage tag = new BufferedImage(image_W, image_H,BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)tag.createGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0,0,image_W,image_H);
tag.getGraphics().drawImage(src, (image_W-new_w)/2, (image_H-new_h)/2, new_w, new_h, null); // 绘制缩小后的图
newimage = new FileOutputStream(newFullName);// 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag); // 近JPEG编码
//file.delete() ;//删除原始的图片
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false ;
}finally{
if(newimage != null){
try {
newimage.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
return true ;
}
}
package nio;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 由原始图片来生产其缩略图
* @author zongb
* @date 2009-10-27
*
*/
public class BuildSmallImage {
String fileFullName;
String newFullName ;
public static void main(String [] args){
String oldFileName = "C://Documents and Settings/nzhai/My Documents/My Pictures/12.jpg" ;
String newFileName = "C://Documents and Settings/nzhai/My Documents/My Pictures/13.jpg" ;
BuildSmallImage bsi = new BuildSmallImage() ;
if(bsi.build(oldFileName, newFileName)){
System.out.println("生成缩略图成功!") ;
}else{
System.out.println("生成缩略图失败!") ;
}
}
public boolean build(String oldFileName,String newFileName) {
this.fileFullName = oldFileName ;//原始文件的全路径
this.newFullName = newFileName ;//新生成文件的全路径
FileOutputStream newimage = null;
try {
File file = new File(fileFullName); // 读入刚才上传的文件
Image src = ImageIO.read(file); // 构造Image对象
//float tagsize = 200; //限制宽度为200
int old_w = src.getWidth(null); // 得到源图宽
int old_h = src.getHeight(null);// 得到源图高
System.out.println("old_w = "+old_w) ;
System.out.println("old_h = "+old_h) ;
float flag_w = 200f ; //限制宽度为200
int new_w = 200; //缩略图宽,
int new_h = 0; //缩略图高
float tempdouble;
if (old_w > new_w) {
tempdouble = old_w / flag_w;
new_w = Math.round(old_w / tempdouble);// 计算缩略图高
new_h = Math.round(old_h / tempdouble);// 计算缩略图高
System.out.println("tempdouble = "+tempdouble) ;
} else {
new_w = old_w ;
new_h = old_h ;
}
int image_H = 140;
int image_W = 200;
System.out.println("new_w = "+new_w) ;
System.out.println("new_h = "+new_h) ;
BufferedImage tag = new BufferedImage(image_W, image_H,BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)tag.createGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0,0,image_W,image_H);
tag.getGraphics().drawImage(src, (image_W-old_w)/2, (image_H-new_h)/2, new_w, new_h, null); // 绘制缩小后的图
newimage = new FileOutputStream(newFullName);// 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag); // 近JPEG编码
//file.delete() ;//删除原始的图片
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false ;
}finally{
if(newimage != null){
try {
newimage.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
return true ;
}
}