Android 이미지 해상도별 자동 비율 구하기

2020. 6. 29. 09:31Android

반응형
private ArrayList<Integer> maxWidth_maxHeight(Context context,Uri mSourceUri){
        ArrayList<Integer> ho = new ArrayList<>();
        BitmapFactory.Options options = new BitmapFactory.Options(); //비율 구하기
        options.inJustDecodeBounds = true;
        try {
            BitmapFactory.decodeStream(
                    context.getContentResolver().openInputStream(mSourceUri),
                    null,
                    options);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        int imageHeight = options.outHeight;
        int imageWidth = options.outWidth;

        float maxHeight = 0;
        float maxWidth = 0;

        float minRatio = 3.0f / 4.0f; //3:4 자신이 하고싶은 비율 넣기
        float maxRatio = 16.0f / 9.0f; // 16:9
       
        float ratio = (float)imageWidth / (float)imageHeight;


        if (ratio < minRatio) {
            maxWidth = (float)imageWidth;
            maxHeight = (float)imageWidth / minRatio;

        } else if (ratio > maxRatio) {
            maxWidth =  (float)imageHeight * maxRatio;
            maxHeight = (float)imageHeight;

        } else {
             maxWidth =  (float)imageWidth;
            maxHeight = (float)imageHeight;
        }

        ho.add((int)maxWidth);
        ho.add((int)maxHeight);

        return ho;
    }
반응형