java中如何用鼠标点击截取一张图片的某部分(希望有具体代码)
比如一张1024*768的图当鼠标点击时自动截取中间200*200的图希望有代码如果比较准确的会追加是由JSP提交到servlet如何在servlet中获取所截取的图片存...
比如一张1024*768的图 当鼠标点击时自动截取中间200*200的图 希望有代码 如果比较准确的 会追加
是由JSP提交到servlet 如何在servlet中获取所截取的图片 存入数据库中 展开
是由JSP提交到servlet 如何在servlet中获取所截取的图片 存入数据库中 展开
2个回答
展开全部
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);
}
}
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);
}
}
展开全部
用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(图片区域上下左右坐标的位置)和超链接地址即可
代码会自动生成,如下:
<map name="Map" id="Map"><area shape="rect" coords="104,303,223,357" href="http://www.baidu.com" />
</map>
你只要换掉104,303,223,357(图片区域上下左右坐标的位置)和超链接地址即可
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询