阅读 261

Android实现调用摄像头拍照并存储照片

本文主要介绍了如何利用Android调用摄像头拍照,并显示拍照后的图片到ImageView中,文中的示例代码讲解详细,感兴趣的可以动手试一试

目录
  • 1、前期准备

  • 2、主要方法

    • 1、需要使用Intent调用摄像头

    • 2、需要检查SD卡(外部存储)状态

    • 3、获取图片及其压缩图片

  • 3、案例展示

    • 1、Layout

    • 2、MainActivity

1、前期准备

需要在Manifest中添加相关权限

1
2
3
4
5
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2、主要方法

1、需要使用Intent调用摄像头

1
2
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//调用Camera
startActivityForResult(intent, Activity.RESULT_OK);//如果正常,则返回    Activity.RESULT_OK    本质为 int类型的1

2、需要检查SD卡(外部存储)状态

Environment.MEDIA_MOUNTED sd卡在手机上正常使用状态

Environment.MEDIA_UNMOUNTED 用户手工到手机设置中卸载sd卡之后的状态

Environment.MEDIA_REMOVED 用户手动卸载,然后将sd卡从手机取出之后的状态

Environment.MEDIA_BAD_REMOVAL 用户未到手机设置中手动卸载sd卡,直接拨出之后的状态

Environment.MEDIA_SHARED 手机直接连接到电脑作为u盘使用之后的状态

Environment.MEDIA_CHECKINGS 手机正在扫描sd卡过程中的状态

在代码中的判断

1
2
3
4
5
String sdStatus = Environment.getExternalStorageState();
if(!sdStatus.equals(Environment.MEDIA_MOUNTED)){
                System.out.println(" ------------- sd card is not avaiable ---------------");
                return;
}

3、获取图片及其压缩图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
String name = "photo.jpg";//定义图片名称
 
Bundle bundle = data.getExtras();//data为onActivityResult中的intent类型的参数
Bitmap bitmap = (Bitmap) bundle.get("data");//bitmap
 
//            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/");
//            file.mkdirs(); //创建文件夹
 
//            String fileName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+name;
String fileName = "sdcard"+"/"+name;//初始化文件路径
 
FileOutputStream fos =null;//初始化文件输出流
 
try {
    // System.out.println(fileName);    // 测试用,查看文件路径
    fos=new FileOutputStream(fileName); // 输出文件到外部存储
    //今天第一次正视这个bitmap.compress()方法,它用来压缩图片大小。
    /*
        这个方法有三个参数:
        Bitmap.CompressFormat format 图像的压缩格式;
        int quality 图像压缩率,0-100。 0 压缩100%,100意味着不压缩;
        OutputStream stream 写入压缩数据的输出流;
        public boolean compress(CompressFormat format, int quality, OutputStream stream) {}
        返回值boolean类型
        如果成功地把压缩数据写入输出流,则返回true。
    */
    bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);   
     
} catch (FileNotFoundException e) {
    e.printStackTrace();
}finally {
    try {
        fos.flush();    //释放输出流
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
 
}

3、案例展示

1、Layout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_take_photo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="take photo"
        android:id="@+id/takephoto"
        />
    <ImageView
        android:layout_below="@+id/takephoto"
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:id="@+id/picture"
        />
</RelativeLayout>

2、MainActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package icu.whatsblog.CameraCrop;
 
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Environment;
import android.provider.MediaStore;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
 
import androidx.appcompat.app.AppCompatActivity;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
import lee.suk.cameracrop.R;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        ((Button) findViewById(R.id.takephoto)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 
                startActivityForResult(intent, 1);
            }
        });
    }
 
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
 
        if (resultCode == Activity.RESULT_OK){
            String sdStatus = Environment.getExternalStorageState();
 
            if(!sdStatus.equals(Environment.MEDIA_MOUNTED)){
                System.out.println(" ------------- sd card is not avaiable ---------------");
                return;
            }
 
 
            String name = "photo.jpg";
 
            Bundle bundle = data.getExtras();
            Bitmap bitmap = (Bitmap) bundle.get("data");
 
//            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/");
//            file.mkdirs(); //创建文件夹
 
//            String fileName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+name;
            String fileName = "sdcard"+"/"+name;
 
            FileOutputStream fos =null;
 
            try {
                System.out.println(fileName);
                fos=new FileOutputStream(fileName);
                bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }finally {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
 
            }
            ((ImageView) findViewById(R.id.picture)).setImageBitmap(bitmap);
        }
    }
}

到此这篇关于Android实现调用摄像头拍照并存储照片的文章就介绍到这了

原文链接:https://blog.csdn.net/w_Eternal/article/details/122425400


文章分类
百科问答
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐