Coder Social home page Coder Social logo

Comments (3)

D-clock avatar D-clock commented on July 20, 2024

@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?

from androidstudycode.

hotstu avatar hotstu commented on July 20, 2024

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

from androidstudycode.

D-clock avatar D-clock commented on July 20, 2024

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

from androidstudycode.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.