日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 图片变量,Android开发实现ImageView加载摄像头拍摄的大图功能

發布時間:2025/4/5 Android 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 图片变量,Android开发实现ImageView加载摄像头拍摄的大图功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實例講述了Android開發實現ImageView加載攝像頭拍攝的大圖功能。分享給大家供大家參考,具體如下:

這個方法是從官方demo中摘錄的,在此記錄學習。

權限

android:name="android.hardware.camera2"

android:required="false" />

設置變量保存文件存儲路徑

private String mCurrentPhotoPath;

/**

* 拍照flag

*/

private static final int REQUEST_IMAGE_CAPTURE_O = 2;

創建存儲路徑及文件名

/**

* 創建拍攝的圖片的存儲路徑及文件名

* @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);

}

}

處理并壓縮拍照結果,takePhotoThenToShowImg是一個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的數據庫中,以便圖庫或者其他程序讀取照片

/**

* 將拍攝到的照片添加到Media Provider的數據庫中

*/

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);

}

如果只需要縮略圖的話,只要調攝像頭拍攝直接處理結果就行

@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);

}

}

希望本文所述對大家Android程序設計有所幫助。

總結

以上是生活随笔為你收集整理的android 图片变量,Android开发实现ImageView加载摄像头拍摄的大图功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。