Android实现调用摄像头拍照并存储照片
作者:吃橘子的季节呢 时间:2023-05-02 10:48:20
1、前期准备
需要在Manifest中添加相关权限
<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调用摄像头
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卡过程中的状态
在代码中的判断
String sdStatus = Environment.getExternalStorageState();
if(!sdStatus.equals(Environment.MEDIA_MOUNTED)){
System.out.println(" ------------- sd card is not avaiable ---------------");
return;
}
3、获取图片及其压缩图片
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
<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
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);
}
}
}
来源:https://blog.csdn.net/w_Eternal/article/details/122425400
标签:Android,摄像头,拍照
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android实现单选按钮
2021-11-20 18:32:53
![](https://img.aspxhome.com/file/2023/7/137587_0s.jpg)
基于使用BeginInvoke,EndInvoke异步调用委托的实现代码
2023-04-29 09:46:49
Java基础学习之集合底层原理
2023-09-30 22:16:40
![](https://img.aspxhome.com/file/2023/0/86820_0s.png)
详解C语言内核字符串拷贝与比较
2023-11-02 15:22:30
![](https://img.aspxhome.com/file/2023/2/107392_0s.png)
浅析C#中数组,ArrayList与List对象的区别
2022-03-12 00:41:30
Struts2 文件上传进度条的实现实例代码
2023-04-20 11:13:59
![](https://img.aspxhome.com/file/2023/0/72620_0s.png)
一小时迅速入门Mybatis之初识篇
2023-07-20 10:27:23
![](https://img.aspxhome.com/file/2023/2/132212_0s.jpg)
Java WebService 简单实例(附实例代码)
2023-01-25 07:52:18
![](https://img.aspxhome.com/file/2023/8/64528_0s.png)
RabbitMQ死信机制实现延迟队列的实战
2023-10-10 18:41:57
![](https://img.aspxhome.com/file/2023/8/77478_0s.png)
java9开始——接口中可以定义private私有方法
2023-03-27 06:53:15
![](https://img.aspxhome.com/file/2023/1/66981_0s.png)
c# 使用OpenCV识别硬币
2021-12-18 15:09:13
![](https://img.aspxhome.com/file/2023/2/119552_0s.png)
Android自定义荷载进度的两种方法
2022-08-29 03:21:39
![](https://img.aspxhome.com/file/2023/9/137939_0s.png)
Spring Security 控制授权的方法
2023-08-06 19:21:08
![](https://img.aspxhome.com/file/2023/4/58024_0s.png)
Android SQLite数据库版本升级的管理实现
2023-11-09 23:58:44
Java异常处理机制try catch流程详解
2022-09-23 08:51:09
![](https://img.aspxhome.com/file/2023/4/62404_0s.png)
@SpringBootApplication注解的使用
2022-09-13 04:53:32
![](https://img.aspxhome.com/file/2023/5/68195_0s.png)
C++临时性对象的生命周期详细解析
2023-01-22 04:26:01
![](https://img.aspxhome.com/file/2023/2/122572_0s.jpg)
Android中listview和imageview实现条目单选效果
2022-12-05 05:02:31
![](https://img.aspxhome.com/file/2023/0/130360_0s.jpg)
Android 中SwipeRefreshLayout与ViewPager滑动事件冲突解决方法
2023-02-26 21:34:17
.NET Core使用C#扫描并读取图片中的文字
2022-03-09 11:06:54
![](https://img.aspxhome.com/file/2023/6/111096_0s.jpg)