如何用自定义按钮拍照

时间:2018-04-28 09:44:27

标签: android

我已经在我的应用程序中打开了相机,我想在按钮点击上拍照。我创建了两个活动:

public class MainActivity extends AppCompatActivity {
private Camera mCamera = null;
private CameraView mCameraView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try{
        mCamera = Camera.open();
    } catch (Exception e){
        Log.d("ERROR", "Failed to get camera: " + e.getMessage());
    }

    if(mCamera != null) {
        mCameraView = new CameraView(this, mCamera);
        FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
        camera_view.addView(mCameraView);
    }

    ImageButton imgClose = (ImageButton)findViewById(R.id.imgClose);
    imgClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
}

}

打开相机并开始预览的CameraActivity:

public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraView(Context context, Camera camera){
    super(context);
    mCamera = camera;
    mCamera.setDisplayOrientation(0);
    //get the holder and set this class as the callback, so we can get camera data here
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    try{
        //when the surface is created, we can set the camera to draw images in this surfaceholder
        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.d("ERROR", "Camera error on surfaceCreated " + e.getMessage());
    }
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
    //before changing the application orientation, you need to stop the preview, rotate and then start it again
    if(mHolder.getSurface() == null)//check if the surface is ready to receive camera data
        return;

    try{
        mCamera.stopPreview();
    } catch (Exception e){
        //this will happen when you are trying the camera if it's not running
    }

    //now, recreate the camera preview
    try{
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.d("ERROR", "Camera error on surfaceChanged " + e.getMessage());
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    //our app has only one screen, so we'll destroy the camera in the surface
    //if you are unsing with more screens, please move this code your activity
    mCamera.stopPreview();
    mCamera.release();
}}

我想在OnClick中的MainActivity方法上拍照。 CameraActivity打开相机并开始预览,同时按钮(十字)即可拍摄照片。我是Android应用程序开发的新手,不知道如何拍照,然后将其存储在位图中并显示在ImageView中。我将进一步处理该位图。但是现在我想从相机拍照并将其保存在位图中并显示在ImageView

1 个答案:

答案 0 :(得分:0)

如果您的唯一目标是拍照并保存,那么我认为实施您自己的相机并使用SurfaceView毫无意义,只需使用设备上已有的相机应用,它就是更容易。我回答了类似的问题并提供了代码,你可以找到它here。但在你查阅之前,首先看一下Android documentation,它很好地解释了如何通过将工作委托给另一个相机应用来捕获图像。

如果您仍想要实现自己的相机,则会有一个名为takePicture的方法,该方法称为mCamera.takePicture(null, null, mPicture),其中mPicturePictureCallback的实例}。只需打开documentation并查找“捕捉图片”部分,您就会在那里找到一个示例。