java中如何用鼠标点击截取一张图片的某部分(希望有具体代码)

发布网友 发布时间:2022-04-21 08:58

我来回答

2个回答

热心网友 时间:2023-11-08 19:15

DivImageByMouse是个JPanel子类,把main去掉,add到别的容器下也可以正常使用,是按照鼠标移动取的,要按200*200的取要稍微改动一下

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class DivImageByMouse extends JPanel{
BufferedImage image=null;
int x1,y1,x2,y2;
public DivImageByMouse(File file){
super();
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
int x=x1<x2?x1:x2;
int y=y1<y2?y1:y2;
int w=(x1>x2?x1:x2)-x;
int h=(y1>y2?y1:y2)-y;
Image image=DivImageByMouse.this.getImageByClip(x, y, w, h);
setClipboardImage2(image);
x1=y1=x2=y2=0;
JOptionPane.showMessageDialog(DivImageByMouse.this,"图片已保存到系统粘贴板!","图片已保存",JOptionPane.INFORMATION_MESSAGE);
DivImageByMouse.this.repaint();
}
});
this.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e) {
x2=e.getX();
y2=e.getY();
DivImageByMouse.this.repaint();
}
});
try {
image=ImageIO.read(file);
} catch (IOException e) {
System.out.println("输入文件不是一个图片文件!");
}
}
public Image getImage(){
return image;
}
public Image getImageByClip(int x,int y,int w,int h){
int rgbs[]=new int[w*h];
rgbs=image.getRGB(x,y, w, h, rgbs,0,w);
BufferedImage tmpImage=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
tmpImage.setRGB(0, 0, w, h, rgbs,0,w);
return tmpImage;
}
public void paint(Graphics g){
super.paint(g);
g.drawImage(image,0,0,this);
System.out.println("("+x1+","+y1+")("+x2+","+y2+")");
if(x1==0&&y1==0&&x2==0&&y2==0) return;
System.out.println("rect");
int x=x1<x2?x1:x2;
int y=y1<y2?y1:y2;
int w=(x1>x2?x1:x2)-x;
int h=(y1>y2?y1:y2)-y;
g.setColor(Color.blue);
g.drawRect(x, y, w, h);
}
protected static void setClipboardImage2(final Image image) {
Transferable trans = new Transferable(){
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.imageFlavor };
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.imageFlavor.equals(flavor);
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if(isDataFlavorSupported(flavor))
return image;
throw new UnsupportedFlavorException(flavor);
}
};
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);
}

public static void main(String args[]){
JFrame jf=new JFrame("");
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setBounds(50,50,1024,768);
jf.add(new DivImageByMouse(new File("e:\\109.jpg")));
jf.add(new JButton(""),"North");
jf.setVisible(true);
}
}

热心网友 时间:2023-11-08 19:15

用map标签,在drw里用热区,拖动就可以了,你试试

代码会自动生成,如下:
<map name="Map" id="Map"><area shape="rect" coords="104,303,223,357" href="http://www.baidu.com" />
</map>

你只要换掉104,303,223,357(图片区域上下左右坐标的位置)和超链接地址即可

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com