Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code in calculateInSampleSize method #3

Closed
hotstu opened this issue May 26, 2016 · 3 comments
Closed

code in calculateInSampleSize method #3

hotstu opened this issue May 26, 2016 · 3 comments

Comments

@hotstu
Copy link

hotstu commented May 26, 2016

` long totalPixels = width * height / inSampleSize;

        final long totalReqPixelsCap = reqWidth * reqHeight * 2;

        while (totalPixels > totalReqPixelsCap) {
            inSampleSize *= 2;
            totalPixels /= 2;
        }`

i dont quite understand the code here, maybe long totalPixels = width * height / inSampleSize; should be long totalPixels = width * height / (inSampleSize * inSampleSize )

@D-clock
Copy link
Owner

D-clock commented May 27, 2016

@hotstu

let me tell you ,

            long totalPixels = width * height / inSampleSize;
            final long totalReqPixelsCap = reqWidth * reqHeight * 2;
            while (totalPixels > totalReqPixelsCap) {
                inSampleSize *= 2;
                totalPixels /= 2;
            }

This code is to deal with a large picture of the length or width of the case.It is OR,not AND.So I chose the code —— long totalPixels = width * height / inSampleSize; Do you understand?

@hotstu
Copy link
Author

hotstu commented May 30, 2016

i wrote a compare

    public static int calculateInSampleSize(int rawW, int rawH, int reqWidth, int reqHeight) {
        final int height = rawH;
        final int width = rawW;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
            while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
            long totalPixels = width * height / inSampleSize;
            final long totalReqPixelsCap = reqWidth * reqHeight * 2;
            while (totalPixels > totalReqPixelsCap) {
                inSampleSize *= 2;
                totalPixels /= 2;
            }
        }
        return inSampleSize;
    }
    public static int calculateInSampleSize2(int rawW, int rawH, int reqWidth, int reqHeight) {
        final int height = rawH;
        final int width = rawW;
        int inSampleSize = 1;
        while ((height / inSampleSize) > reqHeight || (width / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
        return inSampleSize;
    }

    public static int calculateInSampleSize3(int rawW, int rawH, int reqWidth, int reqHeight) {
        final int height = rawH;
        final int width = rawW;
        int inSampleSize = 1;
        long totalPixels = width * height / (inSampleSize* inSampleSize);
        final long totalReqPixelsCap = reqWidth * reqHeight;
        while (totalPixels > totalReqPixelsCap) {
            inSampleSize *= 2;
            totalPixels = width * height / (inSampleSize* inSampleSize);
        }
        return inSampleSize;
    }
    public static void main(String[] args) {
        System.err.println(calculateInSampleSize(20, 1000, 100, 100));
        System.err.println(calculateInSampleSize2(20, 1000, 100, 100));
        System.err.println(calculateInSampleSize3(20, 1000, 100, 100));
        System.err.println("-----------");
        System.err.println(calculateInSampleSize(1000, 1000, 100, 100));
        System.err.println(calculateInSampleSize2(1000, 1000, 100, 100));
        System.err.println(calculateInSampleSize3(1000, 1000, 100, 100));
        System.err.println("-----------");
        System.err.println(calculateInSampleSize(1000, 10, 100, 100));
        System.err.println(calculateInSampleSize2(1000, 10, 100, 100));
        System.err.println(calculateInSampleSize3(1000, 10, 100, 100));
        System.err.println("-----------");
        System.err.println(calculateInSampleSize(10000, 10000, 100, 100));
        System.err.println(calculateInSampleSize2(10000, 10000, 100, 100));
        System.err.println(calculateInSampleSize3(10000, 10000, 100, 100));
    }

and the output:

1
16
2
-----------
64
16
16
-----------
1
16
1
-----------
8192
128
128

@D-clock
Copy link
Owner

D-clock commented Jun 8, 2016

@hotstu hi,friend!You need to use real photo data!

@hotstu hotstu closed this as completed Dec 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants