Home > Mobile >  Take photo and convert to base64 in Android
Take photo and convert to base64 in Android

Time:11-23

I have two buttons that take photos and I want to convert the photos to base64 to send them to my database, but doing it with bitmap lowers a lot the quality of the photo, how can I get the photo in full size and convert it to base64 without bitmap?, or is there any way to get the photo in good quality with bitmap.

You can save the photo in a temporary directory to get the full size photo.

ImageView imageV, imageV2;
Button btn, btn2;
static final int IMAGE_REQUEST = 1;
static final int IMAGE_REQUEST2 = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageV = findViewById(R.id.image_view);
    imageV2 = findViewById(R.id.image_view_2);
    btn = findViewById(R.id.button);
    btn2 = findViewById(R.id.button2);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           Intent takePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
           if (camara.resolveActivity(getPackageManager()) != null) {
              startActivityForResult(takePhoto, IMAGE_REQUEST);
           }
        }
    });

    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           Intent takePhoto2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
           if (camara.resolveActivity(getPackageManager()) != null) {
              startActivityForResult(takePhoto2, IMAGE_REQUEST);
           }
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    
    if (requestCode == IMAGE_REQUEST) {
        if (resultCode == Activity.RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap photo = (Bitmap) extras.get("data");
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            byte[] image = outputStream.toByteArray();
            String imageB64 = Base64.encodeToString(image, Base64.DEFAULT);
        }
    }
    
    if (requestCode == IMAGE_REQUEST2) {
        if (resultCode == Activity.RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap photo2 = (Bitmap) extras.get("data");
            ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
            photoStatement.compress(Bitmap.CompressFormat.JPEG, 100, outputStream2);
            byte[] imagen2 = outputStream2.toByteArray();
            String image2B64 = Base64.encodeToString(imagen2, Base64.DEFAULT);
        }
    }
    
}

CodePudding user response:

I don't want to display or save the photo, I just want to take it and convert it to base64 to send it to my database.

Sorry, but if you want more than that thumbnail you get now you should let the used Camera app save the picture to file. (Do not mess around with a bitmap for that). Then put the file as blob in the database. If you put it base64 encoded in the database you need 30% more bytes.

CodePudding user response:

When you try to get the image from data intent extra, it returns the thumbnail not the real/full-size image captured.

To be able to access the real image captured by the Camera, you should use FileProvider, and get the file by its URI.

If you follow the official documentation, you will find it very easy and most efficient.

  • Related