• 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"/>
  • 相关阅读:
    求每天的收入和支出
    行列 转换 合并 分拆
    用户消费总金额 2000以下 20004000 40006000 查询连续数字,统计个数
    txt文件分割导入数据库
    字符串分割函数拆分成多行
    如何向一个自增字段插值
    sql中带in条件的查询及提高效率
    逐行计算、逐行递延、逐行更新
    SQL2005实现全文检索的步骤 停止数据库的用户连接
    查字段指定数据后一行记录
  • 原文地址:https://www.cnblogs.com/itfenqing/p/6747250.html
Copyright © 2020-2023  润新知