热度 7 ||
讨论一
请问怎么从BufferedImage image得到byte[]数据. ------------------------------------------------------------------------------------------------------ 我现在已经有BufferedImage image, 怎么得到一个byte[]的数组呢?? 但是不要经过文件转。否则太慢了了。 ------------------------------------------------------------------------------------------------------ BufferedImage srcImage = ImageIO.read(new File( "c:/xxx.jpg ")); byte[] data = ((DataBufferByte) srcImage.getData().getDataBuffer()).getData(); ------------------------------------------------------------------------------------------------------ 那怎么通过byte[] data 再次得到一个图片呢?? ------------------------------------------------------------------------------------------------------ java.lang.ClassCastException: java.awt.image.DataBufferInt 报错了。 BufferedImage srcImage = rebot.createScreenCapture(rec); 我的BufferedImage 是从rebot.createScreenCapture(rec);得到的。 ------------------------------------------------------------------------------------------------------ 一个BufferedImage可以得一个Int[]数组.用它的getRGB方法.取得的是它的相素信息. 从一个int[]到BufferedImage可以用MemoryImageSource. ------------------------------------------------------------------------------------------------------ 我想取得byte[]数组是没有什么意义的.当然不是不可以实现.用ImageIO类. BufferedImage bi=ImageIO.read(new ByteArrayInputSream(byte[]); ImageIO.write(bufferedImage, "gif ",new ByteArrayOutputStream(new byte[20000]));然后你就可以从这个ByteArrayOutputStream取得你想要的byte啦! 不过这样一点意义也没有,你无法改任何相素. ------------------------------------------------------------------------------------------------------ 我现在就是想做一个给多个人显示桌面的程序。 不知道上面的高手有没有相关的代码啊。 能发来看看不?? ------------------------------------------------------------------------------------------------------ 事例一
BufferedImage image=ImageIO.read(new File("c://test//error.jpg"));
BufferedImage imageSub=image.getSubimage(100,100,200,200);
ByteArrayOutputStream bao=new ByteArrayOutputStream();
ImageIO.write(imageSub, "jpg",bao);
byte[] bytes= bao.toByteArray();
ByteArrayInputStream bis=new ByteArrayInputStream(bytes);
String strsql="insert into aa(a) values (?)";
NoWebConn connObj=new NoWebConn();//自己的一个连接类,只是为了连接数据库
Connection conn=connObj.getConnection();
java.sql.PreparedStatement ppstmt=conn.prepareStatement(strsql);
ppstmt.setBinaryStream (1, bis,bytes.length);
ppstmt.executeUpdate();
ppstmt.close();
public static void main(String[] args) throws Exception
{
int width = 100;
int height = 100;
String s = "你好";
File file = new File("c:/image.jpg");
Font font = new Font("Serif", Font.BOLD, 10);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)bi.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.RED);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(s, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.drawString(s, (int)x, (int)baseY);
ImageIO.write(bi, "jpg", file);
}手机版|中文乐高 ( 桂ICP备13001575号-7 )
GMT+8, 2025-12-19 07:10 , Processed in 0.163205 second(s), 19 queries .
Powered by Discuz! X3.5
Copyright © 2001-2020, Tencent Cloud.