writing image to byte-array

Discussion in 'Java' started by Georg Andersson, Jul 26, 2004.

  1. hi

    how can i write an image into a byte-array?

    best regards
     
    Georg Andersson, Jul 26, 2004
    #1
    1. Advertising

  2. Georg Andersson

    JScoobyCed Guest

    Georg Andersson wrote:
    > hi
    >
    > how can i write an image into a byte-array?
    >
    > best regards


    Well... It depends what you mean by byte-array. Do you want the file
    loaded in an array of byte ? Do you want the RGB equivalent array ?
    For the first one:
    <sniplet>
    File myImageFile = new File("path_to_image.jpg");
    FileInputStream fis = new FileInputStream(myImageFile);
    byte[] data = new byte[1024];
    byte[] tmp = new byte[0];
    byte[] myArrayImage = new byte[0];
    int len = 0 ;
    int total = 0;
    while( (len = fis.read(data)) != -1 ) {
    total += len;
    tmp = myArrayImage;
    myArrayImage = new byte[total];
    System.arraycopy(tmp,0,myArrayImage,0,tmp.length);
    System.arraycopy(data,0,myArrayImage,tmp.length,len);
    }
    fis.close();
    </sniplet>
    Note: this might not be the most efficient way... But that does the job.

    For the array of RGB values:
    <sniplet>
    Image img = getImage();
    BufferedImage bu = new BufferedImage(img.width,
    img.height,BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = bu.createGraphics();
    g2.drawImage(img,0,0,this); // this referes to the current
    //ImageObserver
    g2.dispose();
    int[] rgb = bu.getRGB(0, 0, img.width, img.height, null, 1);
    </sniplet>

    You can have a look at the BufferedImage in the javadoc.

    --
    JScoobyCed
    What about a JScooby snack Shaggy ? ... Shaggy ?!
     
    JScoobyCed, Jul 26, 2004
    #2
    1. Advertising

  3. Georg Andersson

    Dave Neary Guest

    Hi,

    On 26 Jul 2004 03:16:21 -0700, Georg Andersson said:
    > how can i write an image into a byte-array?


    BufferedImage img = ImageIO.read(inputFile);
    ByteArrayOutputStream bas =
    new ByteArrayOutputStream();
    ImageIO.write(img, "pnm", bas);
    byte[] data = bas.toByteArray();

    This might not be what you want, though...

    Cheers,
    Dave.

    --
    David Neary,
    E-Mail: bolsh at gimp dot org
    Work e-mail: d dot neary at phenix dot fr
    CV: http://dneary.free.fr/CV/
     
    Dave Neary, Jul 26, 2004
    #3
    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. =?Utf-8?B?RGVhc3Vu?=

    Conversion issue: Image>Byte array>Image

    =?Utf-8?B?RGVhc3Vu?=, Jul 14, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    924
    =?Utf-8?B?RGVhc3Vu?=
    Jul 14, 2005
  2. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,464
    Roedy Green
    Aug 5, 2003
  3. Kirby
    Replies:
    3
    Views:
    681
    Kirby
    Oct 8, 2004
  4. Replies:
    20
    Views:
    9,945
    licebmi
    Sep 7, 2009
  5. Tom McGlynn
    Replies:
    4
    Views:
    889
    Mark Space
    Apr 19, 2008
Loading...

Share This Page