急问关于Java中图像处理的问题!
请教一个关于在JAVA中图像处理的问题:
将由PRINT SCREEN键抓屏得到的图像,经过处理以JPEG文件格式保存到硬盘上
后。由于要在聊天程序中再次读入这个图像文件,发送给对方,所以将其读入内存(具体过
程,先用FileInputStream,再用BufferedInputStream,读入Byte[]数组形式的名为oldByte
内存区中)。
现在需要在图像中加入一些处理,有点类似于WINDOWS附件中的画板,需要有在图像中
画直线,选定区域等功能。这里,图像是用ImageIcon强制转换为图标,显示在JLablel 上
的,请问,怎么处理呢?我想要把oldByte强制转换为Image,然后再用getGraphics,进入到
图形环境,才能进行画线之类的操作,可是,我怎么也实现不了。
顺便说一下,还要求把修改的图像能再保存回原图像文件中。
附上源代码,以作参考:
byte[] oldByte = new byte[0];
jLabel1.setIcon(new ImageIcon(oldByte));
public boolean getClipboard() {
Transferable t =
Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try {
if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
//String text =
(String)t.getTransferData(DataFlavor.stringFlavor);
java.awt.image.BufferedImage capture =
(java.awt.image.BufferedImage)
t.getTransferData(DataFlavor.imageFlavor);
FileOutputStream out = new FileOutputStream(imageFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(capture);
out.flush();
out.close();
return true;
}
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
private byte[] getImgByte()
{
try{
FileInputStream fis = new FileInputStream ( imageFile ) ;
// Use Buffered Stream for reading/writing.
BufferedInputStream bis = new BufferedInputStream ( fis ) ;
byte[] buff = new byte[1024] ;
int bytesRead ;
// Simple read/write loop.
int length = 0 ;
while ( -1 != ( bytesRead = bis.read ( buff , 0 , buff.length ) ) ) {
oldByte = mergeByte(oldByte,buff,bytesRead);
}
fis.close();
bis.close();
return oldByte;
}
catch(IOException e)
{
e.printStackTrace();
}
return null;
}
private byte[] mergeByte(byte[]oldByte , byte[] newByte,int length)
{
int old_len = oldByte.length;
int new_len = length;
byte[] currByte = new byte[old_len+new_len];
System.arraycopy(oldByte,0,currByte,0,old_len);
System.arraycopy(newByte,0,currByte,old_len,new_len);
return currByte;
}