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);
- android照相功能介绍
- Android调用照相功能
- Android照相小功能
- Android 照相功能
- Android照相和录音功能的使用
- android 仿微信多图选择器(带预览、照相功能)
- Android照相和录音功能的使用
- Android系列之照相功能的实现
- Android简单调用相机Camera功能,实现打开照相功能
- 照相Android
- android照相
- android 照相
- android 照相
- android两种使用照相功能获取图片的方法
- android两种使用照相功能获取图片的方法
- Android中调用设备的照相和摄像功能
- android两种使用照相功能获取图片的方法
- android两种使用照相功能获取图片的方法
- 人手,人力,人才,人物
- OpenCV HOGDescriptor 参数图解
- C# sql获取datatimepicker 的值
- String 非事务方法做部分事务业务
- emacs 高级的 复制 剪切 粘贴
- android照相功能介绍
- 简述x264几种码率控制方式的实现
- 再培养一个扎克伯克:六款适合儿童上手编程的App
- 人物、人才、人手
- oracle listener
- HDB STUDIO安装时出错C++ command line option syntax error
- WebService之Axis2初学入门工程(详细)
- NYOJ 214 单调递增子序列二
- 雅安救援 小型起重设备尽显神通