!8032 [MS][LITE]Improve interface display
Merge pull request !8032 from gongdaguo/fix_hidemopull/8032/MERGE
commit
b07d622655
@ -0,0 +1,136 @@
|
||||
package com.mindspore.himindspore.objectdetection.help;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Matrix;
|
||||
import android.media.ExifInterface;
|
||||
import android.net.Uri;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class BitmapUtils {
|
||||
private static final String TAG = "BitmapUtils";
|
||||
|
||||
public static void recycleBitmap(Bitmap... bitmaps) {
|
||||
for (Bitmap bitmap : bitmaps) {
|
||||
if (bitmap != null && !bitmap.isRecycled()) {
|
||||
bitmap.recycle();
|
||||
bitmap = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String getImagePath(Activity activity, Uri uri) {
|
||||
String[] projection = {MediaStore.Images.Media.DATA};
|
||||
Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
|
||||
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
||||
cursor.moveToFirst();
|
||||
return cursor.getString(columnIndex);
|
||||
}
|
||||
|
||||
public static Bitmap loadFromPath(Activity activity, int id, int width, int height) {
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inJustDecodeBounds = true;
|
||||
InputStream is = activity.getResources().openRawResource(id);
|
||||
int sampleSize = calculateInSampleSize(options, width, height);
|
||||
options.inSampleSize = sampleSize;
|
||||
options.inJustDecodeBounds = false;
|
||||
return zoomImage(BitmapFactory.decodeStream(is), width, height);
|
||||
}
|
||||
|
||||
public static Bitmap loadFromPath(Activity activity, Uri uri, int width, int height) {
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inJustDecodeBounds = true;
|
||||
|
||||
String path = getImagePath(activity, uri);
|
||||
BitmapFactory.decodeFile(path, options);
|
||||
int sampleSize = calculateInSampleSize(options, width, height);
|
||||
options.inSampleSize = sampleSize;
|
||||
options.inJustDecodeBounds = false;
|
||||
|
||||
Bitmap bitmap = zoomImage(BitmapFactory.decodeFile(path, options), width, height);
|
||||
return rotateBitmap(bitmap, getRotationAngle(path));
|
||||
}
|
||||
|
||||
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
|
||||
final int width = options.outWidth;
|
||||
final int height = options.outHeight;
|
||||
int inSampleSize = 1;
|
||||
|
||||
if (height > reqHeight || width > reqWidth) {
|
||||
// Calculate height and required height scale.
|
||||
final int heightRatio = Math.round((float) height / (float) reqHeight);
|
||||
// Calculate width and required width scale.
|
||||
final int widthRatio = Math.round((float) width / (float) reqWidth);
|
||||
// Take the larger of the values.
|
||||
inSampleSize = heightRatio > widthRatio ? heightRatio : widthRatio;
|
||||
}
|
||||
return inSampleSize;
|
||||
}
|
||||
|
||||
// Scale pictures to screen width.
|
||||
private static Bitmap zoomImage(Bitmap imageBitmap, int targetWidth, int maxHeight) {
|
||||
float scaleFactor =
|
||||
Math.max(
|
||||
(float) imageBitmap.getWidth() / (float) targetWidth,
|
||||
(float) imageBitmap.getHeight() / (float) maxHeight);
|
||||
Bitmap resizedBitmap =
|
||||
Bitmap.createScaledBitmap(
|
||||
imageBitmap,
|
||||
(int) (imageBitmap.getWidth() / scaleFactor),
|
||||
(int) (imageBitmap.getHeight() / scaleFactor),
|
||||
true);
|
||||
|
||||
return resizedBitmap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the rotation angle of the photo.
|
||||
*
|
||||
* @param path photo path.
|
||||
* @return angle.
|
||||
*/
|
||||
public static int getRotationAngle(String path) {
|
||||
int rotation = 0;
|
||||
try {
|
||||
ExifInterface exifInterface = new ExifInterface(path);
|
||||
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
|
||||
switch (orientation) {
|
||||
case ExifInterface.ORIENTATION_ROTATE_90:
|
||||
rotation = 90;
|
||||
break;
|
||||
case ExifInterface.ORIENTATION_ROTATE_180:
|
||||
rotation = 180;
|
||||
break;
|
||||
case ExifInterface.ORIENTATION_ROTATE_270:
|
||||
rotation = 270;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Failed to get rotation: " + e.getMessage());
|
||||
}
|
||||
return rotation;
|
||||
}
|
||||
|
||||
public static Bitmap rotateBitmap(Bitmap bitmap, int angle) {
|
||||
Matrix matrix = new Matrix();
|
||||
matrix.postRotate(angle);
|
||||
Bitmap result = null;
|
||||
try {
|
||||
result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
|
||||
} catch (OutOfMemoryError e) {
|
||||
Log.e(TAG, "Failed to rotate bitmap: " + e.getMessage());
|
||||
}
|
||||
if (result == null) {
|
||||
return bitmap;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,18 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".objectdetection.ui.PhotoActivity">
|
||||
android:background="@color/colorPrimary"
|
||||
android:fitsSystemWindows="true"
|
||||
android:keepScreenOn="true"
|
||||
android:orientation="vertical">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/img_photo"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:scaleType="fitCenter"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerInParent="true"
|
||||
android:scaleType="fitXY"/>
|
||||
</RelativeLayout>
|
Loading…
Reference in new issue