zoukankan      html  css  js  c++  java
  • 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"/>
  • 相关阅读:
    C# TCP/IP 服务端 和 客户端
    (trigger)触发器的定义和作用
    AD账号登陆验证
    DES加密&解密字符串
    机器视觉(工业视觉)需要什么技能
    机器视觉对位贴合
    Halcon blob分析基本处理步骤
    cross_val_score 交叉验证与 K折交叉验证,嗯都是抄来的,自己作个参考
    50道SQL练习题及答案与详细分析(MySQL)
    MySQL8.0 ROW_NUMBER、RANK、DENSE_RANK窗口函数 分组排序排名
  • 原文地址:https://www.cnblogs.com/itfenqing/p/6747250.html
Copyright © 2011-2022 走看看