如何拍摄而不保存/显示图像

时间:2014-03-13 02:41:51

标签: android eclipse image android-camera

我正在使用eclipse为Android开发自定义相机应用程序。我已经弄清楚如何显示相机的预览,但我无法弄清楚如何在不保存图像的情况下拍照。拍摄照片时,我不想保存图像或将其显示在屏幕上,我只是希望它保存为我可以在应用程序中使用的数据类型,并在拍摄另一张照片时覆盖。

修改

public class MainActivity extends Activity {

private Camera mCamera;
private CameraPreview mCameraPreview;
private Bitmap picture;
private static Uri mImageCaptureUri;
private static Bitmap photo = null;
private byte[] image;
private int PICK_FROM_CAMERA = 1;

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

@Override
protected void onCreate(Bundle savedInstanceState) {

    releaseCameraAndPreview();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mCamera = getCameraInstance();
    mCameraPreview = new CameraPreview(this, mCamera);

    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);

    preview.addView(mCameraPreview);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (resultCode != Activity.RESULT_OK) return;

    else{

        Bundle extras = data.getExtras();

        if (extras != null) {                        
            photo = extras.getParcelable("data");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.PNG, 100, bos); 
            image = bos.toByteArray();
        }

        File f = new File(mImageCaptureUri.getPath());            

        if (f.exists()){
            f.delete();
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

//creates camera class for functions
private Camera getCameraInstance() {

    Camera camera = null;

    try {
        camera = Camera.open();
    } catch (Exception e) {
        Log.v("Schweigen", "camera will not open");
    }
    return camera;
}

//releases camera on completion
private void releaseCameraAndPreview() {
    if (mCamera != null) {
        mCamera.release();
        mCamera = null;
    }
}

ShutterCallback shutterCallback = new ShutterCallback() {
    public void onShutter() {
        Log.v("Schweigen", "onShutter");
    }
};

/** Handles data for raw picture */
 PictureCallback rawCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.v("Schweigen", "onPictureTaken");
    }
};

/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
    //picture = BitmapFactory.decodeByteArray(data , 0, data.length);

    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,   mImageCaptureUri);    

        try {
            intent.putExtra("return-data", true);
            startActivityForResult(intent, PICK_FROM_CAMERA);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }
};

}

2 个答案:

答案 0 :(得分:2)

从图片中制作Uri

Intent cameraIntent = new    Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        Uri uriSavedImage=Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString()+"/ImagesFolder/image.jpg"));
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

修改:之后您可以将其存储在byte[]中。

正如我告诉过你的,你必须从相机中取出Intent并将其设为Uri

private static Uri mImageCaptureUri;

private static Bitmap photo = null;

private byte[] image;

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                                                               "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

try {
    intent.putExtra("return-data", true);
    startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
    e.printStackTrace();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (resultCode != Activity.RESULT_OK) return;

    else{

                Bundle extras = data.getExtras();

                if (extras != null) {                        
                    photo = extras.getParcelable("data");
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.PNG, 100, bos); 
                    image = bos.toByteArray();
                }

                File f = new File(mImageCaptureUri.getPath());            

                if (f.exists()) f.delete();

            }


  }

答案 1 :(得分:1)

直接拍照的API是here