Skip to content Skip to sidebar Skip to footer

Take Picture And Convert To Base64

I use code below to make a picture with camera. Instead of saving I would like to encode it to Base64 and after that pass it to another API as an input. I can't see method, how to

Solution 1:

Try this: ImageUri to Bitmap:

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
                finalUriimageUri= data.getData();
                finalInputStreamimageStream= getContentResolver().openInputStream(imageUri);
                finalBitmapselectedImage= BitmapFactory.decodeStream(imageStream);
                StringencodedImage= encodeImage(selectedImage);
            }
        }

Encode Bitmap in base64

private String encodeImage(Bitmap bm)
    {
        ByteArrayOutputStreambaos=newByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
        byte[] b = baos.toByteArray();
        StringencImage= Base64.encodeToString(b, Base64.DEFAULT);

        return encImage;
    }

Encode from FilePath to base64

private String encodeImage(String path)
    {
        Fileimagefile=newFile(path);
        FileInputStreamfis=null;
        try{
            fis = newFileInputStream(imagefile);
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }
        Bitmapbm= BitmapFactory.decodeStream(fis);
        ByteArrayOutputStreambaos=newByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
        byte[] b = baos.toByteArray();
        StringencImage= Base64.encodeToString(b, Base64.DEFAULT);
        //Base64.dereturn encImage;

    }

output: enter image description here

Solution 2:

I've wrote my code like this :

publicclassMainActivityextendsAppCompatActivity {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CameramCamera= Camera.open();
        mCamera.startPreview();// I don't know why I added that, // but without it doesn't work... :D

        mCamera.takePicture(null, null, mPicture);
    }

    private Camera.PictureCallbackmPicture=newCamera.PictureCallback() {

        @OverridepublicvoidonPictureTaken(byte[] data, Camera camera) {
            System.out.println("***************");
            System.out.println(Base64.encodeToString(data, Base64.DEFAULT));
            System.out.println("***************");
        }
    };
}

It works perfectly...

Solution 3:

Just for converting from bitmap to base64 string in kotlin I use:

privatefunencodeImage(bm: Bitmap): String? {
        val baos = ByteArrayOutputStream()
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos)
        val b = baos.toByteArray()
        return Base64.encodeToString(b, Base64.DEFAULT)
    }

Solution 4:

If you want your base64 String to follow the standard format, add this after getting your base64 method from any of the provided answers

Stringbase64=""; //Your encoded string
base64 = "data:image/"+getMimeType(context,profileUri)+";base64,"+base64;

The method to get imageExtension is

publicstaticStringgetMimeType(Context context, Uri uri) {
        String extension;
        if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
            //If scheme is a content
            final MimeTypeMap mime = MimeTypeMap.getSingleton();
            extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
        } else {
            //If scheme is a File//This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters.
            extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(newFile(uri.getPath())).toString());
        }
        return extension;
    }

Post a Comment for "Take Picture And Convert To Base64"