gpt4 book ai didi

来自图库方向的 Android 图片

转载 作者:行者123 更新时间:2023-11-30 01:41:08 34 4
gpt4 key购买 nike

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PICK_IMAGE_REQUEST && resultCode == getActivity().RESULT_OK && data != null && data.getData() != null) {

filePath = data.getData();
String wholeID = DocumentsContract.getDocumentId(filePath);

// Split at colon, use second item in the array
String id = wholeID.split(":")[1];

String[] column = { MediaStore.Images.Media.DATA };

// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";

Cursor cursor = getActivity().getContentResolver().
query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{id}, null);

int columnIndex = cursor.getColumnIndex(column[0]);

if (cursor.moveToFirst()) {
uploadFilePath = cursor.getString(columnIndex);
uploadFilePath = decodeFile(uploadFilePath, 512, 512);
}
cursor.close();

try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);

if(sendImageType.equals("profile")){
imgProfile.setImageBitmap(bitmap);
}
else if(sendImageType.equals("cover")){
imgCover.setImageBitmap(bitmap);

}

pDialog = ProgressDialog.show(getActivity(), "", "Uploading file...", true);

new Thread(new Runnable() {
public void run() {
uploadFile(uploadFilePath);
}
}).start();


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

解码文件:

private String decodeFile(String path,int DESIREDWIDTH, int DESIREDHEIGHT) {
String strMyImagePath = null;
Bitmap scaledBitmap = null;

try {
// Part 1: Decode image
Bitmap unscaledBitmap = ScalingUtilities.decodeFile(path, DESIREDWIDTH, DESIREDHEIGHT, ScalingUtilities.ScalingLogic.FIT);

if (!(unscaledBitmap.getWidth() <= DESIREDWIDTH && unscaledBitmap.getHeight() <= DESIREDHEIGHT)) {
// Part 2: Scale image
scaledBitmap = ScalingUtilities.createScaledBitmap(unscaledBitmap, DESIREDWIDTH, DESIREDHEIGHT, ScalingUtilities.ScalingLogic.FIT);
} else {
unscaledBitmap.recycle();
return path;
}

// Store to tmp file

String extr = Environment.getExternalStorageDirectory().toString();
File mFolder = new File(extr + "/TMMFOLDER");
if (!mFolder.exists()) {
mFolder.mkdir();
}

String s = "tmp.png";

File f = new File(mFolder.getAbsolutePath(), s);

strMyImagePath = f.getAbsolutePath();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 75, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {

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

e.printStackTrace();
}

scaledBitmap.recycle();
} catch (Throwable e) {
}

if (strMyImagePath == null) {
return path;
}
return strMyImagePath;

}

嗨,我有一个图像 uploader ,我正在将图像从 android 画廊上传到我的服务器。但是有些照片水平加载。我如何理解,照片是水平的还是垂直的,我该如何旋转。

上传后如下图:

enter image description here

最佳答案

检查图像的方向

ExifInterface ei = new ExifInterface(photoPath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
break;
case ExifInterface.ORIENTATION_ROTATE_180:
break;
// etc.
}

旋转图像使用

private Bitmap rotateImage(Bitmap source, float angle) {

Bitmap bitmap = null;
Matrix matrix = new Matrix();
matrix.postRotate(angle);
try {
bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
} catch (OutOfMemoryError err) {
err.printStackTrace();
}
return bitmap;
}

关于来自图库方向的 Android 图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34490827/

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