Android實現(xiàn)調(diào)用攝像頭拍照并存儲照片
1、前期準備
需要在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類型的1
2、需要檢查SD卡(外部存儲)狀態(tài)
Environment.MEDIA_MOUNTED sd卡在手機上正常使用狀態(tài)
Environment.MEDIA_UNMOUNTED 用戶手工到手機設(shè)置中卸載sd卡之后的狀態(tài)
Environment.MEDIA_REMOVED 用戶手動卸載,然后將sd卡從手機取出之后的狀態(tài)
Environment.MEDIA_BAD_REMOVAL 用戶未到手機設(shè)置中手動卸載sd卡,直接撥出之后的狀態(tài)
Environment.MEDIA_SHARED 手機直接連接到電腦作為u盤使用之后的狀態(tài)
Environment.MEDIA_CHECKINGS 手機正在掃描sd卡過程中的狀態(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";//定義圖片名稱
Bundle bundle = data.getExtras();//data為onActivityResult中的intent類型的參數(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); // 測試用,查看文件路徑
fos=new FileOutputStream(fileName); // 輸出文件到外部存儲
//今天第一次正視這個bitmap.compress()方法,它用來壓縮圖片大小。
/*
這個方法有三個參數(shù):
Bitmap.CompressFormat format 圖像的壓縮格式;
int quality 圖像壓縮率,0-100。 0 壓縮100%,100意味著不壓縮;
OutputStream stream 寫入壓縮數(shù)據(jù)的輸出流;
public boolean compress(CompressFormat format, int quality, OutputStream stream) {}
返回值boolean類型
如果成功地把壓縮數(shù)據(jù)寫入輸出流,則返回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實現(xiàn)調(diào)用攝像頭拍照并存儲照片的文章就介紹到這了,更多相關(guān)Android調(diào)用攝像頭拍照并存儲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android 動態(tài)添加Fragment的實例代碼
這篇文章主要介紹了Android 動態(tài)添加Fragment的實例代碼的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08
Android Handler 機制實現(xiàn)原理分析
本文主要介紹 Android Handle機制實現(xiàn)的原理,這里整理了詳細的關(guān)于Handler的資料以及工作流程和實際應(yīng)用,有興趣的小伙伴可以參考下2016-08-08
Android開發(fā)入門環(huán)境快速搭建實戰(zhàn)教程
最近想重新學(xué)習下Android,學(xué)習之前開發(fā)環(huán)境的搭建是個首先要解決的問題,所以下面這篇文章主要給大家介紹了Android開發(fā)環(huán)境搭建的相關(guān)資料,文中將實現(xiàn)的步驟一步步介紹的非常詳細,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧。2017-11-11
Android 系統(tǒng)實現(xiàn)多種開機動畫和logo切換功能
這篇文章主要介紹了android 系統(tǒng)實現(xiàn)多種開機動畫和logo切換功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-12-12
Android 6.0動態(tài)權(quán)限申請教程
本文主要介紹了Android 6.0動態(tài)權(quán)限申請的教程,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03

