網(wǎng)站首頁 編程語言 正文
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類型的1
2、需要檢查SD卡(外部存儲)狀態(tài)
Environment.MEDIA_MOUNTED
sd卡在手機(jī)上正常使用狀態(tài)
Environment.MEDIA_UNMOUNTED
用戶手工到手機(jī)設(shè)置中卸載sd卡之后的狀態(tài)
Environment.MEDIA_REMOVED
用戶手動(dòng)卸載,然后將sd卡從手機(jī)取出之后的狀態(tài)
Environment.MEDIA_BAD_REMOVAL
用戶未到手機(jī)設(shè)置中手動(dòng)卸載sd卡,直接撥出之后的狀態(tài)
Environment.MEDIA_SHARED
手機(jī)直接連接到電腦作為u盤使用之后的狀態(tài)
Environment.MEDIA_CHECKINGS
手機(jī)正在掃描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); // 輸出文件到外部存儲 //今天第一次正視這個(gè)bitmap.compress()方法,它用來壓縮圖片大小。 /* 這個(gè)方法有三個(gè)參數(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); } } }
原文鏈接:https://blog.csdn.net/w_Eternal/article/details/122425400
相關(guān)推薦
- 2022-11-18 Redux?thunk中間件及執(zhí)行原理詳細(xì)分析_React
- 2023-08-15 (el-Form)操作(不使用 ts):Element-plus 中 Form 表單組件校驗(yàn)規(guī)則等的
- 2022-09-26 nodemon安裝在開發(fā)環(huán)境(非全局安裝)報(bào)錯(cuò)解決【兩種方式】
- 2022-07-19 詳解c語言中的動(dòng)態(tài)內(nèi)存分配問題
- 2022-08-05 基于redis+lua進(jìn)行限流
- 2022-08-11 GoFrame框架數(shù)據(jù)校驗(yàn)之校驗(yàn)結(jié)果Error接口對象_Golang
- 2023-03-03 flutter布局約束原理深入解析_Android
- 2022-10-02 react如何獲取state的值并更新使用_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支