[android-developers] Re: Problem in encoding image

2010-08-30 Thread pramod.deore
Someone knows how to encode large image in Base64 format. Because I
had tried using creating byte array as large as image size but if
image it encode image if size of image is upto 800kb, if image is
greater than 800kb then it throws runtime exception as
OutofMemoryException. So I had decided use 1kb of byte array, in that
I encode 1kb of image write to a file after that again encode 1kb of
image and write to a file .  The code is like this

public void encodeImage()
{
try
{
out = new PrintWriter(c:/program1/zzz.txt);
InputStream inputStream =  new FileInputStream
(c:/program1/
tt.jpg);
OutputStream outputStream = new
FileOutputStream(c:/program1/
zzz.txt);

byte[] b = new byte[1024];

while((read= inputStream.read(b))0)
{
System.out.println (Inside while);
inputStream.mark(b.length);
String encoded = Base64.encode(b);
//System.out.println (encoded);
out.print(encoded);
out.flush();

//bas.write(b);
}

out.close();

}

catch (Exception e)
{
System.out.println (e);
}

}

But when I decode that image here 
http://www.opinionatedgeek.com/dotnet/tools/base64decode/
then it says that he input is not a valid Base-64 string.

Please help me to solve this issue.

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Re: Problem in encoding image

2010-08-30 Thread Kostya Vasilyev

 Pramod,

Base64 encoding has end-of-data markers.

http://en.wikipedia.org/wiki/Base64

Your code encodes each 1024-byte chunk of input image data into its own 
Base64 string. Each of those has its own end markers.


Because of this, the receiving side doesn't see one piece of data 
encoded with Base64, rather, it sees multiple pieces of data, which I 
guess is not what it expects.


There are several ways to fix this:

1 - Write your own code to encode partial data pieces, flushing the 
output at the very end. Base64 encoding is not difficult.


2 - Use a ready-made class, like this one:

http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64OutputStream.html

It's equivalent is available in Android starting with 2.2:

http://developer.android.com/reference/android/util/Base64OutputStream.html

3 - Change buffer size in your code to 1023 (a multiple of 3). This is a 
hack, but probably the easiest thing to do.


-- Kostya

30.08.2010 11:23, pramod.deore пишет:

Someone knows how to encode large image in Base64 format. Because I
had tried using creating byte array as large as image size but if
image it encode image if size of image is upto 800kb, if image is
greater than 800kb then it throws runtime exception as
OutofMemoryException. So I had decided use 1kb of byte array, in that
I encode 1kb of image write to a file after that again encode 1kb of
image and write to a file .  The code is like this

public void encodeImage()
 {
 try
 {
 out = new PrintWriter(c:/program1/zzz.txt);
 InputStream inputStream =  new FileInputStream
(c:/program1/
tt.jpg);
 OutputStream outputStream = new
FileOutputStream(c:/program1/
zzz.txt);

 byte[] b = new byte[1024];

 while((read= inputStream.read(b))0)
 {
 System.out.println (Inside while);
 inputStream.mark(b.length);
 String encoded = Base64.encode(b);
 //System.out.println (encoded);
 out.print(encoded);
 out.flush();

 //bas.write(b);
 }

 out.close();

 }

 catch (Exception e)
 {
 System.out.println (e);
 }

 }

But when I decode that image here 
http://www.opinionatedgeek.com/dotnet/tools/base64decode/
then it says that he input is not a valid Base-64 string.

Please help me to solve this issue.




--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Re: Problem in encoding image

2010-08-30 Thread pramod.deore
Hi, Kostya

I can't use second solutions because they are for API level8 and I am
developing this for APilevel4(1.6), But after changing byte array size
to 1023 it works perfectly fine, Thanks again Kostya I am working on
this from last 8 days and today I got the solution.

 But still I don't know how it works with 1023 and not with 1024?

On Aug 30, 1:04 pm, Kostya Vasilyev kmans...@gmail.com wrote:
   Pramod,

 Base64 encoding has end-of-data markers.

 http://en.wikipedia.org/wiki/Base64

 Your code encodes each 1024-byte chunk of input image data into its own
 Base64 string. Each of those has its own end markers.

 Because of this, the receiving side doesn't see one piece of data
 encoded with Base64, rather, it sees multiple pieces of data, which I
 guess is not what it expects.

 There are several ways to fix this:

 1 - Write your own code to encode partial data pieces, flushing the
 output at the very end. Base64 encoding is not difficult.

 2 - Use a ready-made class, like this one:

 http://commons.apache.org/codec/apidocs/org/apache/commons/codec/bina...

 It's equivalent is available in Android starting with 2.2:

 http://developer.android.com/reference/android/util/Base64OutputStrea...

 3 - Change buffer size in your code to 1023 (a multiple of 3). This is a
 hack, but probably the easiest thing to do.

 -- Kostya

 30.08.2010 11:23, pramod.deore пишет:



  Someone knows how to encode large image in Base64 format. Because I
  had tried using creating byte array as large as image size but if
  image it encode image if size of image is upto 800kb, if image is
  greater than 800kb then it throws runtime exception as
  OutofMemoryException. So I had decided use 1kb of byte array, in that
  I encode 1kb of image write to a file after that again encode 1kb of
  image and write to a file .  The code is like this

  public void encodeImage()
           {
                   try
                   {
                           out = new PrintWriter(c:/program1/zzz.txt);
                           InputStream inputStream =  new FileInputStream
  (c:/program1/
  tt.jpg);
                           OutputStream outputStream = new
  FileOutputStream(c:/program1/
  zzz.txt);

                           byte[] b = new byte[1024];

                           while((read= inputStream.read(b))0)
                           {
                                   System.out.println (Inside while);
                                   inputStream.mark(b.length);
                                   String encoded = Base64.encode(b);
                                   //System.out.println (encoded);
                                   out.print(encoded);
                                   out.flush();

                                   //bas.write(b);
                           }

                           out.close();

                   }

                   catch (Exception e)
                   {
                           System.out.println (e);
                   }

           }

  But when I decode that image 
  herehttp://www.opinionatedgeek.com/dotnet/tools/base64decode/
  then it says that he input is not a valid Base-64 string.

  Please help me to solve this issue.

 --
 Kostya Vasilyev -- WiFi Manager + pretty widget 
 --http://kmansoft.wordpress.com

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Re: Problem in encoding image

2010-08-30 Thread Kostya Vasilyev


It's magic, man. Pure magic.

:)

Base 64 encoding converts every 3 bytes of input data to 4 bytes of 
output data. If input data size is not a multiple of 3, output is padded 
with = characters, which also means end of data.


Your original buffer size, 1024, is not a multiple of 3, so each output 
chunk ended with end-of-data marker.


On the other hand, 1023 *is* a multiple of 3, so no end-of-data padding 
is generated until the very end of data.


-- Kostya

30.08.2010 13:13, pramod.deore пишет:

Hi, Kostya

I can't use second solutions because they are for API level8 and I am
developing this for APilevel4(1.6), But after changing byte array size
to 1023 it works perfectly fine, Thanks again Kostya I am working on
this from last 8 days and today I got the solution.

  But still I don't know how it works with 1023 and not with 1024?

On Aug 30, 1:04 pm, Kostya Vasilyevkmans...@gmail.com  wrote:

   Pramod,

Base64 encoding has end-of-data markers.

http://en.wikipedia.org/wiki/Base64

Your code encodes each 1024-byte chunk of input image data into its own
Base64 string. Each of those has its own end markers.

Because of this, the receiving side doesn't see one piece of data
encoded with Base64, rather, it sees multiple pieces of data, which I
guess is not what it expects.

There are several ways to fix this:

1 - Write your own code to encode partial data pieces, flushing the
output at the very end. Base64 encoding is not difficult.

2 - Use a ready-made class, like this one:

http://commons.apache.org/codec/apidocs/org/apache/commons/codec/bina...

It's equivalent is available in Android starting with 2.2:

http://developer.android.com/reference/android/util/Base64OutputStrea...

3 - Change buffer size in your code to 1023 (a multiple of 3). This is a
hack, but probably the easiest thing to do.

-- Kostya

30.08.2010 11:23, pramod.deore пишет:




Someone knows how to encode large image in Base64 format. Because I
had tried using creating byte array as large as image size but if
image it encode image if size of image is upto 800kb, if image is
greater than 800kb then it throws runtime exception as
OutofMemoryException. So I had decided use 1kb of byte array, in that
I encode 1kb of image write to a file after that again encode 1kb of
image and write to a file .  The code is like this
public void encodeImage()
  {
  try
  {
  out = new PrintWriter(c:/program1/zzz.txt);
  InputStream inputStream =  new FileInputStream
(c:/program1/
tt.jpg);
  OutputStream outputStream = new
FileOutputStream(c:/program1/
zzz.txt);
  byte[] b = new byte[1024];
  while((read= inputStream.read(b))0)
  {
  System.out.println (Inside while);
  inputStream.mark(b.length);
  String encoded = Base64.encode(b);
  //System.out.println (encoded);
  out.print(encoded);
  out.flush();
  //bas.write(b);
  }
  out.close();
  }
  catch (Exception e)
  {
  System.out.println (e);
  }
  }
But when I decode that image 
herehttp://www.opinionatedgeek.com/dotnet/tools/base64decode/
then it says that he input is not a valid Base-64 string.
Please help me to solve this issue.

--
Kostya Vasilyev -- WiFi Manager + pretty widget --http://kmansoft.wordpress.com



--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en