Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture and save an image using custom camera in Android?

How do I capture an image in custom camera and then save that image in android?

like image 989
Vishnu Avatar asked Jun 06 '12 11:06

Vishnu


People also ask

How do you get an image from a camera or a gallery and save it in Android?

Run the application on an Android phone. Selecting "Take photo" will open your camera. Finally, the image clicked will be displayed in the ImageView. Selecting "Choose from Gallery" will open your gallery (note that the image captured earlier has been added to the phone gallery).


2 Answers

please see below answer.

Custom_CameraActivity.java

public class Custom_CameraActivity extends Activity {     private Camera mCamera;     private CameraPreview mCameraPreview;      /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         mCamera = getCameraInstance();         mCameraPreview = new CameraPreview(this, mCamera);         FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);         preview.addView(mCameraPreview);          Button captureButton = (Button) findViewById(R.id.button_capture);         captureButton.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 mCamera.takePicture(null, null, mPicture);             }         });     }      /**      * Helper method to access the camera returns null if it cannot get the      * camera or does not exist      *       * @return      */     private Camera getCameraInstance() {         Camera camera = null;         try {             camera = Camera.open();         } catch (Exception e) {             // cannot get camera or does not exist         }         return camera;     }      PictureCallback mPicture = new PictureCallback() {         @Override         public void onPictureTaken(byte[] data, Camera camera) {             File pictureFile = getOutputMediaFile();             if (pictureFile == null) {                 return;             }             try {                 FileOutputStream fos = new FileOutputStream(pictureFile);                 fos.write(data);                 fos.close();             } catch (FileNotFoundException e) {              } catch (IOException e) {             }         }     };      private static File getOutputMediaFile() {         File mediaStorageDir = new File(                 Environment                         .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),                 "MyCameraApp");         if (!mediaStorageDir.exists()) {             if (!mediaStorageDir.mkdirs()) {                 Log.d("MyCameraApp", "failed to create directory");                 return null;             }         }         // Create a media file name         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")                 .format(new Date());         File mediaFile;         mediaFile = new File(mediaStorageDir.getPath() + File.separator                 + "IMG_" + timeStamp + ".jpg");          return mediaFile;     } } 

CameraPreview.java

public class CameraPreview extends SurfaceView implements         SurfaceHolder.Callback {     private SurfaceHolder mSurfaceHolder;     private Camera mCamera;      // Constructor that obtains context and camera     @SuppressWarnings("deprecation")     public CameraPreview(Context context, Camera camera) {         super(context);         this.mCamera = camera;         this.mSurfaceHolder = this.getHolder();         this.mSurfaceHolder.addCallback(this);         this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);     }      @Override     public void surfaceCreated(SurfaceHolder surfaceHolder) {         try {             mCamera.setPreviewDisplay(surfaceHolder);             mCamera.startPreview();         } catch (IOException e) {             // left blank for now         }     }      @Override     public void surfaceDestroyed(SurfaceHolder surfaceHolder) {         mCamera.stopPreview();         mCamera.release();     }      @Override     public void surfaceChanged(SurfaceHolder surfaceHolder, int format,             int width, int height) {         // start preview with new settings         try {             mCamera.setPreviewDisplay(surfaceHolder);             mCamera.startPreview();         } catch (Exception e) {             // intentionally left blank for a test         }     } } 

main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="horizontal" >      <FrameLayout         android:id="@+id/camera_preview"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:layout_weight="1" />      <Button         android:id="@+id/button_capture"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:text="Capture" />  </LinearLayout> 

Add Below Lines to your androidmanifest.xml file

<uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
like image 91
Dipak Keshariya Avatar answered Oct 05 '22 12:10

Dipak Keshariya


Following Snippet will help you

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="de.vogella.cameara.api"     android:versionCode="1"     android:versionName="1.0" >      <uses-sdk android:minSdkVersion="15" />     <uses-permission android:name="android.permission.CAMERA"/>     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>      <application         android:icon="@drawable/ic_launcher"         android:label="@string/app_name" >         <activity             android:name="de.vogella.camera.api.MakePhotoActivity"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>     </application>  </manifest> 

main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent" >      <Button         android:id="@+id/captureFront"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerInParent="true"         android:onClick="onClick"         android:text="Make Photo" />  </RelativeLayout> 

PhotoHandler.java

package org.sample;  import java.io.File; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.Date;  import android.content.Context; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.os.Environment; import android.util.Log; import android.widget.Toast;  public class PhotoHandler implements PictureCallback {      private final Context context;      public PhotoHandler(Context context) {         this.context = context;     }      @Override     public void onPictureTaken(byte[] data, Camera camera) {          File pictureFileDir = getDir();          if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {              Log.d(Constants.DEBUG_TAG, "Can't create directory to save image.");             Toast.makeText(context, "Can't create directory to save image.",                     Toast.LENGTH_LONG).show();             return;          }          SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");         String date = dateFormat.format(new Date());         String photoFile = "Picture_" + date + ".jpg";          String filename = pictureFileDir.getPath() + File.separator + photoFile;          File pictureFile = new File(filename);          try {             FileOutputStream fos = new FileOutputStream(pictureFile);             fos.write(data);             fos.close();             Toast.makeText(context, "New Image saved:" + photoFile,                     Toast.LENGTH_LONG).show();         } catch (Exception error) {             Log.d(Constants.DEBUG_TAG, "File" + filename + "not saved: "                     + error.getMessage());             Toast.makeText(context, "Image could not be saved.",                     Toast.LENGTH_LONG).show();         }     }      private File getDir() {         File sdDir = Environment           .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);         return new File(sdDir, "CameraAPIDemo");     } } 

MakePhotoActivity.java

package org.sample;  import android.app.Activity; import android.content.pm.PackageManager; import android.hardware.Camera; import android.hardware.Camera.CameraInfo; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Toast; import de.vogella.cameara.api.R;  public class MakePhotoActivity extends Activity {     private final static String DEBUG_TAG = "MakePhotoActivity";     private Camera camera;     private int cameraId = 0;      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);          // do we have a camera?         if (!getPackageManager()                 .hasSystemFeature(PackageManager.FEATURE_CAMERA)) {             Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)                     .show();         } else {             cameraId = findFrontFacingCamera();             camera = Camera.open(cameraId);             if (cameraId < 0) {                 Toast.makeText(this, "No front facing camera found.",                         Toast.LENGTH_LONG).show();             }         }     }      public void onClick(View view) {         camera.takePicture(null, null,                 new PhotoHandler(getApplicationContext()));     }      private int findFrontFacingCamera() {         int cameraId = -1;         // Search for the front facing camera         int numberOfCameras = Camera.getNumberOfCameras();         for (int i = 0; i < numberOfCameras; i++) {             CameraInfo info = new CameraInfo();             Camera.getCameraInfo(i, info);             if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {                 Log.d(DEBUG_TAG, "Camera found");                 cameraId = i;                 break;             }         }         return cameraId;     }      @Override     protected void onPause() {         if (camera != null) {             camera.release();             camera = null;         }         super.onPause();     }  } 
like image 36
Vipul Avatar answered Oct 05 '22 11:10

Vipul