急问关于Java中图像处理的问题!

galaxy999 2004-08-13 06:03:13
请教一个关于在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;
}



...全文
120 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
朋友别哭 2004-08-13
  • 打赏
  • 举报
回复
好像是
up
galaxy999 2004-08-13
  • 打赏
  • 举报
回复
这个问题是不是很棘手啊?
tomcatjava 2004-08-13
  • 打赏
  • 举报
回复
没涉及过图像处理!帮你顶顶把!

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧