gpt4 book ai didi

java - 尽管高度和宽度为 wrap_content,但 imageview 中显示的图像在 android 中太小

转载 作者:行者123 更新时间:2023-11-30 02:33:24 25 4
gpt4 key购买 nike

我正在做一个项目,我点击一个按钮打开相机并拍照,然后回到项目,它显示在 ImageView 中。我将图像保存到文件中,当我打开我手机目录中的图像显示为大尺寸。如何使图像在 ImageView 中显示得更大?

代码:

btncamera.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
captureImage();
}
});

private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode ==getActivity().RESULT_OK) {
// successfully captured the image
// display it in image view
//image_uri=data.getData().toString();
previewCapturedImage();
} else if (resultCode == getActivity().RESULT_CANCELED) {
// user cancelled Image capture
image_path=null;
Toast.makeText(getActivity(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getActivity(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}
}
private void previewCapturedImage() {
try {





// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();



options.inSampleSize = 4;

final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),options);
ExifInterface exif = null;
try {
exif = new ExifInterface(fileUri.getPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;

int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;

Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, options.outWidth, options.outHeight, matrix, true);

iv_preview.setImageBitmap(rotatedBitmap);

} catch (NullPointerException e) {
e.printStackTrace();
}
}

public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}

/*
* returning image / video
*/
private File getOutputMediaFile(int type) {

// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);

// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}

// Create a media file name
// String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
// Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {

//change image_path file name required for marina with the name of the file
mediaFile = new File(mediaStorageDir.getPath()+File.separator+user_id+"_"+job_object.job_id+"_"+task_id+".jpg");
// if(mediaFile.exists())
// {
// mediaFile.delete();
// mediaFile = new File(mediaStorageDir.getPath()+File.separator+user_id+"_"+job_object.job_id+"_"+task_id+".jpg");
// }
//IF YOU MAKE CHANGE TO THE FILE NAME PATH ALSO CHANGE IN loadpreviousimage method
image_path=mediaStorageDir.getPath() + File.separator+user_id+"_"+job_object.job_id+"_"+task_id+".jpg";
} else {
return null;
}

return mediaFile;
}

是不是因为这行 options.inSampleSize = 4;,我得到了一张小图片?

我该如何解决这个问题?

最佳答案

是的,如果 inSampleSize >1,它将对您的图像进行子采样;用于缩小非常大的图像。 Source here. 而且,你可以使用

ImageView 中的

android:scaleType="fitXY" 使您的图像适合您的 ImageView

关于java - 尽管高度和宽度为 wrap_content,但 imageview 中显示的图像在 android 中太小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26968851/

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