Android开发系列——实战篇12:相机(设备操作篇)


本文开始学习通过Android平台调用一些设备的硬件。

首先是对摄像头的调用

在APP中调用相机有两种方式:

  • 直接调用系统的相机应用(或者其他有相机功能的应用)。
  • 自定义相机功能。

一、调用系统相机

调用系统相机的部分比较容易。

1.建立一个新的布局文件(“name”.xml)。
2.建立一个新的java类文件(“name”.java)。

这里为了方便展示,直接用工程里的main文件。

1.启动相机

"activity_main.xml"文件

在布局文件里建立一个Button控件。

控件视图代码

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="100dp"
        android:layout_marginRight="100dp"
        android:gravity="center"
        android:layout_gravity="center"
        android:onClick="take_photo"
        android:text="拍照" />

控件视图效果

注:
1.这里在Button视图中添加了onClick属性,相当于java代码中设置监听器。
2.onClick属性的方法名称就是java代码中点击类的方法名称。

“MainActivity.java文件”

在java文件中建立一个新的点击事件类,并且通过new一个Intent包,这个包里调用了摄像捕捉的行为,将它作为事件响应传递给一个activity活动。

具体代码如下

package com.example.devices;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;


public class MainActivity extends AppCompatActivity {
   

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

    public void take_photo(View view) {
   
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivity(intent);
   }
}

整个逻辑是在这个应用程序里,通过ACTION行为启动另一个应用程序,相当于放了一个应用程序的入口,类似在电脑桌面中通过快捷方式启动软件。

在AndroidManifest.xml文件中注册相机功能

通过在清单文件中的设置过滤器,可以给应用添加相机功能(无关应用本身,相当于给应用添加了一个身份牌)

具体代码:

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

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

2.获取图片

在调用相机拍完照之后应用程序并不会显示出图片,如果要让它实时显示出图片,则需要对应用程序进行配置。

“activity_main.xml文件”

这里呈现整个布局文件的代码,主要是在LinearLayout视图中设置边距使得布局更加美观:

<?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:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/bt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="take_photo"
        android:text="拍照" />
    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    
</LinearLayout>

布局文件效果图:

“MainActivity.java文件”

java类文件中的代码如下:

package com.example.devices;

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.ImageView;


public class MainActivity extends AppCompatActivity {
   

    private static int req_one=1; //定义一个req_one变量,初始值为1
    private ImageView imageView;  //初始化ImageView对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.iv);
    }

    public void take_photo(View view) {
    //点击方法
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent,req_one);
   }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
   
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == RESULT_FIRST_USER){
    //系统内部标准,代表成功返回理想值
            if(requestCode == req_one){
   
                Bundle bundle = data.getExtras(); //通过Bundle对象从返回值中获取数据
                Bitmap bitmap = (Bitmap) bundle.get("data"); //通过强制类型转换将数据转换成二进制流
                imageView.setImageBitmap(bitmap);//通过调用该方法使得二进制流在指定位置呈现为图片
            }

        }
    }
}

通过这个方法,拍摄的图片就可以直接呈现在主页面。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值