aspose file tools*
The moose likes Java in General and the fly likes How to encode image in parts? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to encode image in parts?" Watch "How to encode image in parts?" New topic
Author

How to encode image in parts?

Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Hi, I want to encode image into Base64 format and write it into text file. I had wrote the code which runs fine. My code is like



But now I want to create array as instead of .
and encode it and write into file, after writing first chunk then read next 102400 bytes and write to file (i.e. append to the txt file) till the end of file. How to do that? Thanks in advance.

Life is easy because we write the source code.....
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Anybody know how to do this?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Why do you want to do this? Is it because your file is too big to fit into memory? If so then consider the use of a stream approach such as the Base64OutputStream in http://commons.apache.org/codec/ .


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629


Thanks James.
Why do you want to do this? Is it because your file is too big to fit into memory?

Yes, But how to do that?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Pramod P Deore wrote:
Thanks James.
Why do you want to do this? Is it because your file is too big to fit into memory?

Yes, But how to do that?


Err .. did you follow the link I provided?
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

But I can't get it. And actually I want to develop this application for mobile and in mobile there is no much memory available like desktop. So I am thinking that read some part of image encode it and save in text file after that read another chunk of bytes and encode them.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Pramod P Deore wrote:But I can't get it.

What can't you get? The site or the library.
And actually I want to develop this application for mobile and in mobile there is no much memory available like desktop.

Which is exactly why you need Base64OutputStream.

> So I am thinking that read some part of image encode it and save in text file after that read another chunk of bytes and encode them.

Which is exactly why you need Base64OutputStream.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

I really don't get how to use it.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Pramod P Deore wrote:I really don't get how to use it.


1) Wrap a FileOutputStream (or some other OutputStream) with a Base64OutputStream.
2) Allocate a byte[] as intermediate buffer. Start with it being fairly small (say 1024 bytes) and later once you have it working experiment to see what size gives you an acceptable performance.
3) Loop reading into the buffer from your InputStream and writing immediately to the Base64OutputStream. Make sure you only write the number of byes actually read. Look at the Javadoc for InputStream.read(byte[] ) to understand this.
4) When you have read and written all the content of the InputStream, close the Base64OutputStream.

Job done!
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Now I think I got it. Thanks James, Now I will try it.
vikas mhatre
Greenhorn

Joined: Apr 24, 2010
Posts: 26
Hi, also want to this but can't succeed anybody please help me.
vikas mhatre
Greenhorn

Joined: Apr 24, 2010
Posts: 26
Hi, Pramod can you help me? Hello anyone is there?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

vikas mhatre wrote:Hi, also want to this but can't succeed anybody please help me.


Short of actually giving you the code I don't see how anyone can help any further until you post what you have produced so far. If you do post the code will of course give details of what does not work and you will post full exception stack traces.
vikas mhatre
Greenhorn

Joined: Apr 24, 2010
Posts: 26
as here it says
Larger data are better handled with streams. The next sample code encodes a file to another, using java.io.InputStream and java.io.OutputStream objects:

InputStream inputStream = new FileInputStream("source.jpg");
OutputStream outputStream = new FileOutputStream("encoded.b64");
Base64.encode(inputStream, outputStream);
outputStream.close();
inputStream.close();


here source,jpg is the image file we have to encode and encoded.b64 is the file where we write encoded image I am correct?

Now I had modified earlier code as


But it give me error as
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

vikas mhatre wrote:
Now I had modified earlier code as


I don't think you have read this thread at all.
vikas mhatre
Greenhorn

Joined: Apr 24, 2010
Posts: 26
Then what is wrong there? will you please explain me?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

vikas mhatre wrote:Then what is wrong there? will you please explain me?


I gave a list of the steps to use using the Apache codec class Base64OutputStream. Your code does not use that class. If you choose instead to use some other class then you should look at it's Javadoc to see how to use it.
vikas mhatre
Greenhorn

Joined: Apr 24, 2010
Posts: 26

1) Wrap a FileOutputStream (or some other OutputStream) with a Base64OutputStream.

Here suppose I want to write data to file zzz.txt then


am I correct? If not then please correct me
vikas mhatre
Greenhorn

Joined: Apr 24, 2010
Posts: 26
But I don't know how to step 2 and 3
2) Allocate a byte[] as intermediate buffer. Start with it being fairly small (say 1024 bytes) and later once you have it working experiment to see what size gives you an acceptable performance.
3) Loop reading into the buffer from your InputStream and writing immediately to the Base64OutputStream. Make sure you only write the number of byes actually read. Look at the Javadoc for InputStream.read(byte[] ) to understand this.
vikas mhatre
Greenhorn

Joined: Apr 24, 2010
Posts: 26
Hello James are you there?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
vikas mhatre wrote:Hello James are you there?
Read this: Patience Is A Virtue.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

vikas mhatre wrote:Hello James are you there?


I'm here. I'm afraid that dealing with forum queries takes second place to getting my lawn mowed while it is not actually raining.

While I was away did I miss some earth shattering news - has Google broken?
vikas mhatre
Greenhorn

Joined: Apr 24, 2010
Posts: 26
1) Wrap a FileOutputStream (or some other OutputStream) with a Base64OutputStream.
2) Allocate a byte[] as intermediate buffer. Start with it being fairly small (say 1024 bytes) and later once you have it working experiment to see what size gives you an acceptable performance.
3) Loop reading into the buffer from your InputStream and writing immediately to the Base64OutputStream. Make sure you only write the number of byes actually read. Look at the Javadoc for InputStream.read(byte[] ) to understand this.
4) When you have read and written all the content of the InputStream, close the Base64OutputStream.


I don't get the 3rd point. I had tried like this


But I didn't found any method in InputStream that return String instead of int as int test = inputStream.read(b);
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

vikas mhatre wrote:[
I don't get the 3rd point.

In your code I don't see the read() being in a loop and I don't see you writing out the bytes you have read. What is the problem with doing that?
vikas mhatre
Greenhorn

Joined: Apr 24, 2010
Posts: 26
Hi James Now look I have Base64 class which accept byte array and return encoded String I have write the code like this


It write data to file but I am not getting how to read next part of image , How to apply the loop?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

vikas mhatre wrote:Hi James Now look I have Base64 class


YOU DON'T NEED A Base64 CLASS. YOU HAVE A Base64OutputStream WHICH DOES ALL THE HARD WORK. IN A LOOP YOU NEED TO READ FROM THE InputStream AND THEN WRITE WHAT HAS BEEN READ TO THE Base64OutputStream UNTIL YOU REACH THE END OF THE InputStream. NO String WRITING IS REQUIRED. NO Base64 CLASS IS REQUIRED. YOU JUST NEED 3 LINES OF CODE.

Google will yield a host of examples of how to read from an InputStream and write to an OutputStream. 3 lines of code. I say again - 3 lines of code. I'm not writing them for you.
vikas mhatre
Greenhorn

Joined: Apr 24, 2010
Posts: 26
Here in am reading and writing using input and outputStream but how to read next part of image
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
In addition to what James says, you can't mix streams with Readers/Writers indiscriminately. That right away raises the question of encodings, which your code completely ignores. Plus, it's almost always incorrect to use "new String(byte[])".

You should read http://faq.javaranch.com/java/ReadDoesntDoWhatYouThinkItDoes
vikas mhatre
Greenhorn

Joined: Apr 24, 2010
Posts: 26
IN A LOOP YOU NEED TO READ FROM THE InputStream AND THEN WRITE WHAT HAS BEEN READ TO THE Base64OutputStream UNTIL YOU REACH THE END OF THE InputStream.


I had read and write but I am not able to apply the loop

How to apply the loop?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
James Sabre, please takes your caps lock off.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

vikas mhatre wrote:
IN A LOOP YOU NEED TO READ FROM THE InputStream AND THEN WRITE WHAT HAS BEEN READ TO THE Base64OutputStream UNTIL YOU REACH THE END OF THE InputStream.


I had read and write but I am not able to apply the loop[code]



This is Java Input/Output 101 knowledge so before going any further you need to spend some time learning about Java IO. As I said, I am not going to write this code for you. I will not respond again until I see that you have spent some time learning about Java IO.

Best of luck.

Bye
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Campbell Ritchie wrote:James Sabre, please takes your caps lock off.


Was it on? I hadn't noticed!
vikas mhatre
Greenhorn

Joined: Apr 24, 2010
Posts: 26
I am still not able to encode image. Anyone please help me?
Thanks in advance
vikas mhatre
Greenhorn

Joined: Apr 24, 2010
Posts: 26
hello guys,
using following code I can encode the some part of image (i.e upto size of array) when i decode file zzz.txt then some part of image I can see But I am not able to encode whole image, my code is like this Please help me.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
  • Again: http://faq.javaranch.com/java/ReadDoesntDoWhatYouThinkItDoes.
  • readLlmit is never assigned a value
  • Just to be sure: tt.jpg is smaller than 1024 byte, yes?
  • Writing the complete array (of 1024 bytes) when it's probably only partially filled is not a good idea.
  • You should never ignore exceptions during I/O. How will know if there is a problem? At least write out an error message.
  • vikas mhatre
    Greenhorn

    Joined: Apr 24, 2010
    Posts: 26
    Just to be sure: tt.jpg is smaller than 1024 byte, yes?


    But I want to encode image larger size (upto 3mb) What I want is- read 1kb of image encode it write to file encoded image then again read another 1kb image and encode it .
    Lester Burnham
    Rancher

    Joined: Oct 14, 2008
    Posts: 1337
    The array is 1024 bytes big, so it should be obvious that it can't hold more than 1024 bytes of data. If you have larger images then you need to do the reading in a loop, as James Sabre pointed out on August 19.
    Peter Taucher
    Ranch Hand

    Joined: Nov 18, 2006
    Posts: 174
    I'm providing 'some' of the source code:



    Fill in the 'read' and 'write' method calls to the streams and the code's working...


    Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
    -- Johann Nepomuk Nestroy
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19543
        
      16

    Peter Taucher wrote:

    That should be Especially with network connections, it's very possible to read 0 bytes at a specific moment in time. There may still be data coming though. The end of stream (return value of -1 for all available read methods) is the only guaranteed indication that you can stop reading.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Peter Taucher
    Ranch Hand

    Joined: Nov 18, 2006
    Posts: 174
    OK, of course you're right.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to encode image in parts?
     
    Similar Threads
    creating a link
    Thumbnail
    read and write file in parts
    Getting Different File Sizes from the server.
    Reading image and convert it to Base64 format