Android Studio,捕获 FrameLayout 视图,当相机处于 StopPreview() 时

Android Studio, Capture FrameLayout View, While Camera is StopPreview()

这是我的 CameraView.java(我从 http://blog.rhesoft.com/ 得到的)

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(90);
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    try{
        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) {
    if(mHolder.getSurface() == null)
        return;
    try{
        mCamera.stopPreview();
    } catch (Exception e){

    }

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

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    mCamera.stopPreview();
    mCamera.release();
}}

这是我的 MainActivity.java

public class MainActivity extends AppCompatActivity {

private Camera mCamera = null;
private CameraView mCameraView = null;
private FrameLayout camera_view;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try{
        mCamera = Camera.open();
        //you can use open(int) to use different cameras
    } catch (Exception e){
        Log.d("ERROR", "Failed to get camera: " + e.getMessage());
    }

    if(mCamera != null) {
        mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data
        camera_view = (FrameLayout)findViewById(R.id.camera_view);
        camera_view.addView(mCameraView);//add the SurfaceView to the layout
    }

    //btn to close the application
    final ImageButton imgClose = (ImageButton)findViewById(R.id.imgClose);
    final ImageButton capImg = (ImageButton) findViewById(R.id.imgCapture);
    imgClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            imgClose.setVisibility(View.INVISIBLE);
            capImg.setVisibility(View.VISIBLE);
            mCamera.startPreview();
        }
    });
    capImg.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            mCamera.stopPreview();
            imgClose.setVisibility(View.VISIBLE);
            capImg.setVisibility(View.INVISIBLE);
        }
    });
}

@Override
public void onBackPressed(){
    System.exit(0);
}}

这是我的 activity_main.xml.

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
    android:id="@+id/camera_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imgClose"
    android:layout_gravity="right|top"
    android:background="@android:drawable/ic_menu_close_clear_cancel"
    android:padding="20dp"
    android:visibility="invisible" />

<ImageButton
    android:layout_width="98dp"
    android:layout_height="98dp"
    android:id="@+id/imgCapture"
    android:layout_gravity="center_horizontal|bottom"
    android:background="@android:drawable/ic_menu_camera"
    android:padding="20dp"/>

我能否将此 FrameLayout 预览捕获为图像或使用该预览进行一些编程,例如删除红色?你能给我一些线索吗?

所以如果我没理解错的话,你是希望得到停止预览时显示的图像数据?如果你这样做,你可以使用 mCamera.takePicture() 方法。它需要 3 个参数,所有这些都是有用的回调。这是我最近做的给你看的东西。

        btn_Capture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           if (mCamera == null)
                return;
            mCamera.takePicture(null, null, mPicture);
    }
    });

这是我的按钮点击侦听器,它是一个浮动图像按钮(任何按钮都可以正常工作)。第三个参数是一个回调,returns 一个可以转换为位图的像素数组。

private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        mCamera.stopPreview();
        Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length);
        preview.removeView(mPreview);
        img_Captured.setImageBitmap(bMap);
    }
};

这是我在 takePicture() 方法中传递的回调。 byte[] 数据是您要获取的图像。如您所见,我将其转换为位图并在删除表面视图(保存相机预览)后将其显示到 ImageView。请注意,takePicture() 方法会自动停止预览,因此请不要在拍照前停止预览。你可以像我在回调中那样做。另外,如果您想再拍一张照片,可以重新开始预览。

希望对您有所帮助!!如果我遗漏了什么,请告诉我!顺便说一下,所有这些都记录在 Android 开发者网站上。

http://developer.android.com/training/camera/cameradirect.html#TaskTakePicture