Example usage for android.graphics Camera.Size equals

List of usage examples for android.graphics Camera.Size equals

Introduction

In this page you can find the example usage for android.graphics Camera.Size equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:com.ezartech.ezar.videooverlay.ezAR.java

/**
 * Selects the most suitable preview and picture size, given the desired width and height.
 * <p/>//from ww w . ja  va2  s .  c o  m
 * Even though we may only need the preview size, it's necessary to find both the preview
 * size and the picture size of the camera together, because these need to have the same aspect
 * ratio.  On some hardware, if you would only set the preview size, you will get a distorted
 * image.
 *
 * @param preferredVideoSize  the best preview size
 * @param desiredWidth  the desired width of the camera preview frames
 * @param desiredHeight the desired height of the camera preview frames
 * @return the selected preview and picture size pair
 */
//code influenced by https://github.com/googlesamples/android-vision/blob/master/visionSamples/barcode-reader/app/src/main/java/com/google/android/gms/samples/vision/barcodereader/ui/camera/CameraSource.java
private static SizePair selectSizePair(Camera.Size preferredVideoSize,
        List<android.hardware.Camera.Size> supportedPreviewSizes,
        List<android.hardware.Camera.Size> supportedPictureSizes, int desiredWidth, int desiredHeight) {
    List<SizePair> validPreviewSizes = generateValidPreviewSizeList(supportedPreviewSizes,
            supportedPictureSizes);

    SizePair selectedPair = null;

    //strategy #1 - match aspect ratio exactly with scale of 1x or 2x
    float targetAspectRatio = (float) desiredWidth / (float) desiredHeight;
    int targetArea = desiredHeight * desiredWidth;
    for (SizePair sizePair : validPreviewSizes) {

        if (Math.abs(targetAspectRatio - sizePair.previewAspectRatio) < 0.05) {
            //exact aspect ratio match
            //ensure that sizePair

            if (sizePair.previewSizeArea <= targetArea) {
                return sizePair;
            }
        }
    }

    //strategy 2
    // The method for selecting the best size is to minimize the sum of the differences between
    // the desired values and the actual values for width and height.  This is certainly not the
    // only way to select the best size, but it provides a decent tradeoff between using the
    // closest aspect ratio vs. using the closest pixel area.
    int minDiff = Integer.MAX_VALUE;
    for (SizePair sizePair : validPreviewSizes) {

        //use camera's preferred video size if  possible
        if (preferredVideoSize != null && preferredVideoSize.equals(sizePair.previewSize)) {
            return sizePair;
        }

        if (supportedPictureSizes != null && sizePair.pictureSize == null) {
            //req'd picture size not avail for this previewSize; skip it
            continue;
        }

        //find largest previewSize w/ perimeter < desired perimeter
        Camera.Size size = sizePair.previewSize;
        int diff = (desiredWidth + desiredHeight) - (size.width + size.height);
        if (0 <= diff && diff < minDiff) {
            selectedPair = sizePair;
            minDiff = diff;
        }
    }

    return selectedPair;
}