• 每日日报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>
  • 相关阅读:
    .NET 事件模型教程(一)
    [转帖]Predicate的用法
    MSBuild入门(续)
    浅析C# 中object sender与EventArgs e
    调用一个按钮的Click事件(利用反射)
    Delegate,Action,Func,匿名方法,匿名委托,事件
    投票系统如何防止一个用户多次投票
    如何发送表单
    SharePoint NLB选Unicast还是选Multicast?
    为SharePoint的多台WFE配置Windows自带的NLB遇到的一个问题
  • 原文地址:https://www.cnblogs.com/song-1/p/14882083.html
Copyright © 2020-2023  润新知