ImageHelper.getSubImage(image, startX, startY, endX, endY)可以裁剪图片,我怎么获取裁剪区域的坐标
c裁剪区域的坐标是自己设置,这是我以前想做拼图软件的代码没写完,你可以参考一下,不过和你用的函数不一样
1 import java.awt.*; 2 import java.awt.event.ActionEvent; 3 import java.awt.event.ActionListener; 4 import java.awt.image.BufferedImage; 5 import java.awt.image.CropImageFilter; 6 import java.awt.image.FilteredImageSource; 7 import java.awt.image.ImageFilter; 8 import java.io.BufferedInputStream; 9 import java.io.File; 10 import java.io.FileInputStream; 11 import java.io.IOException; 12 13 import javax.imageio.ImageIO; 14 import javax.sound.midi.InvalidMidiDataException; 15 import javax.sound.midi.MidiSystem; 16 import javax.sound.midi.MidiUnavailableException; 17 import javax.sound.midi.Sequence; 18 import javax.sound.midi.Sequencer; 19 import javax.swing.*; 20 import javax.swing.filechooser.FileNameExtensionFilter; 21 22 import javazoom.jl.player.Player; 23 import javazoom.jl.player.Player; 24 //包含引入包 25 public class window extends JFrame implements ActionListener{ 26 JPanel p=new JPanel(); 27 MenuItem renew=new MenuItem(" 重玩此关"); 28 MenuItem start=new MenuItem(" 开始游戏"); 29 MenuItem end=new MenuItem(" 退出游戏"); 30 MenuItem offmusic=new MenuItem(" 音乐关"); 31 MenuItem openmusic=new MenuItem(" 音乐开"); 32 MenuItem easy=new MenuItem(" 简单"); 33 MenuItem medium=new MenuItem(" 中等"); 34 MenuItem difficult=new MenuItem(" 困难"); 35 MenuItem overability=new MenuItem(" 变态级难度"); 36 JLabel lpic; 37 String path=new String("music\\Mark Petrie (马克·皮特里) - Go Time (开始时间).mp3"); 38 static String rootname; 39 static Image pic; 40 boolean judge=false; 41 Player player; 42 43 window(String rootname) 44 { 45 super("拼图"); 46 this.setSize(1000, 700); 47 this.setResizable(false); 48 this.setLocationRelativeTo(null); 49 p.setBackground(Color.black); 50 p.setLayout(new BorderLayout()); 51 this.rootname=rootname; 52 mainpanel(rootname); 53 Menu choice=new Menu(" 选项"); 54 choice.add(renew); 55 choice.add(start); 56 choice.add(end); 57 renew.addActionListener(this); 58 start.addActionListener(this); 59 end.addActionListener(this); 60 //----------------------------------- 61 Menu setmusic=new Menu(" 设置音乐"); 62 setmusic.add(offmusic); 63 setmusic.add(openmusic); 64 offmusic.addActionListener(this); 65 openmusic.addActionListener(this); 66 //----------------------------------- 67 Menu ability=new Menu(" 难度"); 68 ability.add(easy); 69 ability.add(medium); 70 ability.add(difficult); 71 ability.add(overability); 72 easy.addActionListener(this); 73 medium.addActionListener(this); 74 difficult.addActionListener(this); 75 overability.addActionListener(this); 76 77 //----------------------------------- 78 MenuBar bar=new MenuBar(); 79 bar.add(choice);bar.add(setmusic);bar.add(ability); 80 this.setMenuBar(bar); 81 this.add(p); 82 p.setVisible(true); 83 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 84 this.setVisible(true); 85 } 86 public void actionPerformed(ActionEvent e) 87 { 88 if(e.getSource()==renew) 89 { 90 changepanel(rootname); 91 } 92 else if(e.getSource()==start)//默认为简单模式 93 { 94 CutPicture(rootname); 95 } 96 else if(e.getSource()==end) 97 { 98 player.close(); 99 System.exit(0); 100 } 101 else if(e.getSource()==offmusic) 102 { 103 if(judge==true) 104 { 105 try 106 { 107 player.wait(); 108 player.close(); 109 } 110 catch (Exception e1) { 111 e1.printStackTrace(); 112 } 113 judge=false; 114 } 115 } 116 else if(e.getSource()==openmusic) 117 { 118 if(judge==false) 119 try { 120 BufferedInputStream buffer = new BufferedInputStream( 121 new FileInputStream(path)); 122 player = new Player(buffer); 123 player.play(); 124 judge=true; 125 } catch (Exception e1) { 126 e1.printStackTrace(); 127 } 128 } 129 else if(e.getSource()==easy) 130 { 131 132 } 133 else if(e.getSource()==medium) 134 { 135 136 } 137 else if(e.getSource()==difficult) 138 { 139 140 } 141 else if(e.getSource()==overability) 142 { 143 144 } 145 146 } 147 public void mainpanel(String rootname) 148 { 149 JPanel p1=new JPanel(); 150 p1.setLayout(null); 151 lpic=new JLabel(new ImageIcon(rootname)); 152 lpic.setBounds(0, 0,600, 600); 153 lpic.setLocation(0,0); 154 p1.add(lpic); 155 p1.setVisible(true); 156 try 157 { 158 ImageIcon a=(ImageIcon)lpic.getIcon(); 159 pic=a.getImage(); 160 } 161 catch(Exception e) 162 { 163 JOptionPane.showMessageDialog(null, "获取图片出错"); 164 } 165 p.add(p1); 166 } 167 public void changepanel(String rootname)//切换面板实现,切换图片的功能 168 { 169 p.removeAll(); 170 JPanel p1=new JPanel(); 171 p1.setLayout(null); 172 lpic=new JLabel(new ImageIcon(rootname)); 173 lpic.setBounds(0, 0,600, 600); 174 lpic.setLocation(0,0); 175 p1.add(lpic); 176 p1.setVisible(true); 177 try 178 { 179 ImageIcon a=(ImageIcon)lpic.getIcon(); 180 pic=a.getImage(); 181 } 182 catch(Exception e) 183 { 184 JOptionPane.showMessageDialog(null, "获取图片出错"); 185 } 186 p.add(p1); 187 p.validate(); 188 } 189 public void CutPicture(String rootname)//切割图片 190 { 191 p.removeAll(); 192 JPanel p1=new JPanel(); 193 int x=0,y=0,i=0; 194 ImageFilter []cropFilter=new ImageFilter[9]; 195 Image [] imgs=new Image[9]; 196 ImageIcon [] img1=new ImageIcon [9]; 197 JLabel []t=new JLabel[9]; 198 MyMouseInputAdapter []listener=new MyMouseInputAdapter [9]; 199 p1.setLayout(null); 200 for(x=0;x<=400;x+=200) 201 for(y=0;y<=400;y+=200) 202 { 203 cropFilter[i]= new CropImageFilter(x,y,200,200);//创建好切图 204 imgs[i] = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(pic.getSource(), cropFilter[i])); 205 img1[i]=new ImageIcon(imgs[i]); 206 t[i]=new JLabel(img1[i]); 207 t[i].setBounds(0, 0,200, 200); 208 listener[i]=new MyMouseInputAdapter(t[i]); //鼠标事件处理 209 t[i].setLocation(y,x);//设置位置 210 t[i].addMouseListener(listener[i]); //增加标签的鼠标事件处理 211 t[i].addMouseMotionListener(listener[i]); 212 p1.add(t[i]); 213 i++; 214 } 215 p1.setVisible(true); 216 p.add(p1); 217 p.validate(); 218 } 219 public static BufferedImage toBufferedImage(Image image) { 220 if (image instanceof BufferedImage) { 221 return (BufferedImage)image; 222 } 223 else 224 { 225 JOptionPane.showMessageDialog(null, "图片转换出错"); 226 return (BufferedImage)image; 227 } 228 229 } 230 231 public static void main(String[] args) { 232 String rootname="pic\\Tower.jpg"; 233 new window(rootname); 234 235 236 } 237 238 }
的,你可以根据图片放置的位置计算裁剪区域的坐标,坐标是多少和你想裁剪的大小有关。
你的问题很大,这个牵涉到很多图像技术。
比如图片处理,包括锐化、除燥、二值化图片等等操作,还有一堆算法去计算图像区域,与图像边界。
换句话说,不是那么容易。