【Android开发小记--14】简单拍照功能的实现

调用系统摄像头来实现拍照功能


首先,设置布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.dingding.mucapture.MainActivity">
    <Button
        android:id="@+id/btn_capture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="使用系统拍照功能" />
    <ImageView
        android:id="@+id/img_capture"
        android:layout_width="320dp"
        android:layout_height="320dp" />
</LinearLayout>


MainActivity.java :

1.使用系统拍照功能并显示
2.使用自定义拍照路径并解析显示

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_capture;
    private ImageView img_capture;
    private Uri outputUri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_capture = (Button) findViewById(R.id.btn_capture);
        img_capture = (ImageView) findViewById(R.id.img_capture);
        btn_capture.setOnClickListener(this);
    }
    private final int TAKE_CAPTURE = 1;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //获取系统拍照功能
            case R.id.btn_capture:
//------------------ 1 ----------------------------------------------
//                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//                startActivityForResult(intent, TAKE_CAPTURE);
//------------------ 2 ----------------------------------------------
                //自定义拍照路径
                File file = new File(Environment.getExternalStorageDirectory(), "my.png");
                outputUri = Uri.fromFile(file);
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
                startActivityForResult(intent, TAKE_CAPTURE);
                break;
        }
    }
    /*
     * 获取照片
     * 覆写startActivityForResult中的方法*/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == TAKE_CAPTURE) {
//------------------ 1 ----------------------------------------------
                //解析照片
                if (data != null) {
                    //解析数据
                    if (data.hasExtra("data")) {
                        Bitmap bitmap = data.getParcelableExtra("data");
                        img_capture.setImageBitmap(bitmap);
                    }
                }
//------------------ 2 ----------------------------------------------
                //读取图片,并进行缩放
                else {
                    int width = img_capture.getWidth();
                    int height = img_capture.getHeight();
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(outputUri.getPath(), options);
                    //图片真正的宽高
                    int imgWidth = options.outWidth;
                    int imgHeight = options.outHeight;
                    //缩放系数
                    int scale = Math.min(imgWidth / width, imgHeight / height);
                    scale = scale == 0 ? 1 : scale;
                    options.inJustDecodeBounds = false;
                    options.inSampleSize = scale;
                    Bitmap bitmap = BitmapFactory.decodeFile(outputUri.getPath(), options);
                    img_capture.setImageBitmap(bitmap);
                }
            }
        }
    }
}



具体代码点击




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值