!9316 [MS][LITE]20201123 detection demo app program freeze after select 'PHOTO' 
	
		
	
				
					
				
			From: @sishuikang Reviewed-by: @zhanghaibo5,@zhang_xue_tong Signed-off-by: @zhanghaibo5pull/9316/MERGE
						commit
						af9be6bd69
					
				@ -0,0 +1,151 @@
 | 
				
			||||
/**
 | 
				
			||||
 * Copyright 2020 Huawei Technologies Co., Ltd
 | 
				
			||||
 * <p>
 | 
				
			||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||
 * you may not use this file except in compliance with the License.
 | 
				
			||||
 * You may obtain a copy of the License at
 | 
				
			||||
 * <p>
 | 
				
			||||
 * http://www.apache.org/licenses/LICENSE-2.0
 | 
				
			||||
 * <p>
 | 
				
			||||
 * Unless required by applicable law or agreed to in writing, software
 | 
				
			||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
				
			||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||
 * See the License for the specific language governing permissions and
 | 
				
			||||
 * limitations under the License.
 | 
				
			||||
 */
 | 
				
			||||
package com.mindspore.hiobject.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;
 | 
				
			||||
    }
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,48 @@
 | 
				
			||||
/**
 | 
				
			||||
 * Copyright 2020 Huawei Technologies Co., Ltd
 | 
				
			||||
 * <p>
 | 
				
			||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||
 * you may not use this file except in compliance with the License.
 | 
				
			||||
 * You may obtain a copy of the License at
 | 
				
			||||
 * <p>
 | 
				
			||||
 * http://www.apache.org/licenses/LICENSE-2.0
 | 
				
			||||
 * <p>
 | 
				
			||||
 * Unless required by applicable law or agreed to in writing, software
 | 
				
			||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
				
			||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||
 * See the License for the specific language governing permissions and
 | 
				
			||||
 * limitations under the License.
 | 
				
			||||
 */
 | 
				
			||||
package com.mindspore.hiobject.help;
 | 
				
			||||
 | 
				
			||||
import android.content.Context;
 | 
				
			||||
 | 
				
			||||
public class DisplayUtil {
 | 
				
			||||
 | 
				
			||||
    private DisplayUtil() {
 | 
				
			||||
        /* cannot be instantiated */
 | 
				
			||||
        throw new UnsupportedOperationException("cannot be instantiated");
 | 
				
			||||
    }
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
    public static int px2dip(Context context, float pxValue) {
 | 
				
			||||
        final float scale = context.getResources().getDisplayMetrics().density;
 | 
				
			||||
        return (int) (pxValue / scale + 0.5f);
 | 
				
			||||
    }
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
    public static int dip2px(Context context, float dipValue) {
 | 
				
			||||
        final float scale = context.getResources().getDisplayMetrics().density;
 | 
				
			||||
        return (int) (dipValue * scale + 0.5f);
 | 
				
			||||
    }
 | 
				
			||||
 | 
				
			||||
    public static int px2sp(Context context, float pxValue) {
 | 
				
			||||
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
 | 
				
			||||
        return (int) (pxValue / fontScale + 0.5f);
 | 
				
			||||
    }
 | 
				
			||||
 | 
				
			||||
    public static int sp2px(Context context, float spValue) {
 | 
				
			||||
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
 | 
				
			||||
        return (int) (spValue * fontScale + 0.5f);
 | 
				
			||||
    }
 | 
				
			||||
}
 | 
				
			||||
| 
		 After Width: | Height: | Size: 2.0 KiB  | 
| 
		 After Width: | Height: | Size: 3.4 KiB  | 
| 
		 After Width: | Height: | Size: 75 KiB  | 
@ -1,13 +1,15 @@
 | 
				
			||||
<?xml version="1.0" encoding="utf-8"?>
 | 
				
			||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 | 
				
			||||
    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=".objectdetect.PhotoActivity">
 | 
				
			||||
    android:fitsSystemWindows="true"
 | 
				
			||||
    android:keepScreenOn="true"
 | 
				
			||||
    android:orientation="vertical">
 | 
				
			||||
 | 
				
			||||
    <ImageView
 | 
				
			||||
        android:scaleType="fitXY"
 | 
				
			||||
        android:id="@+id/img_photo"
 | 
				
			||||
        android:layout_width="match_parent"
 | 
				
			||||
        android:layout_height="match_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