gpt4 book ai didi

java - 用图库选择的图像比用相机拍摄的图像大得多

转载 作者:行者123 更新时间:2023-11-30 03:40:50 25 4
gpt4 key购买 nike

在我的应用中,用户要么从他们的图片库中选择一张图片,要么用他们的相机拍摄一张图片。

当您捕获它时,它会被添加到用户的图库中。

我发现这些图像除了是同一图像外,在尺寸上也有很大的变化。

捕获:33kb

代码:

    if (requestCode == TAKEIMAGE) {
System.out.println("TAKE IMAGE");
int photoLength = 0;
Bitmap photo = (Bitmap) data.getExtras().get("data");
photoLength = GetBitmapLength(photo);

SetSubmissionImage(photo, photoLength);
}

取自图库:1600kb

代码:

    else if (requestCode == CHOOSEIMAGE) {
System.out.println("CHOOSE IMAGE");

Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
try {
FileInputStream fileis=new FileInputStream(selectedImagePath);
BufferedInputStream bufferedstream=new BufferedInputStream(fileis);
byte[] bMapArray= new byte[bufferedstream.available()];
bufferedstream.read(bMapArray);

int photoLength = 0;
Bitmap photo = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);

bMapArray = null;
fileis.close();
bufferedstream.close();



//rotate to be portrait properly
try {
ExifInterface exif = new ExifInterface(selectedImagePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Log.e("EXIF", "Exif: " + orientation);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
}
else if (orientation == 3) {
matrix.postRotate(180);
}
else if (orientation == 8) {
matrix.postRotate(270);
}
photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true); // rotating bitmap
}
catch (Exception e) {

}

//photo = GetCompressedBitmap(photo);
photoLength = GetBitmapLength(photo);
SetSubmissionImage(photo, photoLength);

}

最佳答案

就像评论中的其他人所说的那样,您需要使用包含将保存图像的 uri 的 Intent 启动相机 Activity :

// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

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

然后在您的 onActivityResult() 方法中,您可以检索 uri 并从 Intent 中的 URI 加载图像。

关于java - 用图库选择的图像比用相机拍摄的图像大得多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15780272/

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