CoatedMoose / CustomViews

A set of views for use in android applications, including a signature capture view.
24 stars 6 forks source link

Converting the Bitmap to Base64 encodeded text #10

Closed mohitt closed 10 years ago

mohitt commented 10 years ago

Hi Andrew,

I tried converting the Bitmap being captured to Base64 Encoded string using the following code

ByteArrayOutputStream baos = new ByteArrayOutputStream();
signatureView.getImage().compress(Bitmap.CompressFormat.JPEG, 10, baos);
byte[] imageInByte=baos.toByteArray();
String imgInString = Base64.encodeToString(imageInByte,Base64.NO_WRAP);
Log.d(TAg,imgInString);

But, some how it only shows a part of the image not the complete image, but when I write it to Bitmap as per the demo and read the file again, try printing it on log, it is still has partial image, but if I write it to a text file. and get the content of the text file using DDMS. That seems to have the complete content.

Following is the demo code

        Bitmap image = signatureView.getImage();
        File sd = Environment.getExternalStorageDirectory();
        File fichero = new File(sd, "signature.jpg");
        Log.d(TAg, sd.getAbsolutePath());
        try {
            if (sd.canWrite()) {
                fichero.createNewFile();
                OutputStream os = new FileOutputStream(fichero);
                image.compress(Bitmap.CompressFormat.JPEG, 10, os);
                os.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (sd.canRead()) {
            if (fichero.canRead()) {
                Bitmap img = BitmapFactory.decodeFile(fichero.getAbsolutePath());
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                img.compress(Bitmap.CompressFormat.JPEG, 10, baos);
                byte[] imageInByte = baos.toByteArray();
                String imgInString = Base64.encodeToString(imageInByte, Base64.NO_WRAP);
                Log.d(TAg, imgInString);
                File txtFile = new File(sd, "textEncoded.txt");
                if (txtFile.exists())
                    txtFile.delete();
                try {
                    txtFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                    //BufferedWriter for performance, true to set append to file flag
                    BufferedWriter buf = new BufferedWriter(new FileWriter(txtFile, true));
                    buf.append(imgInString);
                    buf.newLine();
                    buf.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }

Any Help is greatly appreciated :) , thanks a ton in advance.

Regards Mohit

CoatedMoose commented 10 years ago

Hey, sorry for taking so long to respond to this issue. You created the issue when I was crazy busy at work, and then I didn't remember to come back to this.

Have you had any luck with this issue?

If I understand you correctly, you are trying to copy the string provided by logcat out to a file. LogCat has a cap on the line length of what can be written out (a few hundred characters I think, but still easily fewer characters than bytes of image). Would this explain the behaviour you are seeing?

Devang-Targetint commented 10 years ago

Hi Andrew,

Thanks for reply. And about the LogCat behaviour, you are right ...

CoatedMoose commented 10 years ago

It sounds like this is solved. Feel free to reopen if not.

Devang-Targetint commented 10 years ago

Sure .... Thanks