Search in sources :

Example 6 with Size

use of com.journeyapps.barcodescanner.Size in project zxing-android-embedded by journeyapps.

the class CenterCropStrategy method getScore.

/**
     * Get a score for our size.
     *
     * Based on heuristics for penalizing scaling and cropping.
     *
     * 1.0 is perfect (exact match).
     * 0.0 means we can't use it at all.
     *
     * @param size the camera preview size (that can be scaled)
     * @param desired the viewfinder size
     * @return the score
     */
@Override
protected float getScore(Size size, Size desired) {
    if (size.width <= 0 || size.height <= 0) {
        return 0f;
    }
    Size scaled = size.scaleCrop(desired);
    // Scaling preserves aspect ratio
    float scaleRatio = scaled.width * 1.0f / size.width;
    // Treat downscaling as slightly better than upscaling
    float scaleScore;
    if (scaleRatio > 1.0f) {
        // Upscaling
        scaleScore = (float) Math.pow(1.0f / scaleRatio, 1.1);
    } else {
        // Downscaling
        scaleScore = scaleRatio;
    }
    // Ratio of scaledDimension / dimension.
    // Note that with scaleCrop, only one dimension is cropped.
    float cropRatio = scaled.width * 1.0f / desired.width + scaled.height * 1.0f / desired.height;
    // Cropping is bad, square it
    // 1.0 means no cropping. 50% cropping is 0.44f, 10% cropping is 0.82f
    float cropScore = 1.0f / cropRatio / cropRatio;
    return scaleScore * cropScore;
}
Also used : Size(com.journeyapps.barcodescanner.Size)

Example 7 with Size

use of com.journeyapps.barcodescanner.Size in project zxing-android-embedded by journeyapps.

the class CenterCropStrategy method scalePreview.

/**
     * Scale the preview to cover the viewfinder, then center it.
     *
     * Aspect ratio is preserved.
     *
     * @param previewSize the size of the preview (camera), in current display orientation
     * @param viewfinderSize the size of the viewfinder (display), in current display orientation
     * @return a rect placing the preview
     */
public Rect scalePreview(Size previewSize, Size viewfinderSize) {
    // We avoid scaling if feasible.
    Size scaledPreview = previewSize.scaleCrop(viewfinderSize);
    Log.i(TAG, "Preview: " + previewSize + "; Scaled: " + scaledPreview + "; Want: " + viewfinderSize);
    int dx = (scaledPreview.width - viewfinderSize.width) / 2;
    int dy = (scaledPreview.height - viewfinderSize.height) / 2;
    return new Rect(-dx, -dy, scaledPreview.width - dx, scaledPreview.height - dy);
}
Also used : Rect(android.graphics.Rect) Size(com.journeyapps.barcodescanner.Size)

Example 8 with Size

use of com.journeyapps.barcodescanner.Size in project zxing-android-embedded by journeyapps.

the class FitCenterStrategy method getScore.

/**
     * Get a score for our size.
     *
     * Based on heuristics for penalizing scaling and cropping.
     *
     * 1.0 is perfect (exact match).
     * 0.0 means we can't use it at all.
     *
     * @param size the camera preview size (that can be scaled)
     * @param desired the viewfinder size
     * @return the score
     */
@Override
protected float getScore(Size size, Size desired) {
    if (size.width <= 0 || size.height <= 0) {
        return 0f;
    }
    Size scaled = size.scaleFit(desired);
    // Scaling preserves aspect ratio
    float scaleRatio = scaled.width * 1.0f / size.width;
    // Treat downscaling as slightly better than upscaling
    float scaleScore;
    if (scaleRatio > 1.0f) {
        // Upscaling
        scaleScore = (float) Math.pow(1.0f / scaleRatio, 1.1);
    } else {
        // Downscaling
        scaleScore = scaleRatio;
    }
    // Ratio of scaledDimension / dimension.
    // Note that with scaleCrop, only one dimension is cropped.
    float cropRatio = (desired.width * 1.0f / scaled.width) * (desired.height * 1.0f / scaled.height);
    // Cropping is very bad, since it's used-visible for centerFit
    // 1.0 means no cropping.
    float cropScore = 1.0f / cropRatio / cropRatio / cropRatio;
    return scaleScore * cropScore;
}
Also used : Size(com.journeyapps.barcodescanner.Size)

Aggregations

Size (com.journeyapps.barcodescanner.Size)8 Rect (android.graphics.Rect)3 Camera (android.hardware.Camera)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1