fri*_*itz 16 camera android android-intent android-camera-intent
我想在ImageView中使用显示从相机拍摄的图像
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
到目前为止,此工作正常,但在用户使用所选的相机应用程序拍摄照片后,会出现一个对话框(可能来自应用程序),询问是否保存或删除拍摄的照片(至少在Android 2.3和4.2上使用默认的相机应用程序).
我想跳过这个额外的对话框,直接在ImageView中显示图像(当调用onActivityResult时),因为这意味着用户需要额外的交互步骤,这是不必要的,因为他可以保存或删除照片.我的应用.
这可以使用简单的ACTION_IMAGE_CAPTURE Intent,还是需要更复杂的东西,比如Camera Preview和SurfaceView?
Anu*_*_07 16
您可以使用它SurfaceView来捕获图像
package com.camera;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Camera_capture extends Activity implements SurfaceHolder.Callback {
private Camera mCamera;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private Button capture_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera_layout);
    capture_image = (Button) findViewById(R.id.capture_image);
    capture_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            capture();
        }
    });
    surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(Camera_capture.this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    try {
        mCamera = Camera.open();
        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private void capture() {
    mCamera.takePicture(null, null, null, new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            Toast.makeText(getApplicationContext(), "Picture Taken",
                    Toast.LENGTH_SHORT).show();
            Intent intent = new Intent();
            intent.putExtra("image_arr", data);
            setResult(RESULT_OK, intent);
            camera.stopPreview();
            if (camera != null) {
                camera.release();
                mCamera = null;
            }
            finish();
        }
    });
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    Log.e("Surface Changed", "format   ==   " + format + ",   width  ===  "
            + width + ", height   ===    " + height);
    try {
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    Log.e("Surface Created", "");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    Log.e("Surface Destroyed", "");
}
@Override
protected void onPause() {
    super.onPause();
    if (mCamera != null) {
        mCamera.stopPreview();
        mCamera.release();
    }
}
}
布局文件将是
<?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="vertical" >
<SurfaceView
    android:id="@+id/surfaceview"
    android:layout_width="fill_parent"
    android:layout_weight="100"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/capture_image"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Capture" />
</LinearLayout>
启动此Camera_capture与活动startActivityForResult,并onActivityResult可以得到图像byte阵列
byte[] image = data.getExtras().getByteArray("image_arr");
data收到的数据在哪里.
将byte数组解码为Bitmap使用
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0,
                    image.length);
现在设定这个 Bitmap
编辑
由于返回时存在一些问题byte[],byte[]应将其保存在文件中,并将文件的路径发送到之前的Activity文件,以便可以读取该文件.
在onPictureTaken(),只需添加
String PATH = "Any path to store a file";
try {
    FileOutputStream fos=new FileOutputStream(PATH);
    fos.write(data);
    fos.close();
  }
  catch (java.io.IOException e) {
  }
代替:
intent.putExtra("image_arr", data);
写
intent.putExtra("image_path", PATH);
并在之前Activity的路径中接收此路径onActivityResult:
String imagePath = data.getExtras().getString("image_path");
小智 6
使用"android.intent.extra.quickCapture"
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("android.intent.extra.quickCapture",true);
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}