Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: image converting to byte

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default image converting to byte

    hi , am converting gray image to byte array and v disice versa by using bytearrayoutputstream and inputstream but it didn't displayed image am try by getting width and hieght but tere difference in size any body help me please with this problem


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: image converting to byte

    How are you testing the code?
    I don't understand what conversion is needed.
    An image is already an array of bytes.

    Post a small complete program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with image

    Hi
    i have problem with converting gray image to byte array and byte array to image in java, the size of input image are difference
    with the output image ,anybody can help me

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: image converting to byte

    Please read the forum announcements - I have merged your seemingly identical threads.

    I suggest posting an SSCCE as Norm suggested, unless you wish someone to take a wild guess.

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: image converting to byte

    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferByte;
    import java.awt.image.WritableRaster;
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.IOException;

    import javax.imageio.ImageIO;


    public class ImageProMainClass2 {

    public ImageProMainClass2() {

    }

    public static byte[] imgFileToByteArray(String imgFileName){

    int width = 0 ;
    int height = 0 ;
    byte[] readData = null;
    try {
    File imgPath = new File(imgFileName);
    BufferedImage bufferedImage = ImageIO.read(imgPath);

    // get DataBufferBytes from Raster
    WritableRaster raster = bufferedImage .getRaster();
    DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
    readData=data.getData();

    //return ( readData );


    } catch (IOException e) {
    e.printStackTrace();
    }

    return readData;
    }
    public static BufferedImage byteArrayToBufImg(byte[] array)
    {
    BufferedImage imgout = new BufferedImage(12, 12, BufferedImage.TYPE_INT_ARGB_PRE);
    WritableRaster wr = imgout.getRaster();
    int data = 0 ;
    int index = 0 ;
    for(int i = 0 ; i<12 ;i++){
    for(int j = 0 ; j<12 ;j++){
    wr.setSample(i, j, 0, array[index]);
    index++;
    }
    }
    for(int i = 0 ; i<array.length;i++){
    System.out.println("this is array2["+i+"]:"+array[i]);
    }
    return imgout;
    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    String filename = "strawberry.png" ;
    byte[] imgData = imgFileToByteArray(filename);
    BufferedImage img = byteArrayToBufImg(imgData);

    try {
    ImageIO.write(img, "png", new File("out2.png"));
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }





    }

    }

    --- Update ---

    thanks very much for answer my question i convert image to array of byte and then convert the array of byte to image again , but when i convert the array of byte to image the image difference with the original image in size, i take gray scale png image
    regards

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: image converting to byte

    Can you post some data or output showing what the problem is?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: image converting to byte

    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferByte;
    import java.awt.image.WritableRaster;
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
     
     
    public class ImageProMainClass2 {
     
    public ImageProMainClass2() {
     
    }
     
    public static byte[] imgFileToByteArray(String imgFileName){
     
    int width = 0 ;
    int height = 0 ;
    byte[] readData = null;
    try {
    File imgPath = new File(imgFileName);
    BufferedImage bufferedImage = ImageIO.read(imgPath);
     
    // get DataBufferBytes from Raster
    WritableRaster raster = bufferedImage .getRaster();
    DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
    readData=data.getData();
     
    //return ( readData );
     
     
    } catch (IOException e) {
    e.printStackTrace();
    }
     
    return readData;
    }
    public static BufferedImage byteArrayToBufImg(byte[] array)
    { 
    BufferedImage imgout = new BufferedImage(12, 12, BufferedImage.TYPE_INT_ARGB_PRE);
    WritableRaster wr = imgout.getRaster();
    int data = 0 ;
    int index = 0 ;
    for(int i = 0 ; i<12 ;i++){
    for(int j = 0 ; j<12 ;j++){
    wr.setSample(i, j, 0, array[index]);
    index++;
    }
    }
    for(int i = 0 ; i<array.length;i++){
    System.out.println("this is array2["+i+"]:"+array[i]);
    }
    return imgout;
    }
     
    public static void main(String[] args) {
    // TODO Auto-generated method stub
     
    String filename = "strawberry.png" ;
    byte[] imgData = imgFileToByteArray(filename);
    BufferedImage img = byteArrayToBufImg(imgData);
     
    try {
    ImageIO.write(img, "png", new File("out2.png"));
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
     
     
     
     
     
    }
     
    }


    i used small logo by size 12*12 the input logo size 28 kb the out is logo 128 bytes am try to do the same code but depending on size and by using input and output stream but i still have size problem issue
    regard

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: image converting to byte

    The posted code has lost all its formatting.

    Why are you using Raster? Could you get the bytes directly from the BufferedImage and restore them directly to a BufferedImage.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: image converting to byte

    i get the byte from buffered image and restore them from buffered image but i still have the same problem ,second i test it by image correlation measure to test their similarity the correlation zero , my head go to explode with it

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: image converting to byte

    Please post the new code.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: image converting to byte

    my code is to long i embed image bit to audio but when restore the image my byte restored correctly but i have problem with reconstruct the image
    i get the the error reading png file so i skip 8 byte befor embedding and call the original image in extract just to get the header pa
    rt from it

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: image converting to byte

    If the image file is not written correctly, I think you would get some kind of error.
    Without code that executes, it is very hard to find the problem.

    Don't post a large program. Make a SSCCE.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Byte array to image

     File file=new File(imgName);
           byte[] originalImage=null;
     
     
         try{ // here i read the original image to get the header  
             BufferedImage Image = ImageIO.read(file);
                 ByteArrayOutputStream aos = new ByteArrayOutputStream();
                    ImageIO.write(Image, "gif", aos);
                    //System.out.println("this is bytearray:"+baos);
                    aos.flush();
                    originalImage = aos.toByteArray();
    // here is the returned bit to reconstruct the image
              BitSet result=returnphase();
           /
            byte[] array=result.toByteArray();
    // skip 14 if it is gif type
            for (int i=14; i<array.length;i++)
            {// put the returned in original   
             originalImage[i]=array[i];
     
            }
            for (int i=0; i<originalImage.length;i++)
            { // here i concatenate with the header  
                System.out.println("this is origin["+i+"]:"+originalImage[i]);
            }
     
             ByteArrayInputStream in = new ByteArrayInputStream(originalImage);
              final  BufferedImage bufferedImage = ImageIO.read(in);
    // here i displayed the image in fff file
                 File imagefile =new File("fff.gif");
                ImageIO.write(bufferedImage, "gif",imagefile );
         }catch (Exception ex) {
                ex.printStackTrace();

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How Convert a Byte array to a image format

    Please post a complete program that will compile, execute and show the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How Convert a Byte array to a image format

    i told you my code is too long it i 20 class but i get the errorerror reading png header file ) or any type of image please if you can help me

  16. #16
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: image converting to byte

    Please stop posting the same thing in new threads, and please do not hijack threads that are over 4 years old. I have merged the threads. Please keep the discussion of your problem in this thread. If you continue to ignore moderator warnings, you leave us no alternative than to take further action.

Similar Threads

  1. Byte Array of SVG to Image file
    By JavaCoffee in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2013, 02:57 PM
  2. Problem recreating an image from a byte array
    By Kalelovil in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 19th, 2012, 01:17 AM
  3. Replies: 1
    Last Post: April 7th, 2011, 01:32 PM
  4. How Convert a Byte array to a image format
    By perlWhite in forum Algorithms & Recursion
    Replies: 7
    Last Post: February 19th, 2011, 03:16 PM
  5. Replies: 2
    Last Post: June 29th, 2009, 03:06 PM