zoukankan      html  css  js  c++  java
  • 每日日报2021 5/25

    总概括:

    public class MainActivity extends AppCompatActivity {
    
        private SurfaceView sfv_preview;
        private Button btn_take;
        private Camera camera = null;
        private SurfaceHolder.Callback cpHolderCallback = new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                startPreview();
            }
    
            @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);
            bindViews();
        }
    
        private void bindViews() {
            sfv_preview = (SurfaceView) findViewById(R.id.sfv_preview);
            btn_take = (Button) findViewById(R.id.btn_take);
            sfv_preview.getHolder().addCallback(cpHolderCallback);
    
            btn_take.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    camera.takePicture(null, null, new Camera.PictureCallback() {
                        @Override
                        public void onPictureTaken(byte[] data, Camera camera) {
                            String path = "";
                            if ((path = saveFile(data)) != null) {
                                Intent it = new Intent(MainActivity.this, PreviewActivity.class);
                                it.putExtra("path", path);
                                startActivity(it);
                            } else {
                                Toast.makeText(MainActivity.this, "保存照片失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            });
        }
    
        //保存临时文件的方法
        private String saveFile(byte[] bytes){
            try {
                File file = File.createTempFile("img","");
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(bytes);
                fos.flush();
                fos.close();
                return file.getAbsolutePath();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "";
        }
    
    
        //开始预览
        private void startPreview(){
            camera = Camera.open();
            try {
                camera.setPreviewDisplay(sfv_preview.getHolder());
                camera.setDisplayOrientation(90);   //让相机旋转90度
                camera.startPreview();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        //停止预览
        private void stopPreview() {
            camera.stopPreview();
            camera.release();
            camera = null;
        }
    
    }


    /**
     * Created by Jay on 2015/11/22 0022.
     */
    public class PreviewActivity extends AppCompatActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ImageView img = new ImageView(this);
            String path = getIntent().getStringExtra("path");
            if(path != null){
                img.setImageURI(Uri.fromFile(new File(path)));
            }
            setContentView(img);
        }
    }
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <SurfaceView
            android:id="@+id/sfv_preview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    
        <Button
            android:id="@+id/btn_take"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="调用系统照相机" />
    
    </LinearLayout>
  • 相关阅读:
    国产CPU研究单位及现状
    大型网站用什么技术比较好,JSP,PHP,ASP.NET
    韩国企业百强排行榜!
    中国操作系统
    Visual C++2010开发权威指南 中文高清PDF
    printf以及各种变种
    SPDY以及HTTP2.0
    数字证书认证这点事, SSL/TLS,OpenSSL
    利用Fiddler,解密wireshark抓的HTTPS包
    C跟C++
  • 原文地址:https://www.cnblogs.com/song-1/p/14882083.html
Copyright © 2011-2022 走看看