1

I have a problem of proper implementation of camera preview. I looked over many answers provided here on StackOverflow and many tutorials. I created my own project to have it properly done.

https://bitbucket.org/rdkit/cameratutorial

I thought it works ok, but it turned out after checking this code on Sony Xperia SP that unfortunately it doesn't.

Can anyone look at this code and help me?

Screenshot from Moto G

Moto G

and Sony Xperia SP Sony Xperia SP

rdkit
  • 246
  • 3
  • 16
  • http://stackoverflow.com/questions/19577299/android-camera-preview-stretched – Fedor Kazakov Nov 06 '14 at 13:34
  • Thanks, but this is one of the sources that led me to write my own code. Maybe there is something I missed during implementation. That's why I provide source code, maybe someone can point out the mistake I make – rdkit Nov 06 '14 at 13:37
  • Yes you have to write you own method for selecting preview size – Fedor Kazakov Nov 06 '14 at 14:05
  • I use this magic method `getOptimalPreviewSize()`. On Moto G preview looks ok, but on Sony Xperia it doesn't. Moto G and Xperia has the same resolution 720x1280 and preview sizes returned by `mCamera.getParameters().getSupportedPreviewSizes()` are also the same. But why aspect ratio is not kept? – rdkit Nov 06 '14 at 14:13

2 Answers2

0

Camera PreviewSize is very tricky to set.

On some phones, the top status bar's height will not be taken into consideration, but other implementation do will take that height into consideration. And also the bottom virtual button area.

For eg, if the total screen's width = 1080 and height = 1920, then when the status bar's height and virtual button area's height are not be taken into consideration, if you set PreviewSize a ratio with 16:9, it will be look good. But! When the status bar's height and the virtaul button ares's height are taken into consideration, the available screen height will never get 1920, then the ratio in your screen will be different to the PreviewSize, and it will be stretched.

To solve the problem, you should implement your own getOptimalPreviewSize() method and choose the proper preview ratio according to your available screen height.

abcfrom0
  • 31
  • 2
0

Finally I found a solution by myself. I did not stop the preview before making changes.

As comments say in method

surfaceChanged()

If your preview can change or rotate, take care of those events here. Make sure to stop the preview before resizing or reformatting it.

So I needed to add

try {
    mCamera.stopPreview();
} catch (Exception e) {
    // ignore: tried to stop a non-existent preview
}
rdkit
  • 246
  • 3
  • 16