• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to encode image in parts?

 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anybody know how to do this?
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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/ .
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really don't get how to use it.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I think I got it. Thanks James, Now I will try it.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, also want to this but can't succeed anybody please help me.
 
vikas mhatre
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Pramod can you help me? Hello anyone is there?
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vikas mhatre wrote:
Now I had modified earlier code as



I don't think you have read this thread at all.
 
vikas mhatre
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then what is wrong there? will you please explain me?
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello James are you there?
 
Marshal
Posts: 80288
433
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vikas mhatre wrote:Hello James are you there?

Read this: Patience Is A Virtue.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here in am reading and writing using input and outputStream but how to read next part of image
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 80288
433
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James Sabre, please takes your caps lock off.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Was it on? I hadn't noticed!
 
vikas mhatre
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am still not able to encode image. Anyone please help me?
Thanks in advance
 
vikas mhatre
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • 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
    Posts: 26
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 1337
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Ranch Hand
    Posts: 174
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'm providing 'some' of the source code:



    Fill in the 'read' and 'write' method calls to the streams and the code's working...
     
    Sheriff
    Posts: 22821
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    Peter Taucher
    Ranch Hand
    Posts: 174
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    OK, of course you're right.
     
    So I left, I came home, and I ate some pie. And then I read this tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic