Android Camera API ISO Setting?

前端 未结 5 1846
一个人的身影
一个人的身影 2020-12-05 09:12

Would anyone know where to control the ISO setting for the camera from in the Android SDK ? It should be possible as the native camera application on the HTC Desire has ISO

5条回答
  •  醉酒成梦
    2020-12-05 09:31

    since I had the same problem of finding if the device has an ISO parameter I looked at this answer https://stackoverflow.com/a/23567103/3976589 and saw that @j.c had solved the problem for 8/18 devices by listing some parameters that he had found on different devices. Based on that listing I found that each paramter contains the words iso and values (sometimes only those words, sometimes something additional).

    So if I list all of the camera parameters and search for a strings that contain both words I will know what is the name of the ISO parameter, if it exists. Furthermore if the parameter exists I can take the supported ISO values and if I want to set one of those values i.e. change the camera parameter, I can just remove the -values at the end of the iso-values parameter and then I can change the ISO value successfully.

    I will now share my code for this task. First a snippet that retrieves a list with supported ISO values.

        private String ISOValuesParameter = null;
        private String ISOParameter = null;
        private String ISOValues = null;
    
    
        private void initCamera() {
            Camera mCamera = Camera.open();
    
            // this will list supported values
            String ISOvalues = getISOValues(); 
            textViewISO = (TextView) findViewById(R.id.viewISO);
            textViewISO.setText(ISOvalues);
    
        // setting Minimum ISO value
        if(ISOValuesParameter != null) {
            Camera.Parameters params = mCamera.getParameters();
    
            ISOParameter = ISOValuesParameter.replace("-values", "");
            params.set(ISOParameter, getMinISO());
    
            mCamera.setParameters(params);
    
            // get the updated ISO value
            params = mCamera.getParameters();
    
            String ISO = params.get(ISOParameter);
    
            Toast.makeText(this,"ISO set to: " + ISO, Toast.LENGTH_SHORT).show();
        }
    
        }
    
        // returns a list with supported ISO values
        private String getISOValues() {
                ISOValuesParamter = getISOValuesParameter();
                Camera.Parameters params = mCamera.getParameters();
                ISOValues = params.get(ISOValuesParamter);
    
                return ISOValues!=null ? ISOValues : "ISO not supported";
        }
    
        // this will return the name of the ISO parameter containing supported ISO values
        private String getISOValuesParameter() {
                Camera.Parameters params = mCamera.getParameters();
    
                String flatten = params.flatten();
                String[] paramsSeparated = flatten.split(";");
                for(String cur : paramsSeparated) {
                    if(cur.contains("iso") && cur.contains("values")) {
                        return cur.substring(0,cur.indexOf('='));
                    }
                }
    
                return null;
        }
    

    This snippet only lists the supported ISO values. In my application I needed to pick the lowest ISO. Here is my solution:

    private String getMinISO() {
        if(ISOValues == null) {
            return null;
        }
    
        String[] ISOarray = ISOValues.split(",");
        Arrays.sort(ISOarray, myComparator);
    
        String minISO = ISOarray[ISOarray.length-1];
    
        return minISO;
    }
    

    Here myComparator is a class that compares two strings and sorts the array in descending order. All alphabet words are at the beginning and all numbers are at the end. Here is my implementation:

    // Singelton class
    public class MyComparator implements  Comparator {
    
    private static MyComparator myComparator = null;
    
    private MyComparator() {}
    
    @Override
    public int compare(String a, String b) {
        return compareString(a,b);
    }
    
    public static int compareString(String a, String b) {
        if (a.length() > b.length())
            return -1;
        if (a.length() == b.length()) {
            if (a.compareTo(b) > 0)
                return -1;
            else if (a.compareTo(b) == 0)
                return 0;
        }
    
        return 1;
    }
    
    public static synchronized MyComparator getInstance() {
        if(myComparator==null) {
            myComparator = new MyComparator();
        }
    
        return myComparator;
    }
    
    }
    

    I hope my answer helps other people. :)

    Cheers! @ee3509

提交回复
热议问题