Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 modify code to take pictures in Base64 instead of regular files.

public class CameraDemoActivity extends Activity {     int TAKE_PHOTO_CODE = 0;     public static int count = 0;      @Override     public void onCreate(Bundle savedInstanceState)     {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);          final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";         File newdir = new File(dir);         newdir.mkdirs();          Button capture = (Button) findViewById(R.id.btnCapture);         capture.setOnClickListener(new View.OnClickListener() {             public void onClick(View v) {                 count++;                 String file = dir+count+".jpg";                 File newfile = new File(file);                 try {                     newfile.createNewFile();                 }                 catch (IOException e)                 {                 }                  Uri outputFileUri = Uri.fromFile(newfile);                  Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);                  startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);             }         });     }      @Override     protected void onActivityResult(int requestCode, int resultCode, Intent data) {         super.onActivityResult(requestCode, resultCode, data);          if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {             Log.d("CameraDemo", "Pic saved");         }     } } 

I try to use code below to convert an image to Base64.

public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) {     ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();     image.compress(compressFormat, quality, byteArrayOS);     return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT); } 

Above described should be a much more direct and easier way than saving image and after that looking for image to encode it.

like image 684
plaidshirt Avatar asked Mar 23 '16 21:03

plaidshirt


People also ask

Can we convert image to Base64?

Convert Images to Base64Just select your JPG, PNG, GIF, Webp, or BMP picture or drag & drop it in the form below, press the Convert to Base64 button, and you'll get a base-64 string of the image. Press a button – get base64. No ads, nonsense, or garbage. Drag and drop your image here!

What is Base64 picture?

Base64 encoding is a way to encode binary data in ASCII text. It's primarily used to store or transfer images, audio files, and other media online. It is also often used when there are limitations on the characters that can be used in a filename for various reasons.


2 Answers

Try this:
ImageUri to Bitmap:

 @Override         protected void onActivityResult(int requestCode, int resultCode, Intent data) {             super.onActivityResult(requestCode, resultCode, data);              if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {                 final Uri imageUri = data.getData();                 final InputStream imageStream = getContentResolver().openInputStream(imageUri);                 final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);                 String encodedImage = encodeImage(selectedImage);             }         } 

Encode Bitmap in base64

   private String encodeImage(Bitmap bm)     {         ByteArrayOutputStream baos = new ByteArrayOutputStream();         bm.compress(Bitmap.CompressFormat.JPEG,100,baos);         byte[] b = baos.toByteArray();         String encImage = Base64.encodeToString(b, Base64.DEFAULT);          return encImage;     } 

Encode from FilePath to base64

 private String encodeImage(String path)     {         File imagefile = new File(path);         FileInputStream fis = null;         try{             fis = new FileInputStream(imagefile);         }catch(FileNotFoundException e){             e.printStackTrace();         }         Bitmap bm = BitmapFactory.decodeStream(fis);         ByteArrayOutputStream baos = new ByteArrayOutputStream();         bm.compress(Bitmap.CompressFormat.JPEG,100,baos);         byte[] b = baos.toByteArray();         String encImage = Base64.encodeToString(b, Base64.DEFAULT);         //Base64.de         return encImage;      } 

output: enter image description here

like image 165
Fabio Venturi Pastor Avatar answered Oct 19 '22 19:10

Fabio Venturi Pastor


I've wrote my code like this :

public class MainActivity extends AppCompatActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          Camera mCamera = 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.PictureCallback mPicture = new Camera.PictureCallback() {          @Override         public void onPictureTaken(byte[] data, Camera camera) {             System.out.println("***************");             System.out.println(Base64.encodeToString(data, Base64.DEFAULT));             System.out.println("***************");         }     }; } 

It works perfectly...

like image 21
LetsGoBrandon Avatar answered Oct 19 '22 20:10

LetsGoBrandon