您的位置:首页 > 移动开发 > Android开发

Android拍照或从相册选取以及裁剪

2016-08-23 15:41 295 查看
private void getPicFromPhoto() {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intent, PHOTO_REQUEST);
}

private void getPicFromCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//intent.putExtra("android.intent.extras.CAMERA_FACING", 1); // 调用前置摄像头
// 下面这句指定调用相机拍照后的照片存储的路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory(), "test.jpg")));
startActivityForResult(intent, CAMERA_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CAMERA_REQUEST:
switch (resultCode) {
case -1:// -1表示拍照成功
File file = new File(Environment.getExternalStorageDirectory()
+ "/test.jpg");
if (file.exists()) {
photoClip(Uri.fromFile(file));
}
break;
default:
break;
}
break;
case PHOTO_REQUEST:
if (data != null) {
photoClip(data.getData());
}
break;
case PHOTO_CLIP:
if (data != null) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
faceImage.setImageBitmap(photo);
}
}
break;
default:
break;
}

}

private void photoClip(Uri uri) {
// 调用系统中自带的图片剪裁
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, PHOTO_CLIP);
}


public static Bitmap resizeImage(String path,
int width, int height)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//不加载bitmap到内存中
BitmapFactory.decodeFile(path,options);
int outWidth = options.outWidth;
int outHeight = options.outHeight;
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inSampleSize = 1;

if (outWidth != 0 && outHeight != 0 && width != 0 && height != 0)
{
int sampleSize= (int) (Math.ceil((outWidth/width+outHeight/height)/2));
Logger.d( "sampleSize = " + sampleSize);
options.inSampleSize = 4;
}

options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}


将bitmap压缩到指定大小:

从Android 2.2开始系统新增了一个缩略图ThumbnailUtils类,位于framework包下的android.media.ThumbnailUtils位置,可以帮助我们从mediaprovider中获取系统中的视频或图片文件的缩略图,该类提供了三种静态方法可以直接调用获取。

/**
*
* 创建一个指定大小的缩略图
* @param source 源文件(Bitmap类型)
* @param width  压缩成的宽度
* @param height 压缩成的高度
*/
ThumbnailUtils.extractThumbnail(source, width, height);

/**
* 创建一个指定大小居中的缩略图
*
* @param source 源文件(Bitmap类型)
* @param width  输出缩略图的宽度
* @param height 输出缩略图的高度
* @param options 如果options定义为OPTIONS_RECYCLE_INPUT,则回收@param source这个资源文件
* (除非缩略图等于@param source)
*
*/
ThumbnailUtils.extractThumbnail(source, width, height, options);

/**
* 创建一张视频的缩略图
* 如果视频已损坏或者格式不支持可能返回null
*
* @param filePath 视频文件路径  如:/sdcard/android.3gp
* @param kind kind可以为MINI_KIND或MICRO_KIND
*
*/
ThumbnailUtils.createVideoThumbnail(filePath, kind);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 拍照