首页 > 代码库 > SurfaceView实现拍照预览

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实现拍照预览