图片裁剪,缩放 批处理

package dwr.demo;

 

import java.awt.Color;

import java.awt.FileDialog;

import java.awt.FlowLayout;

import java.awt.Image;

import java.awt.Rectangle;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.image.BufferedImage;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Iterator;

 

import javax.imageio.ImageIO;

import javax.imageio.ImageReadParam;

import javax.imageio.ImageReader;

import javax.imageio.stream.ImageInputStream;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

 

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

 

 

public class ImgReSize {

private JFrame frame;

private JButton chooseImgDirBtn;

private JTextField  ImgDirLab;

private JButton saveImgDirBtn;

private JTextField  saveDirLab;

private JButton ok_btn;

private JButton reset_btn;

private JTextField w=new JTextField();

private JTextField h=new JTextField();

private JTextField left=new JTextField(2);

private JTextField top=new JTextField(2);

private JTextField cutW=new JTextField(2);

private JTextField cutH=new JTextField(2);

private JPanel pro=new JPanel();

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

new ImgReSize();

}

ImgReSize(){

frame=new JFrame("图片批量改变大小");

frame.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width/2-150,Toolkit.getDefaultToolkit().getScreenSize().height/2-100,300, 200);

frame.setResizable(false);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//frame.add(chooceImgDir);

frame.setLayout(new FlowLayout(FlowLayout.LEFT));

ImgDirLab=new JTextField();ImgDirLab.setColumns(15);ImgDirLab.setEditable(false);

frame.add(ImgDirLab);

chooseImgDirBtn=new JButton("需转换的目录");

chooseImgDirBtn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

FileDialog chooceImgDir=new FileDialog(new JFrame(),"请选择要转换的文件目录",FileDialog.LOAD);

  chooceImgDir.setVisible(true);

  ImgDirLab.setText(chooceImgDir.getDirectory());

  chooseImgDirBtn.setEnabled(false);

}});

frame.add(chooseImgDirBtn);

saveDirLab=new JTextField();saveDirLab.setColumns(15);saveDirLab.setEditable(false);

frame.add(saveDirLab);

saveImgDirBtn=new JButton("保存的目录");

saveImgDirBtn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

FileDialog chooceImgDir=new FileDialog(new JFrame(),"请选择要保存的文件目录",FileDialog.LOAD);

  chooceImgDir.setVisible(true);

  saveDirLab.setText(chooceImgDir.getDirectory());

  saveImgDirBtn.setEnabled(false);

}});

frame.add(saveImgDirBtn);

frame.add(new JLabel("输入大小:"));

w.setColumns(3);h.setColumns(3);

frame.add(w);frame.add(new JLabel("X"));

frame.add(h);

ok_btn=new  JButton("转换");

ok_btn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(w.getText().equals("")||h.getText().equals("")||ImgDirLab.getText().equals("")||saveDirLab.getText().equals(""))return;

final int width=Integer.parseInt(w.getText());

final int hieght=Integer.parseInt(h.getText());

final int Ctop=Integer.parseInt(top.getText());

final int Cleft=Integer.parseInt(left.getText());

final int CutW=Integer.parseInt(cutW.getText());

final int CutH=Integer.parseInt(cutH.getText());

final String basePath=ImgDirLab.getText(); 

final String exportPath=saveDirLab.getText(); 

File file = new File(basePath);

File []files=file.listFiles();

if(files!=null){

float jg=(float) (frame.getWidth()/(files.length*1.0));

for(int i=0;i<files.length;i++){

File tmp=files[i];

if(tmp.isFile()){

String name=tmp.getName();

//System.out.println(tmp.getName());

String syn=name.substring(name.indexOf('.'));

if(syn!=null&&(syn.equalsIgnoreCase(".GIF")||syn.equalsIgnoreCase(".PNG")||syn.equalsIgnoreCase(".JPG"))){

///System.out.println(name);

try {

FileInputStream in=new FileInputStream(tmp);

byte[] b=new byte[2*1024*1024];

in.read(b);

in.close();

byte[]tb1=this.cut(b,Ctop,Cleft,CutW,CutH);

byte[]tb=this.fixPic(tb1,width,hieght);

FileOutputStream out=new FileOutputStream(exportPath+name);

out.write(tb);

out.flush();

out.close();pro.setSize((int)((i+1)*jg), 15);

} catch (FileNotFoundException e1) {

e1.printStackTrace();

}catch (IOException e1) {

e1.printStackTrace();

}

}

}

}

}

System.out.println("转换完成!");

}

private byte[] fixPic(byte[] bts,int w,int h){ 

try { 

 

Image img=ImageIO.read(new ByteArrayInputStream(bts)); 

BufferedImage tag = new BufferedImage(w,  h, BufferedImage.TYPE_INT_RGB); 

tag.getGraphics().drawImage(img.getScaledInstance(w, h, Image.SCALE_SMOOTH), 0, 0, null); 

ByteArrayOutputStream out=new ByteArrayOutputStream(); 

JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out); 

encoder.encode(tag); 

byte[] by= out.toByteArray(); 

out.close(); 

return by; 

} catch (IOException e) { 

e.printStackTrace(); 

return null; 

 

private byte[] cut(byte[] bts,int left,int top,int w,int hight){

try {

Iterator readers = ImageIO.getImageReadersByFormatName("jpg");

           System.out.println(readers);

           ImageReader reader = (ImageReader)readers.next();

           InputStream source=new ByteArrayInputStream(bts);

           ImageInputStream iis = ImageIO.createImageInputStream(source);

           reader.setInput(iis, true);

           ImageReadParam param = reader.getDefaultReadParam();

           int imageIndex = 0;

           int half_width = reader.getWidth(imageIndex)/2;

           int half_height = reader.getHeight(imageIndex)/2;

           Rectangle rect = new Rectangle(60, 60, 100, 100);

           param.setSourceRegion(rect);

           BufferedImage bi = reader.read(0,param);

           ByteArrayOutputStream out=new ByteArrayOutputStream(); 

           ImageIO.write(bi, "jpg",out);  

           return out.toByteArray();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

return bts;

}

});

frame.add(ok_btn);

reset_btn=new  JButton("重置");

reset_btn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

ImgDirLab.setText("");

saveDirLab.setText("");

chooseImgDirBtn.setEnabled(true);

saveImgDirBtn.setEnabled(true);

}});

frame.add(reset_btn);

frame.add(new JLabel("裁剪位置:"));

frame.add(new JLabel("top:"));

frame.add(this.top);

frame.add(new JLabel("left:"));

frame.add(this.left);

frame.add(new JLabel("w:"));

frame.add(this.cutW);

frame.add(new JLabel("h:"));

frame.add(this.cutH);

pro.setBackground(Color.GREEN);pro.setLayout(null);

frame.add(pro);frame.add(pro);

frame.setVisible(true);

 

}

}


你可能感兴趣的:(批处理)