инициализация камеры во многом зависит от конкретного устройства.Например, определенное устройство 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