SurfaceView实现拍照预览

Posted 罗夏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SurfaceView实现拍照预览相关的知识,希望对你有一定的参考价值。

一.布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
   >



    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/sfv_preview"
        android:layout_weight="1"/>
    <Button
        android:text="调用系统相机"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_take"
        android:onClick="myClick"
        />
</LinearLayout>

 

 

二.关键代码

public class MainActivity extends Activity {

    private SurfaceView sfv_view;
    private Button btn_take;
    private Camera camera;

    private SurfaceHolder.Callback cpHolderCallback = new SurfaceHolder.Callback(){

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            preview();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            stopPreview();
        }
    };

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

        sfv_view = (SurfaceView)findViewById(R.id.sfv_preview);
        sfv_view.getHolder().addCallback(cpHolderCallback);
    }

    protected void myClick(View v){
        if( v.getId() == R.id.btn_take ){
            camera.takePicture(null, null, new Camera.PictureCallback(){

                @Override
                public void onPictureTaken(byte[] data, Camera camera) {
                    String path = "";
                    if( (path = saveImage(data)) != null){
                        Intent it = new Intent(MainActivity.this, PreviewActivity.class);
                        it.putExtra("path", path);
                        startActivity(it);
                    }else{
                        Toast.makeText(MainActivity.this,
                                "保存图片失败", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    }

    // 保存临时问价
    private String saveImage(byte[] bytes){
        try{
            File f = File.createTempFile("img", "");
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(bytes);
            fos.flush();
            fos.close();
            return f.getAbsolutePath();
        }catch (IOException e){
            e.printStackTrace();
        }
        return "";
    }

    // 预览
    private void preview(){
        camera = Camera.open();
        try{
            camera.setPreviewDisplay(sfv_view.getHolder());
            camera.setDisplayOrientation(90);
            camera.startPreview();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    // 停止预览
    private void stopPreview(){
        camera.stopPreview();
        camera.release();
        camera = null;
    }
}

 

 

三.PreviewActivity代码

 

public class PreviewActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ImageView imageView = new ImageView(this);
        String path = getIntent().getStringExtra("path");
        if( path != null ){
            imageView.setImageURI(Uri.fromFile(new File(path)));
        }
        setContentView(imageView);
    }
}

 

 

四.所需权限:

 

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

以上是关于SurfaceView实现拍照预览的主要内容,如果未能解决你的问题,请参考以下文章

Android Camera2 拍照——使用TextureView

Android Camera开发:使用TextureView和SurfaceTexture预览Camera 基础拍照demo

玩转Android Camera开发:使用TextureView和SurfaceTexture预览Camera 基础拍照demo

从surfaceview保存图片

转玩转Android Camera开发:国内首发---使用GLSurfaceView预览Camera 基础拍照demo

玩转Android Camera开发:使用TextureView和SurfaceTexture预览Camera 基础拍照demo