简体   繁体   中英

Take a photo with Camera but don't save it as a file, only show it

In my Android project I want to take a photo with Camera but don't save it as a file, only show it on a ImageView , as a drawable or other.

In my project I can see the camera, like the code below.

I tryied a lot of ways, but I cant take the photo without save it SDCard.

ShowCamera.java

 public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder holdMe;
    private Camera theCamera;

    public ShowCamera(Context context, Camera camera) {
        super(context);
        theCamera = camera;
        holdMe = getHolder();
        holdMe.addCallback(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            theCamera.setPreviewDisplay(holder);
            theCamera.startPreview();
        } catch (IOException e) {
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
    }
}

MainActivity.java

public class MainActivity extends Activity  {

       private Camera cameraObject;
       private ShowCamera showCamera;

       public static Camera isCameraAvailiable(){
          Camera object = null;
          try {
             object = Camera.open(); 
          }
          catch (Exception e){
          }
          return object; 
       }



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

          cameraObject = isCameraAvailiable();
          showCamera = new ShowCamera(this, cameraObject);
          final FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
          preview.addView(showCamera);
    }

Thanks!

First of all, do not forget to put this in your AndroidManifest.xml: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

So, you can follow this example: http://hmkcode.com/android-camera-taking-photos-camera/

You need to include this line inside ur button click routine:

mCamera.takePicture(null, null, mPicture);

And then create a routine:

private PictureCallback mPicture = new PictureCallback(){ //+ a lot of things...}

The code below should be inside the PictureCallBack routine

public void onPictureTaken(byte[] data, Camera camera){
   File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
      try{
         FileOutputStream fos = new FileOutputStream(pictureFile);
         fos.write(data);
         fos.close();
      }
      catch(FileNotFoundException e){}
      catch(IOException e){}
      File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                                      Environment.DIRECTORY_PICTURES), "MyCameraApp");
}

And try to find the function:

private static File getOutputMediaFile(int type){}

That also should be included!

So you need an image taken from camera and show it on a ImageView , not to save it?

Create a picture callback to receive data from camera and decode it to Bitmap:

private PictureCallback mPicture = new PictureCallback() {

    @Override
    public void onPictureTaken(final byte[] data, Camera c) {
        Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
        yourImageView.setImageBitmap(bm);
    }
};

Call camera capture (when capture button clicked):

try {
    mCamera.takePicture(null, null, mPicture);
} catch (Exception e) {
    // take picture failed
    e.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM