安卓中关于相机的一些知识点

代码块

1.调用相机动作

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

2.用uri 可指定拍照后的保存位置,用于获取清晰图片

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Uri uri = Uri.fromFile(new File(path));
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intent, Photo_Big);

3.回调方法

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == Photo_Small) {
            Bundle bundle = data.getExtras();
            Bitmap bitmap = (Bitmap) bundle.get("data");
            iv.setImageBitmap(bitmap);

        }
        if (requestCode == Photo_Big) {
            FileInputStream is = null;
            try {
                is = new FileInputStream(path);
                Bitmap bitmap = BitmapFactory.decodeStream(is);
                iv.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

自定义相机

1.布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="Click"
        android:text="Click!" 
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

2.代码

public class mCamera extends Activity implements SurfaceHolder.Callback {

    private Camera mCamera;
    private SurfaceView preview;
    private SurfaceHolder mholder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);
        preview = (SurfaceView) findViewById(R.id.sfv);
        mholder = preview.getHolder();
        mholder.addCallback(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mCamera == null) {
            mCamera = GetCamera();
            if (mholder != null) {
                StartPreview(mCamera, mholder);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        ReleaseCamera();
    }

    public void Click(View v) {

    }

    // 获取硬件相机资源
    private Camera GetCamera() {
        Camera camera = Camera.open();
        return camera;
    }

    // 开启预览
    private void StartPreview(Camera camera, SurfaceHolder holder) {
        try {
            camera.setPreviewDisplay(holder);
            camera.setDisplayOrientation(90);
            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 释放硬件相机资源
    private void ReleaseCamera() {
        if (mCamera != null) {
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
    }

    /* 重写方法 */
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        StartPreview(mCamera, mholder);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mCamera.stopPreview();
        StartPreview(mCamera, mholder);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        ReleaseCamera();
    }
    /* 重写方法 */
}

3.添加 intent-filter

            <intent-filter >
                <action android:name="android.media.action.IMAGE_CAPTURE"/>

                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filte`
>

4.添加权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值