马上注册 登录
中文乐高 返回首页

糖伯虎的个人空间 https://bbs.cmnxt.com/?1 [收藏] [复制] [分享] [RSS]

日志

java中的bufferedImage

热度 7已有 2170 次阅读2012-2-8 19:46 |个人分类:Java| java

讨论一
请问怎么从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);      
    }

路过

雷人

握手

鲜花

鸡蛋

发表评论 评论 (2 个评论)

hidden
flicker 彩虹炫 | flicker 显身卡 | 本站网友2013-2-10 10:07
Your's is a point of view where real intelligence sihens through.
hidden
flicker 彩虹炫 | flicker 显身卡 | 本站网友2013-2-12 18:40
That's a smart way of thninkig about it.

facelist

您需要登录后才可以评论 登录 | 马上注册

QQ|手机版|中文乐高 ( 桂ICP备13001575号-7 )

GMT+8, 2024-5-3 19:03 , Processed in 0.047907 second(s), 19 queries .

Powered by Discuz! X3.5

Copyright © 2001-2020, Tencent Cloud.

返回顶部