android照相功能介绍

1.获得相机的权限。这样设置没有相机的设备将不能安装使用。

<manifest ... >
   
<uses-feature android:name="android.hardware.camera" />
    ...
</manifest ... >

可以使用android:required="false"设置,这样没有相机的设备也可以下载。这时我们需要在运行时调用hasSystemFeature(PackageManager.FEATURE.CAMERA);如果相机不可用,把相机功能取消。

2.拍照(使用照相应用程序)。

android通过启动一个intent来实现拍照的功能。这其中包括三个方面:intent本身,开启一个Activity和处理图像数据。

private void dispatchTakePictureIntent(int actionCode) {
   
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult
(takePictureIntent, actionCode);
}

我们还需要一个activity来处理这个intent,下面是检测是否有这么一个activity的方法:

public static boolean isIntentAvailable(Context context, String action) {
   
final PackageManager packageManager = context.getPackageManager();
   
final Intent intent = new Intent(action);
   
List<ResolveInfo> list =
            packageManager
.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
   
return list.size() > 0;
}

3.查看照片。获得相机应用拍摄的照片,返回的是Bitmap图像。下面的代码是获得并显示在ImageView中的代码。

private void handleSmallCameraPhoto(Intent intent) {
   
Bundle extras = intent.getExtras();
    mImageBitmap
= (Bitmap) extras.get("data");
    mImageView
.setImageBitmap(mImageBitmap);
}

4.保存图片。android相机应用程序将图片全尺寸保存。你需要提供具体位置,包括盘符,文件夹,文件名。在Android2.2后,我们很容易得到路径:

storageDir = new File(
   
Environment.getExternalStoragePublicDirectory(
       
Environment.DIRECTORY_PICTURES
   
),
    getAlbumName
()
);

早期版本你需要自己提供文件的目录:

storageDir = new File (
   
Environment.getExternalStorageDirectory()
       
+ PICTURES_DIR
       
+ getAlbumName()
);

设置文件名:private File createImageFile() throws IOException {
   
// Create an image file name
   
String timeStamp =
       
new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
   
String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
   
File image = File.createTempFile(
        imageFileName
,
        JPEG_FILE_SUFFIX
,
        getAlbumDir
()
   
);
    mCurrentPhotoPath
= image.getAbsolutePath();
   
return image;
}

通过Intent将文件位置传递给相机应用:

File f = createImageFile();
takePictureIntent
.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));

5.添加照片到Gallery。

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

6.解码图像尺寸。如果你的系统在现实几个图片后,内存使用紧张,可使用下面的技术:一中减少动态堆的数量的方法。

private void setPic() {
   
// Get the dimensions of the View
   
int targetW = mImageView.getWidth();
   
int targetH = mImageView.getHeight();
 
   
// Get the dimensions of the bitmap
   
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions
.inJustDecodeBounds = true;
   
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
   
int photoW = bmOptions.outWidth;
   
int photoH = bmOptions.outHeight;
 
   
// Determine how much to scale down the image
   
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
 
   
// Decode the image file into a Bitmap sized to fill the View
    bmOptions
.inJustDecodeBounds = false;
    bmOptions
.inSampleSize = scaleFactor;
    bmOptions
.inPurgeable = true;
 
   
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    mImageView
.setImageBitmap(bitmap);

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值