gpt4 book ai didi

android捕获的图像是纵向的

转载 作者:太空宇宙 更新时间:2023-11-03 10:51:24 27 4
gpt4 key购买 nike

在我的应用程序中,我需要从我的手机图库页面上传一些图片。

我使用的是 Samsung Galaxy ace,我使用手机的默认摄像头以纵向模式拍摄了一些图像。捕获后,我在我的应用程序中打开了这些图像,并尝试在 ImageView 中显示它。在纵向模式下拍摄的图像在 ImageView 中似乎是横向的。

使用 exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION) 我检查图像方向值为 6。

使用以下代码,我在 ImageView 中以纵向模式显示图像,

 Matrix matrix = new Matrix();
matrix.postRotate(90);
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(HomePage._uri));
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
i.setImageBitmap(bitmap);

但是在上传图像并在我的应用程序的另一个 Activity 中检索它之后,它似乎再次处于横向模式。如何上传肖像本身的图像?

我已经用肖像拍摄了,我自己用肖像显示了它,在上传时我需要它本身是肖像的,这样当我检索它时我可以在肖像模式下查看它,

如何完成这项工作,(为了捕捉我在我的应用程序中使用相机,我使用手机默认相机捕捉,在应用程序之外)

最佳答案

我找到了从图库中获取图像并上传的解决方案。从图库中选择的某些图像可能看起来旋转了,在这种情况下,以下解决方案效果很好

从图库中选择图片

Intent intent = new Intent(Intent.ACTION_PICK,  android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, 2);

在onActivityResult中下一步

public void onActivityResult(int requestCode, int resultCode, final Intent data) 
{
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK )
{
f(requestCode == 2)
{
try
{
String [] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(data.getData(), proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
pathInput = cursor.getString(column_index);

Appconstants.f = Environment.getExternalStorageDirectory() + "/tmp_siva.jpg";
ImageUtils.resampleImageAndSaveToNewLocation(pathInput, Appconstants.f);
}
catch (Exception ex)
{
Log.e("Exception ex @ try catch",""+ex);
}
}
}
}

这是 ImageUtils 类

public class ImageUtils 
{
private ImageUtils()
{
}

public static void resampleImageAndSaveToNewLocation(String pathInput, String pathOutput) throws Exception
{
Bitmap bmp = resampleImage(pathInput, 800);

OutputStream out = new FileOutputStream(pathOutput);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
}

public static Bitmap resampleImage(String path, int maxDim) throws Exception
{
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, bfo);

BitmapFactory.Options optsDownSample = new BitmapFactory.Options();
optsDownSample.inSampleSize = getClosestResampleSize(bfo.outWidth, bfo.outHeight, maxDim);

Bitmap bmpt = BitmapFactory.decodeFile(path, optsDownSample);

Matrix m = new Matrix();

if (bmpt.getWidth() > maxDim || bmpt.getHeight() > maxDim)
{
BitmapFactory.Options optsScale = getResampling(bmpt.getWidth(), bmpt.getHeight(), maxDim);
m.postScale((float)optsScale.outWidth / (float)bmpt.getWidth(), (float)optsScale.outHeight / (float)bmpt.getHeight());
}

int sdk = new Integer(Build.VERSION.SDK).intValue();
if (sdk > 4)
{
int rotation = ExifUtils.getExifRotation(path);
if (rotation != 0)
{
m.postRotate(rotation);
}
}

return Bitmap.createBitmap(bmpt, 0, 0, bmpt.getWidth(), bmpt.getHeight(), m, true);
}

private static BitmapFactory.Options getResampling(int cx, int cy, int max)
{
float scaleVal = 1.0f;
BitmapFactory.Options bfo = new BitmapFactory.Options();
if (cx > cy)
{
scaleVal = (float)max / (float)cx;
}
else if (cy > cx)
{
scaleVal = (float)max / (float)cy;
}
else
{
scaleVal = (float)max / (float)cx;
}
bfo.outWidth = (int)(cx * scaleVal + 0.5f);
bfo.outHeight = (int)(cy * scaleVal + 0.5f);
return bfo;
}

private static int getClosestResampleSize(int cx, int cy, int maxDim)
{
/*Log.e("cx",""+cx);
Log.e("cy",""+cy);*/
int max = Math.max(cx, cy);

int resample = 1;
for (resample = 1; resample < Integer.MAX_VALUE; resample++)
{
if (resample * maxDim > max)
{
resample--;
break;
}
}

if (resample > 0)
{
return resample;
}
return 1;
}

public static BitmapFactory.Options getBitmapDims(String path) throws Exception
{
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, bfo);
return bfo;
}
}

这是 Exif 类

public class ExifUtils 
{
private ExifUtils()
{
}

public static int getExifRotation(String imgPath)
{
try
{
ExifInterface exif = new ExifInterface(imgPath);
String rotationAmount = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
if (!TextUtils.isEmpty(rotationAmount))
{
int rotationParam = Integer.parseInt(rotationAmount);
switch (rotationParam)
{
case ExifInterface.ORIENTATION_NORMAL:
return 0;
case ExifInterface.ORIENTATION_ROTATE_90:
return 90;
case ExifInterface.ORIENTATION_ROTATE_180:
return 180;
case ExifInterface.ORIENTATION_ROTATE_270:
return 270;
default:
return 0;
}
}
else
{
return 0;
}
}
catch (Exception ex)
{
return 0;
}
}
}

在图库中选择的图像已检查是纵向还是横向类型,并已旋转并保存在 sdcard 中的新路径中。为避免 OOM 问题,已调整其大小。

关于android捕获的图像是纵向的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12777386/

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