相机意图返回小图片
我正在使用相机意图捕捉图片。这是我的代码,它的伟大工程:相机意图返回小图片
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
我onActivityResult看起来是这样的:
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap photo = (Bitmap) extras.get("data");
}
}
的问题是,虽然由相机拍摄的照片是480 * 800(我使用HTC Desire),返回的位图仅为194 * 324!
任何想法为什么发生这种情况,以及如何解决它?
谢谢!
阅读这个答案,当你使用阅读从额外Bitmap
得到完整的图像尺寸 How to capture an image and store it with the native Android Camera
,您将获得的图像
我有同样的问题的Thumbnail
,我发现了一个在google android网站上的示例:http://developer.android.com/training/camera/photobasics.html
如果我们想从 摄像机产生高质量图像我们不会“T有一个选择,但它保存到文件,然后使用:
- 所以首先像以前一样,我们需要创建一个静态的INT,这将是我们的requestCode:
公共静态final int CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE = 1777;
- 接下来,我们再次触发的意图来启动活动为结果:
-
最后,我们将接收的结果在onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) { //Check that request code matches ours: if (requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE) { //Get our saved file into a bitmap object: File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg"); Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700); } }
意图意图=新意图( “android.media.action.IMAGE_CAPTURE” );文件 file = new File(Environment.getExternalStorageDirectory()+ File.separator + “image.jpg”); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); startActivityForResult(intent, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);
在这里,我们实际上是传递一个URI作为额外的意图,以保存图像。
decodeSampledBitmapFromFile方法是:
public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight)
{ // BEST QUALITY MATCH
//First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize, Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
options.inPreferredConfig = Bitmap.Config.RGB_565;
int inSampleSize = 1;
if (height > reqHeight)
{
inSampleSize = Math.round((float)height/(float)reqHeight);
}
int expectedWidth = width/inSampleSize;
if (expectedWidth > reqWidth)
{
//if(Math.round((float)width/(float)reqWidth) > inSampleSize) // If bigger SampSize..
inSampleSize = Math.round((float)width/(float)reqWidth);
}
options.inSampleSize = inSampleSize;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
的培训相关相机权限添加到清单文件:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
希望它会帮助任何人解决问题的方式。因为它为我工作100%。
我跟着它,我成功地将图像存储到画廊。但图像存储也与质量差的图像..如何获得良好的质量? – Kasnady 2018-02-03 00:43:50