自定义广场摄像头 - Android电子 [英] Custom Square camera - Android

查看:145
本文介绍了自定义广场摄像头 - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义相机集成。

I want to integrate a custom camera.

我创建了一个相机preVIEW 并添加到布局

I've created a camera preview and added to the layout

CameraPreview mPreview = new CameraPreview(this);
LayoutParams previewLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mLayout.addView(mPreview, 0, previewLayoutParams);

但相机preVIEW不是方形。我想使相机preVIEW为方形,并在广场上播放视频,样品,像藤的应用程序。

but the camera preview is not square. I want to make the camera preview as a square and play the video in the square, for sample, like "vine" app.

如何实现这一目标?

推荐答案

尽量工具SurfaceHolder.Callback和你想这样使用自定义表面观:

Try to implements SurfaceHolder.Callback and use Custom Surface view as you want like this:

public class TakePicture extends Activity implements SurfaceHolder.Callback {
// a variable to store a reference to the Image View at the main.xml file
// private ImageView iv_image;
// a variable to store a reference to the Surface View at the main.xml file
private SurfaceView sv;

// a bitmap to display the captured image
private Bitmap bmp;
FileOutputStream fo;

// Camera variables
// a surface holder
private SurfaceHolder sHolder;
// a variable to control the camera
private Camera mCamera;
// the camera parameters
private Parameters parameters;
private String FLASH_MODE ;
private boolean isFrontCamRequest = false;

/** Called when the activity is first created. */
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera_surface_holder);

    // check if this device has a camera
    if (checkCameraHardware(getApplicationContext())) {
        // get the Image View at the main.xml file
        // iv_image = (ImageView) findViewById(R.id.imageView);

        // get the Surface View at the main.xml file
        Bundle extras = getIntent().getExtras();
        String flash_mode = extras.getString("FLASH");
        FLASH_MODE = flash_mode;
        boolean front_cam_req = extras.getBoolean("Front_Request");
        isFrontCamRequest = front_cam_req;

        sv = (SurfaceView) findViewById(R.id.camera_preview);

        // Get a surface
        sHolder = sv.getHolder();

        // add the callback interface methods defined below as the Surface
        // View
        // callbacks
        sHolder.addCallback(this);

        // tells Android that this surface will have its data constantly
        // replaced
        sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    } else {
        // display in long period of time
        Toast.makeText(getApplicationContext(),
                "Your Device dosen't have a Camera !", Toast.LENGTH_LONG)
                .show();
    }

}

/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(
            PackageManager.FEATURE_CAMERA)) {
        // this device has a camera
        return true;
    } else {
        // no camera on this device
        return false;
    }
}

public static Camera getCameraInstance() {
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    } catch (Exception e) {
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    // get camera parameters
    parameters = mCamera.getParameters();
    if (FLASH_MODE == null || FLASH_MODE.isEmpty())
    {
        FLASH_MODE = "auto";
    }
    parameters.setFlashMode(FLASH_MODE);

    // set camera parameters
    mCamera.setParameters(parameters);
    mCamera.startPreview();

    // sets what code should be executed after the picture is taken
    Camera.PictureCallback mCall = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // decode the data obtained by the camera into a Bitmap
            Log.d("ImageTakin", "Done");

            mCamera.stopPreview();
            // release the camera
            mCamera.release();
            Toast.makeText(getApplicationContext(),
                    "Your Picture has been taken !", Toast.LENGTH_LONG)
                    .show();

            finish();

        }
    };

    mCamera.takePicture(null, null, mCall);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, acquire the camera and tell it where
    // to draw the preview.

        mCamera = getCameraInstance();
        try {
            mCamera.setPreviewDisplay(holder);

        } catch (IOException exception) {
            mCamera.release();
            mCamera = null;
        }
    }

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

然后使用您的自定义表面观(方认为)是这样的:

Then use your Custom Surface View (square view)like This:

 <SurfaceView
    android:id="@+id/camera_preview"
    android:layout_width="..."
    android:layout_height="..."
    ..... />

这篇关于自定义广场摄像头 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆