Android Studio,捕获FrameLayout视图,而Camera是StopPreview()

时间:2016-01-16 07:19:41

标签: android-studio android-camera android-framelayout

这是我的def list = [[1, 2], [2, 4], [3, 6]] assert list.collect { a, b -> a + b } == [3, 6, 9, ] (我是从http://blog.rhesoft.com/获得的)

CameraView.java

这是我的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预览捕获为图像,还是使用该预览进行一些编程,如删除红色?你能给我一些线索吗?

1 个答案:

答案 0 :(得分:0)

因此,如果我理解正确,您希望获得停止预览时显示的图像数据?如果你这样你就可以使用mCamera.takePicture()方法。它需要3个参数,所有参数都是有用的回调。以下是我最近向您展示的内容。

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

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

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 []数据是您尝试获取的图像。正如您所看到的,我将其转换为位图并在移除surfaceview(它保存相机预览)后将其显示到ImageView。只需注意,takePicture()方法会自动停止预览,因此在拍摄照片之前不要停止预览。你可以在回调中做到这一点。此外,如果您想拍摄另一张照片,可以再次开始预览。

我希望这有帮助!!如果我遗漏了任何东西,请告诉我!顺便说一句,它都记录在Android开发者网站上。

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