calculate proportional layout dimension for displaying a camera preview according to a given camera preview size - Android android.hardware

Android examples for android.hardware:Camera Preview

Description

calculate proportional layout dimension for displaying a camera preview according to a given camera preview size

Demo Code

import android.hardware.Camera;
import java.util.List;

public class Main{

    /**/*from ww w.ja  v  a 2 s .  co m*/
     * calculate proportional layout dimension for displaying a camera preview according to a given camera preview size
     *
     * @param size       Camera preview size used {@see CameraUtils.getBestPreviewSize}
     * @param targetW    current width of the layout which will host camera preview
     * @param targetH    current height of the layout which will host camera preview
     * @param isPortrait indicate if current orientation is portrait since the camera sizes are all in landscape mode
     * @return dimension which matches camera preview size ratio
     */
    public static int[] getProportionalDimension(Camera.Size size,
            int targetW, int targetH, boolean isPortrait) {
        int[] adaptedDimension = new int[2];
        double previewRatio;

        if (isPortrait) {
            previewRatio = (double) size.height / size.width;
        } else {
            previewRatio = (double) size.width / size.height;
        }

        if (((double) targetW / targetH) > previewRatio) {
            adaptedDimension[0] = targetW;
            adaptedDimension[1] = (int) (adaptedDimension[0] / previewRatio);
        } else {
            adaptedDimension[1] = targetH;
            adaptedDimension[0] = (int) (adaptedDimension[1] * previewRatio);
        }

        return adaptedDimension;
    }

}

Related Tutorials