Android中的多媒体开发(三)——使用Camera拍照

    第一种方法就很简单啦,我们进行简单的调用系统的Camera来完成功能。

代码实现

 

   //定义一个保存图片的File变量
    private File currentImageFile = null;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();
        bindViews();
 
/**
 StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
 StrictMode.setVmPolicy(builder.build());
 builder.detectFileUriExposure();
 在Appaction的onCreat()方法中添加以上的代码。
 //android7.0系统解决拍照的问题。
 */

} private void bindViews() { img_show = (ImageView) findViewById(R.id.img_show); btn_start = (Button) findViewById(R.id.btn_start); btn_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //在按钮点击事件处写上这些东西,这些是在SD卡创建图片文件的: File dir = new File(Environment.getExternalStorageDirectory(),"pictures"); if(dir.exists()){ dir.mkdirs(); } currentImageFile = new File(dir,System.currentTimeMillis() + ".jpg"); if(!currentImageFile.exists()){ try { currentImageFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(currentImageFile)); startActivityForResult(it, Activity.DEFAULT_KEYS_DIALER); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == Activity.DEFAULT_KEYS_DIALER) { img_show.setImageURI(Uri.fromFile(currentImageFile)); } }}

通过将图片保存在SD卡中,然后再进行显示图片会更加清晰。

 

另一种方式肯定就是自己写一个拍照页面。

 

代码实现:

布局代码: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: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>

MainActivity.java:

 

        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;
        }

    }
}

另外一个PreviewActivity.java,将图片显示到界面上~

 

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"/>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值