Android Studio:使用Camera拍照(一)调用系统相机

写在前面的话:每一个实例的代码都会附上相应的代码片或者图片,保证代码完整展示在博客中。最重要的是保证例程的完整性!!!方便自己也方便他人~欢迎大家交流讨论~

(注:每处代码都会附上对应的代码片or图片)
1.新建一个Android项目,取名为startcamera。
java主文件(startcamera.java)
layout文件(activity_startcamerae.xml)

2.编辑activity_startcamerae.xml

<?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"
    tools:context=".startcamera">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="startCamera"
        android:text="camera"/>
</LinearLayout>

这里写图片描述
效果预览

3.编辑startcamera.java

package com.example.administrator.startcamere;

import android.app.Activity;
import android.content.Intent;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;

public class startcamera extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startcamera);}
     public void startCamera(View view){
        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivity(intent);
    }
 }

4.运行
运行模拟器or用USB连接真机开启USB调试模式即可看到执行效果

附:学习的原视频教程地址Android摄像头基础https://www.imooc.com/learn/543

猜你喜欢

转载自blog.csdn.net/Leo_Huang720/article/details/81103549