Android Camera 自定义拍照界面

一般情况下调用系统相机就足够了,但比如扫描二维码则需要自定义拍照界面

我们写一个范例创建自定义拍照界面,我们会使用 SurfaceView 作为我们的预览界面


  1. 创建一个 空的 Android 项目 cn.twle.android.CustomCamera

  2. 修改 activity_main.xml 添加一个简单的 SurfaceView + Button

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">
    
        <SurfaceView
            android:id="@+id/sfv_preview"
            android:layout_marginTop="100dp"
            android:layout_width="200dp"
            android:layout_height="200dp" />
    
        <Button
            android:id="@+id/btn_take"
            android:layout_marginTop="32dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="拍照" />
    
    </LinearLayout>
    
  3. 创建 activity_preview.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
    
        <ImageView
            android:id="@+id/preview"
            android:layout_marginTop="100dp"
            android:layout_width="200dp"
            android:layout_height="200dp" />
    
    </LinearLayout>
    
  4. 然后创建一个 PreviewActivity.java 用于预览内容

    package cn.twle.android.customcamera;
    
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.ImageView;
    
    import java.io.File;
    
    public class PreviewActivity extends AppCompatActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_preview);
    
            ImageView img = findViewById(R.id.preview);
            String path = getIntent().getStringExtra("path");
            if(path != null){
                img.setImageURI(Uri.fromFile(new File(path)));
            }
        }
    }
    
  5. 修改 AndroidManifest.xml 添加调用系统相机权限和注册 PreviewActivity

    调用系统相机权限

    <uses-permission android:name="android.permission.CAMERA"/>
    

    注册 PreviewActivity

    <activity android:name=".PreviewActivity" />
    
  6. 修改 MainActivity.java 拍照并保存照片

    package cn.twle.android.customcamera;
    
    import android.content.Intent;
    import android.hardware.Camera;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    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;
        }
    
    }
    

Android 基础教程

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.