convert BufferedImage to a byte array

Discussion in 'Java' started by cyberco, Feb 12, 2006.

  1. cyberco

    cyberco Guest

    I want to get the bytes from a BufferedImage to send it over the line,
    how can I achieve that? I seems to be trickier than I thought.
    cyberco, Feb 12, 2006
    #1
    1. Advertising

  2. cyberco

    Timo Stamm Guest

    cyberco schrieb:
    > I want to get the bytes from a BufferedImage to send it over the line,
    > how can I achieve that? I seems to be trickier than I thought.



    See ImageIO
    Timo Stamm, Feb 12, 2006
    #2
    1. Advertising

  3. cyberco

    Roedy Green Guest

    On 12 Feb 2006 06:19:21 -0800, "cyberco" <> wrote,
    quoted or indirectly quoted someone who said :

    >I want to get the bytes from a BufferedImage to send it over the line,
    >how can I achieve that? I seems to be trickier than I thought.


    you can't serialise it. The actual form is platform dependent, so it
    might not mean anything when you got to the other end. What you want
    to do is extract an array of ints or shorts or bytes and send that by
    serialisation or DataOutputStream.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Feb 12, 2006
    #3
  4. cyberco

    Roedy Green Guest

    On Sun, 12 Feb 2006 16:36:20 GMT, Roedy Green
    <> wrote, quoted or
    indirectly quoted someone who said :

    >>I want to get the bytes from a BufferedImage to send it over the line,
    >>how can I achieve that? I seems to be trickier than I thought.

    >
    >you can't serialise it. The actual form is platform dependent, so it
    >might not mean anything when you got to the other end. What you want
    >to do is extract an array of ints or shorts or bytes and send that by
    >serialisation or DataOutputStream.


    you can convert it to png or jpg format and send that.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Feb 12, 2006
    #4
  5. cyberco

    cyberco Guest

    Yes, getting the bytes is what I'm trying to do. Unfortunately ImageIO
    does not have any convenience methods for that purpose. Any other
    suggestions?
    cyberco, Feb 12, 2006
    #5
  6. cyberco

    Timo Stamm Guest

    cyberco schrieb:
    > Yes, getting the bytes is what I'm trying to do.


    Roedy pointed out why BufferedImage itself isn't serializable.

    You could access the matrices and send that data over the line, but you
    will most likely need some meta data for different color models etc. and
    will reinvent the wheel, because there are already dozens of image file
    formats available through ImageIO.


    > Unfortunately ImageIO does not have any convenience methods for that
    > purpose.



    What could be more convenient than:

    ImageIO.write(RenderedImage im, String formatName, OutputStream output);

    ?


    Timo
    Timo Stamm, Feb 12, 2006
    #6
  7. cyberco

    cyberco Guest

    Oops! Sorry, Timo, you're right. My bad. I overlook ImageIO.write(). I
    succeeded getting the byte[] in the following way.

    ####################
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(bufImage, formatName, baos);
    byte[] bytesOut = baos.toByteArray();
    cyberco, Feb 12, 2006
    #7
  8. cyberco

    E. Naubauer Guest

    cyberco schrieb:
    > I want to get the bytes from a BufferedImage to send it over the line,
    > how can I achieve that? I seems to be trickier than I thought.
    >


    If you know the type of the data buffer of the BufferedImage its easy:

    int[] t =
    ((DataBufferInt)(myBufferedImage).getRaster().getDataBuffer()).getData();

    with DataBufferByte it works the same.
    E. Naubauer, Feb 12, 2006
    #8
  9. cyberco

    Roedy Green Guest

    On 12 Feb 2006 10:46:10 -0800, "cyberco" <> wrote,
    quoted or indirectly quoted someone who said :

    >Yes, getting the bytes is what I'm trying to do. Unfortunately ImageIO
    >does not have any convenience methods for that purpose. Any other
    >suggestions?


    It can write to an OutputStream. Make that a ByteArrayOutputStream.
    see
    http://mindprod.com/jgloss/imageio.html#TOBYTES
    for how.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Feb 13, 2006
    #9
  10. cyberco

    cyberco Guest

    Excellent examples, Roedy! Thanks for the link!
    cyberco, Feb 13, 2006
    #10
  11. opalinski from opalpaweb, Feb 13, 2006
    #11
  12. cyberco

    greejose

    Joined:
    Jun 27, 2008
    Messages:
    1
    Similar Issue

    When I create my cropped image it is showing as a black square. The width and the height are correct but it is not "cropping" from the original image. Any help would be appreciated thanks!

    String sp = "C:\\testin.jpg";
    Image image = new ImageIcon(sp).getImage();
    //image = (Image) request.getSession().getAttribute("fixed_image");

    BufferedImage resizedImage = new BufferedImage(200, 200,BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = resizedImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    graphics2D.drawImage(image, 0,0, 80, 80, null, null);

    BufferedImage croppedImage = resizedImage.getSubimage(1, 90, 90, 90);
    File tempout = new File("C:\\testout.jpg");
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
    //FileOutputStream outImg = new FileOutputStream(tempimg);
    ImageIO.write(croppedImage, "jpg", baos);
    byte[] b = baos.toByteArray();

    FileOutputStream outImg = new FileOutputStream(tempout);
    outImg.write(b);
    greejose, Jun 27, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kirby
    Replies:
    3
    Views:
    637
    Kirby
    Oct 8, 2004
  2. Shane Wealti

    convert byte[] to Byte[]

    Shane Wealti, Jun 13, 2005, in forum: Java
    Replies:
    5
    Views:
    42,163
    Brzezi
    Jun 13, 2005
  3. Replies:
    20
    Views:
    9,751
    licebmi
    Sep 7, 2009
  4. Replies:
    1
    Views:
    1,803
  5. Polaris431
    Replies:
    8
    Views:
    753
    SM Ryan
    Dec 4, 2006
Loading...

Share This Page