如何使用SurfaceView和SurfaceHolder显示区域的统治?

时间:2014-03-05 06:19:09

标签: android-camera surfaceview

我想知道是否有办法只显示SurfaceView上显示的相机区域。

我有一个像这样的图像(显微图像)Full Image

我想要显示那样的图像(裁剪和拉伸): Cropped and Stretched image

这是我的代码的一部分(它是使用SurfaceHolder在SurfaceView上显示相机的基本代码):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

setContentView(R.layout.activity_sample_testing);

overridePendingTransition(0, 0);

cameraView = (SurfaceView)findViewById(R.id.CameraView);

    holder = cameraView.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

    @Override
public void surfaceCreated(SurfaceHolder holder) {
    try {
        camera = Camera.open();

        parameters = camera.getParameters();
        parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
        parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);

        //parameters.setZoom(10);
        camera.setParameters(parameters);
        camera.setPreviewDisplay(holder);
        camera.setDisplayOrientation(90);
        camera.startPreview();
        camera.autoFocus(autoFocusCallback);
        recorder = new MediaRecorder();

        final Timer myTimer = new Timer();
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                StartTest(0);
                myTimer.cancel();
                myTimer.purge();
            }
        },10000, 10); 


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}
    private void initRecorder() {
    camera.unlock();

    recorder.setCamera(camera);
    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    CamcorderProfile cpHigh = CamcorderProfile
            .get(CamcorderProfile.QUALITY_HIGH);
    recorder.setProfile(cpHigh);
    recorder.setMaxDuration(50000); // 50 seconds
    recorder.setMaxFileSize(300000000); // Approximately 5 megabytes

    recorder.setOutputFile(VIDEO_PATH);

}
private void prepareRecorder() {

    recorder.setPreviewDisplay(holder.getSurface());
    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        finish();
    } catch (IOException e) {
        e.printStackTrace();
        finish();
    }
}

感谢您的帮助!

0 个答案:

没有答案