java实现的均匀切割图片

发布于:2021-09-22 03:53:41

在做拼图的时候,需要将图片均匀切割,这时候就需要一个切图工具了,ps虽然有一个切图工具,但是那个ps太大了,于是手动做了一个用来切图的小工具,具体代码实现如下


?


package Mytool;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
class toolWindow extends JFrame implements ActionListener{
Button m_cut,m_loading,m_save;
String RecRoad=null;//路径
Image image;
Image w_image[];//剪切好的图片
ImageIcon icon;
JTextField row,col;
JLabel rowtext,coltext,nametext;
int t_row=0,t_col=0;//行列数
JTabbedPane pane;
BufferedImage bf_image;
int flag=1,flag2=0,flag3=0;
Box box;
public toolWindow(String s) {
setTitle(s);//设置名字
setBounds(100, 100,232, 236);//窗口位置和大小
setLayout(new FlowLayout(FlowLayout.LEFT));//窗口布局
m_cut=new Button("剪切");//按钮设置
m_loading=new Button("加载图片");
m_save=new Button("保存图片");
rowtext=new JLabel("切割行数:");
coltext=new JLabel("切割列数:");
nametext=new JLabel("制作人:高镜皓 制作日期:2018.5.15");
box=Box.createVerticalBox();
box.add(new JLabel("1.先按下加载按钮,加载图片"));
box.add(new JLabel("2.输入行列数"));
box.add(new JLabel("3.点击剪切再保存"));
box.add(new JLabel("4.文件在程序当下的image文件夹下"));
row=new JTextField(10);
col=new JTextField(10);
m_cut.addActionListener(this);//按钮监听
m_loading.addActionListener(this);
m_save.addActionListener(this);
add(m_cut);
add(m_save);
add(m_loading);//添加组件
add(rowtext);add(row);
add(coltext);add(col);
add(nametext);
add(box);
setVisible(true);//显示窗口
this.setResizable(false);
validate();//显示按钮
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
void cut(int k) throws IOException//剪切图片
{
bf_image=ImageIO.read(new File(RecRoad));
int w = (bf_image.getWidth(this))/t_col;
int h = (bf_image.getHeight(this))/t_row;
System.out.println("图片宽度:"+w);
System.out.println("图片高度:"+h);
for (int i = 0; i < k; i++) {
BufferedImage outImg=bf_image.getSubimage(i%t_col*w,i/t_row*h , w, h);
w_image[i]=(Image)outImg;
}
System.out.println("剪切成功");
}
public String open(){ //获取文件路径
JFileChooser jFileChooser=new JFileChooser();
File fileone = null;
if(jFileChooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
fileone=jFileChooser.getSelectedFile();
return fileone.getAbsolutePath();
}
return null;
}
public void actionPerformed(ActionEvent e) {//监听按钮
if(e.getSource()==m_loading){
RecRoad=open();
}
else if(e.getSource()==m_save)
{
if (RecRoad!=null && flag>=1) {
for (int i = 0; i < t_row*t_col; i++) {
savePic(w_image[i], i);
}
flag++;
}
}
else if(e.getSource()==m_cut)//调用cut
{
try {
t_row=Integer.parseInt(row.getText());
t_col=Integer.parseInt(col.getText());
if (t_row!=0 && t_col!=0) {
flag2=1;
w_image=new Image[t_col*t_row];
try {
cut(t_col*t_row);
} catch (IOException e1) {
e1.printStackTrace();
}
}
} catch (NumberFormatException ee) {
row.setText("请输入行数");
col.setText("请输入列数");
}
}

}
public void savePic(Image myimage,int i){//保存图片
int w = myimage.getWidth(this);
int h = myimage.getHeight(this);
//首先创建一个BufferedImage变量,因为ImageIO写图片用到了BufferedImage变量。
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
System.out.println("保存图片"+i);
//再创建一个Graphics变量,用来画出来要保持的图片,及上面传递过来的Image变量
//Graphics g = bi.getGraphics();
try {
//g.drawImage(myimage, 0, 0, null);
//将BufferedImage变量写入文件中。
File file = new File("./image");
file.mkdirs();
ImageIO.write(bi,"jpg",new File("./image/image"+flag+"_"+i+".jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Mytool {
public static void main(String[] args) {
new toolWindow("切片工具");
}
}

?

相关推荐

最新更新

猜你喜欢