This is an import of the code I found here.
The original code had trouble on some devices because the preview resolution was smaller than many modern phones can manage. This version calls getSupportedPreviewSizes to find the smallest supported preview size.
Please note that I am in no way a java programmer. I'm a python coder who last coded java ten years ago.
As far as I can determine, you need Eclipse to build this.