本文介绍了如何捕获从定制CameraView图像中的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的捕捉图片要求部分的屏幕 的。

i need to capture image from required portion of the screen.

捕获映像的距离相机

在当时的其他屏幕内容,因为它是的。

这是怎么可能

推荐答案

尝试使用表面观作为的创建动态相机查看和设置在您需要的部分。

try to use Surface View for creating dynamic camera view and set in your required portion.

以下code试

变量设置类级别(全球)

Button btn_capture;
Camera camera1;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
public static boolean previewing = false;

随着code。在的onCreate()方式

getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = new SurfaceView(this);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
btn_capture = (Button) findViewById(R.id.button1);

surfaceView.setBackgroundResource(R.drawable.your_background_image);

if(!previewing){

        camera1 = Camera.open();
        if (camera1 != null){
            try {
                camera1.setDisplayOrientation(90);
                camera1.setPreviewDisplay(surfaceHolder);
                camera1.startPreview();
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

btn_capture.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            if(camera != null)
            {
                camera1.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG);

            }
        }
    });

随着code将后的onCreate()中的的。

ShutterCallback myShutterCallback = new ShutterCallback(){

    public void onShutter() {
        // TODO Auto-generated method stub
    }};

PictureCallback myPictureCallback_RAW = new PictureCallback(){

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
    }};

PictureCallback myPictureCallback_JPG = new PictureCallback(){

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
        Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);

        Bitmap correctBmp = Bitmap.createBitmap(bitmapPicture, 0, 0, bitmapPicture.getWidth(), bitmapPicture.getHeight(), null, true);

    }};

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
    if(previewing){
        camera1.stopPreview();
        previewing = false;
    }

    if (camera1 != null){
        try {
            camera1.setPreviewDisplay(surfaceHolder);
            camera1.startPreview();
            previewing = true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

        camera1.stopPreview();
        camera1.release();
        camera1 = null;
        previewing = false;

}

AndroidManifest.xml中提供的用户权限

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>

和也没有忘记(实现SurfaceHolder.Callback)的类。

这篇关于如何捕获从定制CameraView图像中的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:40