Android中实现调用摄像头拍照并显示在ImageView中

场景

点击拍照按钮调用系统摄像机进行拍照,并将拍的照片显示在ImageView中。

 

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

新建一个Activity,设计其布局如下

 

布局xml文件为

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".CameraActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启动摄像头" />

    <ImageView
        android:id="@+id/iv_camera"
        android:layout_width="350dp"
        android:layout_height="350dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp" />

</LinearLayout>

然后在对应的Activity中,通过指定Action的Intent来调用系统摄像头,并采用带返回结果的方式启动Activity,

然后在重写的获取Activity返回结果的方法中,判断请求码与上面自定义的常量一致并且请求结果为OK

那么通过将data强转为Bitmap并给ImageView设置数据源进行显示

package com.badao.androidstudy;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class CameraActivity extends AppCompatActivity {


    private Button btnCamera;
    private ImageView imageView;
    private final int CAMERA_REQUEST = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        initView();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CAMERA_REQUEST:
                if (resultCode == RESULT_OK) {
                    Bitmap bitmap = (Bitmap) data.getExtras().get("data");
                    imageView.setImageBitmap(bitmap);
                }
                break;
        }
    }

    public void initView(){
        imageView = findViewById(R.id.iv_camera);
        btnCamera = findViewById(R.id.button);
        btnCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,CAMERA_REQUEST);
            }
        });
    }
}

示例效果

 

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,这是一个比较常见的需求,下面是实现的步骤: 1. 添加权限 在 AndroidManifest.xml 文件添加相机和文件读写权限: ```xml <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 2. 添加布局 在布局文件添加一个 ImageView 和一个 Button: ```xml <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="centerCrop" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拍照" /> ``` 3. 调用相机拍照 在按钮的点击事件调用相机拍照,并将照片保存到本地: ```java private static final int REQUEST_CODE_CAMERA = 1; private String mImagePath; Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 创建拍照的 Intent Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 创建保存图片的文件 File file = new File(getExternalCacheDir(), System.currentTimeMillis() + ".jpg"); mImagePath = file.getAbsolutePath(); // 设置拍照保存照片的路径 Uri uri = FileProvider.getUriForFile(MainActivity.this, getPackageName() + ".fileprovider", file); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); // 调用相机拍照 startActivityForResult(intent, REQUEST_CODE_CAMERA); } }); ``` 在拍照后,会将照片保存到指定的路径。 4. 显示照片 在 onActivityResult 方法获取拍照后的照片,并显示ImageView : ```java @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) { // 显示照片 Bitmap bitmap = BitmapFactory.decodeFile(mImagePath); ImageView imageView = findViewById(R.id.imageView); imageView.setImageBitmap(bitmap); } } ``` 这样就可以实现调用相机拍照显示ImageView 了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值