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 15 of 15

Thread: Image to file and contra

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Image to file and contra

    Is there any way to write an array of images into file ( for example text file) and read them from file (in Java of course).


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Image to file and contra

    Yes. You can write images to a file, by concatenating one after the other. I think you'd need a record length field to keep track of each separate image.
    Why do you want them in a text file? Images are composed of binary data that is useless to view in a text editor.
    If you need to convert the bytes of the images to text, look at Base64 encoding.

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Image to file and contra

    I am making project and its GUI needs to show some images (that can be changed in program) and then I want to save them back. Saving images in folder "Images" and something like that will make things more difficult (difficulties with picture names etc...) I use the following program:

    [highlight]
    BufferedImage image=ImageIO.read(new File(legal file path));
     
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", baos);
            byte[] bitii=baos.toByteArray();
            FileOutputStream f=new FileOutputStream(new File(legal file path));
            f.write(biti);
     
    [/highlight]

    Yesterday it did not work properly, today it works norm can I use a hint of placing 1 or more empty lines between two different images and then read first one until I reach empty line and so on?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Image to file and contra

    I would recommend against writing multiple images to the same file. Instead, create multiple files with one image per file.

    To read/write images, see this post: Images (read/write and drawing)

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Image to file and contra

    What byte makes an empty line? The new line character? Since the image is binary, it can contain any byte value including the newline character. It could be most images would not have 3 newline characters in a row. But it is not impossible.

    For an exercise: Write an image to a byte array. Create a histogram of the bytes used in an image by writing a loop to go thru that byte array and use the value of the byte to increment/count occurances. Then print out the results. For example:
    byte[] theImage // the array with the image bytes
    int[] histogram = new int[256]; // the byte usage counters
     
    for(int i =0; i < theImage.length; i++) {
      histogram[theImage[i] & 0xFF]++; // count usage of this byte from the image
    }
    System.out.println("histo=" + Arrays.toString(histogram));  // show the counts
    Last edited by Norm; August 29th, 2010 at 03:20 PM. Reason: Use int and add & 0xFF

  6. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Image to file and contra

    Or use a ZipOutputStream and write the images as separate ZipEntries to a ZipFile.

    db

  7. #7
    Junior Member
    Join Date
    Aug 2010
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Image to file and contra

    Thanks for advices.

    I have also found Base64 encoder and decoder at Encode/Decode to/from Base64 - Real's Java How-to and downloaded java mail from https://cds.sun.com/is-bin/INTERSHOP...-CDS_Developer
    and how can I add this classes ( something like library) to be used in my project .
    I tried to add this one to bootstrap entries (I am using Eclipse) but import of javax.mail.internet.MimeUtility is not recognized yet ?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Image to file and contra

    Why do you want the image's bytes to be output to a text file? Is this a student exercise to see how to do something?

    Sorry, I have no idea how to configure your IDE.

  9. #9
    Junior Member
    Join Date
    Aug 2010
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Image to file and contra

    I am doing Java project in which each user should have its own picture that can be changed and I have to save these changes Since there can be many many users it can become a little bit difficult to deal with all these pictures and I wanted to write them in just one tex file but I have forsaken this idea (because it is a little bit complicated and I do not have too much time now to deal with all these conversions + javax.mail.internet.MimeUtility not recognized and so on) and will save each image separately in one folder.

  10. #10
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Image to file and contra

    You still haven't explained why you want to use a TEXT file. If that requirement is not part of some learning assignment, it's a downright stupid approach.

    db

  11. #11
    Junior Member
    Join Date
    Aug 2010
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Image to file and contra

    Darryl.Burke I think that you have not read my first post carefully I wrote : for example text file It can be any type of file but aim is to have just one file instead of many pictures. I am also sure that it will increase my project points and of course I want to learn how to do this (not just for project points).

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Image to file and contra

    user should have its own picture that can be changed and I have to save these changes
    How will the images be changed if they are all in one file? To change any one image in the file, you will have to write out the file with all of the images.

  13. #13
    Junior Member
    Join Date
    Aug 2010
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Image to file and contra

    norm When program starts, it reads all of images from file Then a frame containing combo box for name selection is drawn. And if for example John Norm is selected then panel containing his picture and some infos is shown. We can change a picture that is shown and when we close application all these pictures should be saved back into file from which they were read.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Image to file and contra

    Ok that could work if you read ALL of the images in at the start and write all of the images out at the end and don't do any I/O to the images file while the program is running.

    I'm attaching a zip with a project I did a while back that does something like you are trying to do. I had a web site that restricted the number of files I could post. It did allow jar files and .dat files. So I put all the images in one file vs have separate files for each image. Enjoy.

    Norm
    Attached Files Attached Files
    Last edited by Norm; August 31st, 2010 at 10:44 AM.

  15. The Following User Says Thank You to Norm For This Useful Post:

    Javabeginner (September 2nd, 2010)

  16. #15
    Junior Member
    Join Date
    Aug 2010
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Image to file and contra

    Thanks for post.

Similar Threads

  1. How to read a text from an Image file?
    By GautamRy in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: October 5th, 2011, 05:02 AM
  2. How to Convert DWG File to an Image
    By lance9200 in forum Java Theory & Questions
    Replies: 0
    Last Post: June 17th, 2010, 10:21 AM
  3. Image location on a Runnable JAR file?
    By DarrenReeder in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 07:59 AM
  4. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM
  5. Saving an image file (PGM format)
    By Mickey2315 in forum Algorithms & Recursion
    Replies: 3
    Last Post: September 12th, 2009, 01:18 AM