gpt4 book ai didi

android - 从相机获取完整质量的图像

转载 作者:太空狗 更新时间:2023-10-29 13:19:33 25 4
gpt4 key购买 nike

当我用相机拍照然后我想在 ImageView 中显示该图像时,我采用了下一个方法:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_IMAGE_REQUEST_FRAG:
if (resultCode == getActivity().RESULT_OK && data != null) {

Bitmap srcBmp = (Bitmap) data.getExtras().get("data");

... (process image to scale size and rotate if necesary)

pic_view.setImageBitmap(srcBmp);
}
}
}

我正在获取图像并将其正确显示在 ImageView 中,但我意识到获得的图像质量非常低。经过一番研究,我发现用这种方法得到的图像是拍摄图像的缩略图。所以我根据其他 SO 帖子的一些指示修改了代码:

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "Pictures/timeStamp.jpg";
takenPicUri = Uri.fromFile(new File(imageFilePath));
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, takenPicUri);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_IMAGE_REQUEST_FRAG:
if (resultCode == getActivity().RESULT_OK && data != null) {

Bitmap srcBmp = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, null);

... (process image to scale size and rotate if necesary)

pic_view.setImageBitmap(srcBmp);
}
}
}

但是现在,图像没有显示在 ImageView(pic_view) 中。在其他帖子中,我读到有人报告说这种方法对他们有用,但对我不起作用。我忘记了什么或者我做错了什么?

最佳答案

好吧,我在旧帖子中读到过,这种执行此操作的方法可能会引起麻烦,因为存在一些与之相关的错误,但这是我实现它的唯一方法。

就这么简单:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_IMAGE_REQUEST_FRAG:
if (resultCode == getActivity().RESULT_OK && data != null) {

Uri selectedImageUri = data.getData();
Bitmap srcBmp = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, null);

... (process image to scale size and rotate if necesary)

pic_view.setImageBitmap(srcBmp);
}
}
}

这种方式适用于 android 5.0 和 4.4.4。

关于android - 从相机获取完整质量的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30751334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com