Android 调用手机摄像头拍照并显示在当前界面
发布日期:2021-05-06 03:14:25 浏览次数:19 分类:原创文章

本文共 1619 字,大约阅读时间需要 5 分钟。

Android 调用手机摄像头拍照并显示在当前界面


第一步:安卓界面布局


<?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=".MainActivity">        <Button        android:id="@+id/b1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_marginTop="20dp"        android:text="启动摄像头"/>    <ImageView        android:id="@+id/i1"        android:layout_width="400dp"        android:layout_gravity="center"        android:layout_marginTop="50dp"        android:layout_height="300dp"/></LinearLayout>

界面截图:
在这里插入图片描述
第二步:功能实现
找到控件并绑定ID
在这里插入图片描述
调用摄像头


b1.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent,CAMERA_REQUEST); } });

显示照片


    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CAMERA_REQUEST: if (resultCode == RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data"); i1.setImageBitmap(bitmap); } break; } super.onActivityResult(requestCode, resultCode, data); }
上一篇:C语言 汇总笔记(小甲鱼:带你学C带你飞)
下一篇:Android 五种常用的对话框

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年03月16日 08时35分25秒