Android開發(fā)實(shí)現(xiàn)ImageView加載攝像頭拍攝的大圖功能
本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)ImageView加載攝像頭拍攝的大圖功能。分享給大家供大家參考,具體如下:
這個(gè)方法是從官方demo中摘錄的,在此記錄學(xué)習(xí)。
權(quán)限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-feature android:name="android.hardware.camera2" android:required="false" />
另:關(guān)于權(quán)限控制還可參考:Android Manifest功能與權(quán)限描述大全
設(shè)置變量保存文件存儲(chǔ)路徑
private String mCurrentPhotoPath; /** * 拍照f(shuō)lag */ private static final int REQUEST_IMAGE_CAPTURE_O = 2;
創(chuàng)建存儲(chǔ)路徑及文件名
/**
* 創(chuàng)建拍攝的圖片的存儲(chǔ)路徑及文件名
* @return
* @throws IOException
*/
private File createImageFile() throws IOException{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
Log.d("TrainingFirstActivity", "storageDir:" + storageDir);
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
mCurrentPhotoPath = image.getAbsolutePath();
Log.d("image.getAbsolutePath()", image.getAbsolutePath() + "");
return image;
}
拍攝圖片并保存
Intent takePictureOintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureOintent.resolveActivity(getPackageManager()) != null){
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
if (photoFile != null){
takePictureOintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureOintent, REQUEST_IMAGE_CAPTURE_O);
}
}
處理并壓縮拍照結(jié)果,takePhotoThenToShowImg是一個(gè)ImageView控件
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE_O && resultCode == RESULT_OK){
int targetW = takePhotoThenToShowImg.getWidth();
int targetH = takePhotoThenToShowImg.getHeight();
/* Get the size of the image */
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
/* Figure out which way needs to be reduced less */
int scaleFactor = 1;
if ((targetW > 0) || (targetH > 0)) {
scaleFactor = Math.min(photoW/targetW, photoH/targetH);
}
/* Set bitmap options to scale the image decode target */
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
/* Decode the JPEG file into a Bitmap */
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
/* Associate the Bitmap to the ImageView */
takePhotoThenToShowImg.setImageBitmap(bitmap);
galleryAddPic();
}
}
最后可以將拍攝到的照片添加到Media Provider的數(shù)據(jù)庫(kù)中,以便圖庫(kù)或者其他程序讀取照片
/**
* 將拍攝到的照片添加到Media Provider的數(shù)據(jù)庫(kù)中
*/
private void galleryAddPic(){
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
如果只需要縮略圖的話,只要調(diào)攝像頭拍攝直接處理結(jié)果就行
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){//展示圖片
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
takePhotoThenToShowImg.setImageBitmap(imageBitmap);
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android拍照與圖片處理技巧總結(jié)》、《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android使用控件ImageView加載圖片的方法
- Android 自定義圓形頭像CircleImageView支持加載網(wǎng)絡(luò)圖片的實(shí)現(xiàn)代碼
- 詳解Android中Glide與CircleImageView加載圓形圖片的問(wèn)題
- android imageview圖片居中技巧應(yīng)用
- Android實(shí)現(xiàn)ImageView圖片雙擊放大及縮小
- Android中ImageView使用網(wǎng)絡(luò)圖片資源的方法
- Android開發(fā)之imageView圖片按比例縮放的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)ImageView陰影和圖層效果
- Android實(shí)現(xiàn)ImageView圖片縮放和拖動(dòng)
- Android自定義ImageView實(shí)現(xiàn)在圖片上添加圖層效果
相關(guān)文章
Android編程之TabWidget選項(xiàng)卡用法實(shí)例分析
這篇文章主要介紹了Android編程之TabWidget選項(xiàng)卡用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了TabWidget選項(xiàng)卡的具體實(shí)現(xiàn)技巧與使用注意事項(xiàng),需要的朋友可以參考下2015-12-12
Android中RecyclerView實(shí)現(xiàn)Item添加和刪除的代碼示例
本篇文章主要介紹了Android中RecyclerView實(shí)現(xiàn)Item添加和刪除的代碼示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09
android 應(yīng)用內(nèi)部懸浮可拖動(dòng)按鈕簡(jiǎn)單實(shí)現(xiàn)代碼
本篇文章主要介紹了android 應(yīng)用內(nèi)部懸浮可拖動(dòng)按鈕簡(jiǎn)單實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Android手機(jī)端小米推送Demo解析和實(shí)現(xiàn)方法
本篇文章主要是介紹了Android端小米推送Demo解析和實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10
Android Studio使用教程(五):Gradle命令詳解和導(dǎo)入第三方包
這篇文章主要介紹了Android Studio使用教程(五):Gradle命令詳解和導(dǎo)入第三方包,本文講解了導(dǎo)入Android Studio、Gradle常用命令等內(nèi)容,需要的朋友可以參考下2015-05-05
flutter ExpansionTile 層級(jí)菜單的實(shí)現(xiàn)
這篇文章主要介紹了flutter ExpansionTile 層級(jí)菜單的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

