Android實(shí)現(xiàn)調(diào)用攝像頭拍照并存儲(chǔ)照片
1、前期準(zhǔn)備
需要在Manifest中添加相關(guān)權(quán)限
<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調(diào)用攝像頭
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//調(diào)用Camera startActivityForResult(intent, Activity.RESULT_OK);//如果正常,則返回 Activity.RESULT_OK 本質(zhì)為 int類(lèi)型的1
2、需要檢查SD卡(外部存儲(chǔ))狀態(tài)
Environment.MEDIA_MOUNTED sd卡在手機(jī)上正常使用狀態(tài)
Environment.MEDIA_UNMOUNTED 用戶(hù)手工到手機(jī)設(shè)置中卸載sd卡之后的狀態(tài)
Environment.MEDIA_REMOVED 用戶(hù)手動(dòng)卸載,然后將sd卡從手機(jī)取出之后的狀態(tài)
Environment.MEDIA_BAD_REMOVAL 用戶(hù)未到手機(jī)設(shè)置中手動(dòng)卸載sd卡,直接撥出之后的狀態(tài)
Environment.MEDIA_SHARED 手機(jī)直接連接到電腦作為u盤(pán)使用之后的狀態(tài)
Environment.MEDIA_CHECKINGS 手機(jī)正在掃描sd卡過(guò)程中的狀態(tài)
在代碼中的判斷
String sdStatus = Environment.getExternalStorageState();
if(!sdStatus.equals(Environment.MEDIA_MOUNTED)){
System.out.println(" ------------- sd card is not avaiable ---------------");
return;
}
3、獲取圖片及其壓縮圖片
String name = "photo.jpg";//定義圖片名稱(chēng)
Bundle bundle = data.getExtras();//data為onActivityResult中的intent類(lèi)型的參數(shù)
Bitmap bitmap = (Bitmap) bundle.get("data");//bitmap
// File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/");
// file.mkdirs(); //創(chuàng)建文件夾
// String fileName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+name;
String fileName = "sdcard"+"/"+name;//初始化文件路徑
FileOutputStream fos =null;//初始化文件輸出流
try {
// System.out.println(fileName); // 測(cè)試用,查看文件路徑
fos=new FileOutputStream(fileName); // 輸出文件到外部存儲(chǔ)
//今天第一次正視這個(gè)bitmap.compress()方法,它用來(lái)壓縮圖片大小。
/*
這個(gè)方法有三個(gè)參數(shù):
Bitmap.CompressFormat format 圖像的壓縮格式;
int quality 圖像壓縮率,0-100。 0 壓縮100%,100意味著不壓縮;
OutputStream stream 寫(xiě)入壓縮數(shù)據(jù)的輸出流;
public boolean compress(CompressFormat format, int quality, OutputStream stream) {}
返回值boolean類(lèi)型
如果成功地把壓縮數(shù)據(jù)寫(xiě)入輸出流,則返回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(); //創(chuàng)建文件夾
// 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);
}
}
}
到此這篇關(guān)于Android實(shí)現(xiàn)調(diào)用攝像頭拍照并存儲(chǔ)照片的文章就介紹到這了,更多相關(guān)Android調(diào)用攝像頭拍照并存儲(chǔ)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Androidstudio調(diào)用攝像頭拍照并保存照片
- Android調(diào)用手機(jī)攝像頭拍照和錄音功能
- Android調(diào)用系統(tǒng)攝像頭拍照并顯示在ImageView上
- Android實(shí)現(xiàn)調(diào)用攝像頭拍照與視頻功能
- Android實(shí)現(xiàn)攝像頭拍照功能
- Android調(diào)用攝像頭拍照開(kāi)發(fā)教程
- Android實(shí)現(xiàn)調(diào)用攝像頭進(jìn)行拍照功能
- Android 開(kāi)發(fā)隨手筆記之使用攝像頭拍照
- Android調(diào)用外置攝像頭的方法
- Android實(shí)現(xiàn)控制攝像頭拍照
相關(guān)文章
Android 動(dòng)態(tài)添加Fragment的實(shí)例代碼
這篇文章主要介紹了Android 動(dòng)態(tài)添加Fragment的實(shí)例代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
Android Handler 機(jī)制實(shí)現(xiàn)原理分析
本文主要介紹 Android Handle機(jī)制實(shí)現(xiàn)的原理,這里整理了詳細(xì)的關(guān)于Handler的資料以及工作流程和實(shí)際應(yīng)用,有興趣的小伙伴可以參考下2016-08-08
Android開(kāi)發(fā)入門(mén)環(huán)境快速搭建實(shí)戰(zhàn)教程
最近想重新學(xué)習(xí)下Android,學(xué)習(xí)之前開(kāi)發(fā)環(huán)境的搭建是個(gè)首先要解決的問(wèn)題,所以下面這篇文章主要給大家介紹了Android開(kāi)發(fā)環(huán)境搭建的相關(guān)資料,文中將實(shí)現(xiàn)的步驟一步步介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
Android 系統(tǒng)實(shí)現(xiàn)多種開(kāi)機(jī)動(dòng)畫(huà)和logo切換功能
這篇文章主要介紹了android 系統(tǒng)實(shí)現(xiàn)多種開(kāi)機(jī)動(dòng)畫(huà)和logo切換功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12
Android 6.0動(dòng)態(tài)權(quán)限申請(qǐng)教程
本文主要介紹了Android 6.0動(dòng)態(tài)權(quán)限申請(qǐng)的教程,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
Android實(shí)現(xiàn)IP地址輸入框的方法示例代碼
輸入框是我們?nèi)粘i_(kāi)發(fā)中經(jīng)常遇到的一個(gè)控件,如果更好的控制輸入框是對(duì)用戶(hù)體驗(yàn)很重要的一步,所以下面這篇文章主要給大家介紹了關(guān)于Android如何實(shí)現(xiàn)IP輸入框的相關(guān)資料,需要的朋友可以參考下。2017-10-10
Android實(shí)現(xiàn)后臺(tái)服務(wù)拍照功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)后臺(tái)服務(wù)拍照功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

