Camera.getParameters () возвращает ноль на вкладке Galaxy - PullRequest
2 голосов
/ 21 ноября 2011

Вот мой поверхностно-измененный код обработки событий:

    public void surfaceChanged(SurfaceHolder holder,
                               int format, int width,
                               int height) {
    Camera.Parameters parameters = camera.getParameters();
    Camera.Size size = getBestPreviewSize(width, height,
                parameters);
       //...
    }


    private Camera.Size getBestPreviewSize(int width, int height,
                                       Camera.Parameters parameters) {
        Camera.Size result = null;

        // it fails with NullPointerExceptiopn here,
        // when accessing "getSupportedPreviewSizes" method:
        // that means "parameters" is null
        for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
            ///...
        }
    }

Я инициализирую камеру следующим образом:

    @Override
    public void onResume() {
        super.onResume();
        camera = Camera.open();
    }

Эта проблема не возникает на моем Galaxy S Plus, также какпроизойдет на телефоне LG Optimus Black.Кто-нибудь думает, что здесь не так?

Ответы [ 3 ]

3 голосов
/ 23 ноября 2011

Я решил это.

parameters.getSupportedPreviewSizes()

Возвращает NULL на Galaxy Tab.Поэтому я просто проверяю, является ли он нулевым, и не устанавливаю новый размер предварительного просмотра в таком случае.К такому выводу я пришел после изучения стандартных источников приложений для камеры.

0 голосов
/ 21 ноября 2011

инициализация камеры во многом зависит от конкретного устройства.Например, определенное устройство Samsung GT5500 сообщает нулевое значение (ширина = 0, высота = 0) в качестве допустимого разрешения для предварительного просмотра, но вылетает весь телефон («жесткая» перезагрузка), если вы пытаетесь его использовать.Мы испытали это с помощью механизма дополненной реальности mixare (http://www.mixare.org), и это была PITA для отладки (так как у нас не было телефона и мы не могли воспроизвести ошибку на любом другом оборудовании).

Однако о получении«Правильный» размер предварительного просмотра вы можете посмотреть в нашем коде (это бесплатное приложение с открытым исходным кодом) на github. В файле: https://github.com/mixare/mixare/blob/master/src/org/mixare/MixView.java (строка 871 и далее)

        List<Camera.Size> supportedSizes = null;
        //On older devices (<1.6) the following will fail
        //the camera will work nevertheless
        supportedSizes = Compatibility.getSupportedPreviewSizes(parameters);

        //preview form factor
        float ff = (float)w/h;
        Log.d("Mixare", "Screen res: w:"+ w + " h:" + h + " aspect ratio:" + ff);

        //holder for the best form factor and size
        float bff = 0;
        int bestw = 0;
        int besth = 0;
        Iterator<Camera.Size> itr = supportedSizes.iterator();

        //we look for the best preview size, it has to be the closest to the
        //screen form factor, and be less wide than the screen itself
        //the latter requirement is because the HTC Hero with update 2.1 will
        //report camera preview sizes larger than the screen, and it will fail
        //to initialize the camera
        //other devices could work with previews larger than the screen though
        while(itr.hasNext()) {
            Camera.Size element = itr.next();
            //current form factor
            float cff = (float)element.width/element.height;
            //check if the current element is a candidate to replace the best match so far
            //current form factor should be closer to the bff
            //preview width should be less than screen width
            //preview width should be more than current bestw
            //this combination will ensure that the highest resolution will win
            Log.d("Mixare", "Candidate camera element: w:"+ element.width + " h:" + element.height + " aspect ratio:" + cff);
            if ((ff-cff <= ff-bff) && (element.width <= w) && (element.width >= bestw)) {
                bff=cff;
                bestw = element.width;
                besth = element.height;
            }
        } 
        Log.d("Mixare", "Chosen camera element: w:"+ bestw + " h:" + besth + " aspect ratio:" + bff);
        //Some Samsung phones will end up with bestw and besth = 0 because their minimum preview size is bigger then the screen size.
        //In this case, we use the default values: 480x320
        if ((bestw == 0) || (besth == 0)){
            Log.d("Mixare", "Using default camera parameters!");
            bestw = 480;
            besth = 320;
        }
        parameters.setPreviewSize(bestw, besth);

Как вы видите, мы не используем напрямую вызов getSupportedPreviewSizes класса Camera, но вместо этого добавили слой совместимости (код здесь: https://github.com/mixare/mixare/blob/master/src/org/mixare/Compatibility.java), потому что нам нужна была совместимость со старыми телефонами.Если вы хотите поддерживать старые версии Android, вы можете использовать метод класса Camera напрямую.

HTH Daniele

0 голосов
/ 21 ноября 2011

Похоже, что переменная камеры никогда не инициализировалась, поэтому вы вызываете getParameters () для null Попробуйте позвонить camera = Camera.open(); сначала

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...