android照相功能介绍

来源:互联网 发布:商业端游源码 编辑:程序博客网 时间:2024/05/02 01:48

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 = newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult
(takePictureIntent, actionCode);
}

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

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

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

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

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

storageDir= newFile(
   
Environment.getExternalStoragePublicDirectory(
       
Environment.DIRECTORY_PICTURES
   
),
    getAlbumName
()
);

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

storageDir= newFile (
   
Environment.getExternalStorageDirectory()
       
+ PICTURES_DIR
       
+ getAlbumName()
);

设置文件名:privateFile createImageFile()throws IOException{
   
// Create an image file name
   
String timeStamp =
       
new SimpleDateFormat("yyyyMMdd_HHmmss").format(newDate());
   
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。

privatevoid galleryAddPic(){
   
Intent mediaScanIntent = newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
   
File f =new File(mCurrentPhotoPath);
   
Uri contentUri =Uri.fromFile(f);
    mediaScanIntent
.setData(contentUri);
   
this.sendBroadcast(mediaScanIntent);
}

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

privatevoid setPic(){
   
// Get the dimensions of the View
   
int targetW = mImageView.getWidth();
   
int targetH = mImageView.getHeight();
 
   
// Get the dimensions of the bitmap
   
BitmapFactory.Options bmOptions= newBitmapFactory.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);

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 吃了地屈孕酮后月经不干不净怎么办 藕片用热水炒后变色了怎么办? 外汇延期收款忘了报告了怎么办 怀孕不小心吃了马生菜怎么办 高压锅的皮圈很容易坏是怎么办 华为应用市场账号密码忘记了怎么办 业主对我们提出批评意见时怎么办 向环保局投诉被公司发现了怎么办 在政务大厅上班被群众投诉怎么办 政府下发的文件通知不履行该怎么办 给私人老板开车不给工资怎么办 给个体老板开车不给工资怎么办 户口转走在人才市场的档案怎么办 外来媳妇转上海户口没有档案怎么办 公务员考试笔试差9分面试怎么办 想从事人事方面的工作没经验怎么办 教师资格考试后户籍转走认定怎么办 总经理离职了作为高管的我怎么办 企业换总经理想换供应商应该怎么办 换了晶振时间还快怎么办 职场两个人都想要你的情况下怎么办 造价起步工资太低又结婚了怎么办 3d保存时写入文件出错怎么办 无经验想在工地承包点小活怎么办 一级建造师挂靠后中标后怎么办 用360对系统修补漏洞很慢怎么办 如果美国和俄罗斯开战中国会怎么办 戴牙冠前临时补牙材料掉了怎么办 设备间在业主家里每次上锁怎么办 成都安全员证原件丢了并过期怎么办 记不施工员证书号了怎么办 优易学车学员版登录不了怎么办 先科移动dvd主板坏了怎么办 离职单位不出劳动解除书怎么办 离职后一级建造师注册证怎么办 京牌货车报废挂靠公司不给办怎么办 二建挂靠注册证书到期了怎么办 二建证书挂靠公司不给钱怎么办 二建拿到证书原单位不解锁怎么办 凯云软件清单锁定只读了怎么办 苹果笔记本鼠标触摸板没反应怎么办