package com.photomg.suolue;
import java.io.*;
import java.security.PublicKey;
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.*;
/**
* Created by IntelliJ IDEA.
* User: Rain
* Date: 11-1-18
* Time: 下午4:21
* To change this template use File | Settings | File Templates.
*/
//缩略图类
//本java类能将jpg图片文件,进行等比或非等比的大小转换。
//具体使用方法
//s_pic大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
public class Deal {
String InputDir;//输入图路径
String OutputDir;//输出图路径
String InputFileName;//输入图文件名
String OutputFileName;//输出图文建平
int OutputWidth=80;//默认输出图片宽度
int OutputHeight=80;//默认输出图片高度
int rate=0;
boolean proportion=true;//是否进行等比缩放标记(默认为等比缩放)
public Deal(){
}
public void setInputDir(String inputDir) {
InputDir = inputDir;
}
public void setOutputDir(String outputDir) {
OutputDir = outputDir;
}
public void setInputFileName(String inputFileName) {
InputFileName = inputFileName;
}
public void setOutputFileName(String outputFileName) {
OutputFileName = outputFileName;
}
public void setOutputWidth(int outputWidth) {
OutputWidth = outputWidth;
}
public void setOutputHeight(int outputHeight) {
this.OutputHeight = outputHeight;
}
public void setW_H(int width,int height){
this.OutputHeight=height;
this.OutputWidth=width;
}
public String s_pic(){
BufferedImage image;
String NewFileName;
//建立输出文件对象
File file=new File(OutputDir+OutputFileName);
FileOutputStream tempout=null;
try{
tempout=new FileOutputStream(file);
}catch (Exception e){
System.out.println();e.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) OutputWidth+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 e){
System.out.println(e.toString());
}
}
return "ok";
}
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(大图片路径,生成小图片路径,大图片文件名,生成小图片文件名,生成小图片宽度,生成小图片高度,是否等比缩放)
Deal mypic=new Deal();
System.out.println(
mypic.s_pic("E:\\c寸\\pic\\","E:\\c寸\\pic\\","a.jpg","b.jpg",120,90,true));
}
}
公司老总:
刘总
import java.io.*;
import java.security.PublicKey;
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.*;
/**
* Created by IntelliJ IDEA.
* User: Rain
* Date: 11-1-18
* Time: 下午4:21
* To change this template use File | Settings | File Templates.
*/
//缩略图类
//本java类能将jpg图片文件,进行等比或非等比的大小转换。
//具体使用方法
//s_pic大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
public class Deal {
String InputDir;//输入图路径
String OutputDir;//输出图路径
String InputFileName;//输入图文件名
String OutputFileName;//输出图文建平
int OutputWidth=80;//默认输出图片宽度
int OutputHeight=80;//默认输出图片高度
int rate=0;
boolean proportion=true;//是否进行等比缩放标记(默认为等比缩放)
public Deal(){
}
public void setInputDir(String inputDir) {
InputDir = inputDir;
}
public void setOutputDir(String outputDir) {
OutputDir = outputDir;
}
public void setInputFileName(String inputFileName) {
InputFileName = inputFileName;
}
public void setOutputFileName(String outputFileName) {
OutputFileName = outputFileName;
}
public void setOutputWidth(int outputWidth) {
OutputWidth = outputWidth;
}
public void setOutputHeight(int outputHeight) {
this.OutputHeight = outputHeight;
}
public void setW_H(int width,int height){
this.OutputHeight=height;
this.OutputWidth=width;
}
public String s_pic(){
BufferedImage image;
String NewFileName;
//建立输出文件对象
File file=new File(OutputDir+OutputFileName);
FileOutputStream tempout=null;
try{
tempout=new FileOutputStream(file);
}catch (Exception e){
System.out.println();e.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) OutputWidth+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 e){
System.out.println(e.toString());
}
}
return "ok";
}
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(大图片路径,生成小图片路径,大图片文件名,生成小图片文件名,生成小图片宽度,生成小图片高度,是否等比缩放)
Deal mypic=new Deal();
System.out.println(
mypic.s_pic("E:\\c寸\\pic\\","E:\\c寸\\pic\\","a.jpg","b.jpg",120,90,true));
}
}
公司老总:
刘总