日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2025/3/19 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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"><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="啟動攝像頭" /><ImageViewandroid: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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_camera);initView();}@Overrideprotected 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() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent,CAMERA_REQUEST);}});} }

示例效果

?

?

總結

以上是生活随笔為你收集整理的Android中实现调用摄像头拍照并显示在ImageView中的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。