Java Code Examples for com.google.android.gms.common.images.Size#getHeight()

The following examples show how to use com.google.android.gms.common.images.Size#getHeight() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CameraSource.java    From trust-wallet-android-source with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates one buffer for the camera preview callback.  The size of the buffer is based off of
 * the camera preview size and the format of the camera image.
 *
 * @return a new preview buffer of the appropriate size for the current camera settings
 */
private byte[] createPreviewBuffer(Size previewSize) {
    int bitsPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.NV21);
    long sizeInBits = previewSize.getHeight() * previewSize.getWidth() * bitsPerPixel;
    int bufferSize = (int) Math.ceil(sizeInBits / 8.0d) + 1;

    //
    // NOTICE: This code only works when using play services v. 8.1 or higher.
    //

    // Creating the byte array this way and wrapping it, as opposed to using .allocate(),
    // should guarantee that there will be an array to work with.
    byte[] byteArray = new byte[bufferSize];
    ByteBuffer buffer = ByteBuffer.wrap(byteArray);
    if (!buffer.hasArray() || (buffer.array() != byteArray)) {
        // I don't think that this will ever happen.  But if it does, then we wouldn't be
        // passing the preview content to the underlying detector later.
        throw new IllegalStateException("Failed to create valid buffer for camera source.");
    }

    mBytesToByteBuffer.put(byteArray, buffer);
    return byteArray;
}
 
Example 2
Source File: CameraSource.java    From fast_qr_reader_view with MIT License 6 votes vote down vote up
/**
 * Creates one buffer for the camera preview callback. The size of the buffer is based off of the
 * camera preview size and the format of the camera image.
 *
 * @return a new preview buffer of the appropriate size for the current camera settings
 */
@SuppressLint("InlinedApi")
private byte[] createPreviewBuffer(Size previewSize) {
  int bitsPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.NV21);
  long sizeInBits = (long) previewSize.getHeight() * previewSize.getWidth() * bitsPerPixel;
  int bufferSize = (int) Math.ceil(sizeInBits / 8.0d) + 1;

  // Creating the byte array this way and wrapping it, as opposed to using .allocate(),
  // should guarantee that there will be an array to work with.
  byte[] byteArray = new byte[bufferSize];
  ByteBuffer buffer = ByteBuffer.wrap(byteArray);
  if (!buffer.hasArray() || (buffer.array() != byteArray)) {
    // I don't think that this will ever happen.  But if it does, then we wouldn't be
    // passing the preview content to the underlying detector later.
    throw new IllegalStateException("Failed to create valid buffer for camera source.");
  }

  bytesToByteBuffer.put(byteArray, buffer);
  return byteArray;
}
 
Example 3
Source File: CameraSource.java    From mlkit-material-android with Apache License 2.0 6 votes vote down vote up
/**
 * Creates one buffer for the camera preview callback. The size of the buffer is based off of the
 * camera preview size and the format of the camera image.
 *
 * @return a new preview buffer of the appropriate size for the current camera settings.
 */
private byte[] createPreviewBuffer(Size previewSize) {
  int bitsPerPixel = ImageFormat.getBitsPerPixel(IMAGE_FORMAT);
  long sizeInBits = (long) previewSize.getHeight() * previewSize.getWidth() * bitsPerPixel;
  int bufferSize = (int) Math.ceil(sizeInBits / 8.0d) + 1;

  // Creating the byte array this way and wrapping it, as opposed to using .allocate(),
  // should guarantee that there will be an array to work with.
  byte[] byteArray = new byte[bufferSize];
  ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray);
  if (!byteBuffer.hasArray() || (byteBuffer.array() != byteArray)) {
    // This should never happen. If it does, then we wouldn't be passing the preview content to
    // the underlying detector later.
    throw new IllegalStateException("Failed to create valid buffer for camera source.");
  }

  bytesToByteBuffer.put(byteArray, byteBuffer);
  return byteArray;
}
 
Example 4
Source File: CameraSource.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 6 votes vote down vote up
/**
 * Creates one buffer for the camera preview callback.  The size of the buffer is based off of
 * the camera preview size and the format of the camera image.
 *
 * @return a new preview buffer of the appropriate size for the current camera settings
 */
private byte[] createPreviewBuffer(Size previewSize) {
    int bitsPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.NV21);
    long sizeInBits = previewSize.getHeight() * previewSize.getWidth() * bitsPerPixel;
    int bufferSize = (int) Math.ceil(sizeInBits / 8.0d) + 1;

    //
    // NOTICE: This code only works when using play services v. 8.1 or higher.
    //

    // Creating the byte array this way and wrapping it, as opposed to using .allocate(),
    // should guarantee that there will be an array to work with.
    byte[] byteArray = new byte[bufferSize];
    ByteBuffer buffer = ByteBuffer.wrap(byteArray);
    if (!buffer.hasArray() || (buffer.array() != byteArray)) {
        // I don't think that this will ever happen.  But if it does, then we wouldn't be
        // passing the preview content to the underlying detector later.
        throw new IllegalStateException("Failed to create valid buffer for camera source.");
    }

    mBytesToByteBuffer.put(byteArray, buffer);
    return byteArray;
}
 
Example 5
Source File: CameraSource.java    From samples-android with Apache License 2.0 6 votes vote down vote up
/**
 * Creates one buffer for the camera preview callback.  The size of the buffer is based off of
 * the camera preview size and the format of the camera image.
 *
 * @return a new preview buffer of the appropriate size for the current camera settings
 */
private byte[] createPreviewBuffer(Size previewSize) {
    int bitsPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.NV21);
    long sizeInBits = previewSize.getHeight() * previewSize.getWidth() * bitsPerPixel;
    int bufferSize = (int) Math.ceil(sizeInBits / 8.0d) + 1;

    //
    // NOTICE: This code only works when using play services v. 8.1 or higher.
    //

    // Creating the byte array this way and wrapping it, as opposed to using .allocate(),
    // should guarantee that there will be an array to work with.
    byte[] byteArray = new byte[bufferSize];
    ByteBuffer buffer = ByteBuffer.wrap(byteArray);
    if (!buffer.hasArray() || (buffer.array() != byteArray)) {
        // I don't think that this will ever happen.  But if it does, then we wouldn't be
        // passing the preview content to the underlying detector later.
        throw new IllegalStateException("Failed to create valid buffer for camera source.");
    }

    mBytesToByteBuffer.put(byteArray, buffer);
    return byteArray;
}
 
Example 6
Source File: CameraSource.java    From esp-idf-provisioning-android with Apache License 2.0 6 votes vote down vote up
/**
 * Creates one buffer for the camera preview callback.  The size of the buffer is based off of
 * the camera preview size and the format of the camera image.
 *
 * @return a new preview buffer of the appropriate size for the current camera settings
 */
private byte[] createPreviewBuffer(Size previewSize) {
    int bitsPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.NV21);
    long sizeInBits = previewSize.getHeight() * previewSize.getWidth() * bitsPerPixel;
    int bufferSize = (int) Math.ceil(sizeInBits / 8.0d) + 1;

    //
    // NOTICE: This code only works when using play services v. 8.1 or higher.
    //

    // Creating the byte array this way and wrapping it, as opposed to using .allocate(),
    // should guarantee that there will be an array to work with.
    byte[] byteArray = new byte[bufferSize];
    ByteBuffer buffer = ByteBuffer.wrap(byteArray);
    if (!buffer.hasArray() || (buffer.array() != byteArray)) {
        // I don't think that this will ever happen.  But if it does, then we wouldn't be
        // passing the preview content to the underlying detector later.
        throw new IllegalStateException("Failed to create valid buffer for camera source.");
    }

    mBytesToByteBuffer.put(byteArray, buffer);
    return byteArray;
}
 
Example 7
Source File: CameraSource.java    From mobikul-standalone-pos with MIT License 6 votes vote down vote up
/**
 * Creates one buffer for the camera preview callback.  The size of the buffer is based off of
 * the camera preview size and the format of the camera image.
 *
 * @return a new preview buffer of the appropriate size for the current camera settings
 */
private byte[] createPreviewBuffer(Size previewSize) {
    int bitsPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.NV21);
    long sizeInBits = previewSize.getHeight() * previewSize.getWidth() * bitsPerPixel;
    int bufferSize = (int) Math.ceil(sizeInBits / 8.0d) + 1;

    //
    // NOTICE: This code only works when using play services v. 8.1 or higher.
    //

    // Creating the byte array this way and wrapping it, as opposed to using .allocate(),
    // should guarantee that there will be an array to work with.
    byte[] byteArray = new byte[bufferSize];
    ByteBuffer buffer = ByteBuffer.wrap(byteArray);
    if (!buffer.hasArray() || (buffer.array() != byteArray)) {
        // I don't think that this will ever happen.  But if it does, then we wouldn't be
        // passing the preview content to the underlying detector later.
        throw new IllegalStateException("Failed to create valid buffer for camera source.");
    }

    mBytesToByteBuffer.put(byteArray, buffer);
    return byteArray;
}
 
Example 8
Source File: CameraSource.java    From flutter_mobile_vision with MIT License 6 votes vote down vote up
/**
 * Creates one buffer for the camera preview callback.  The size of the buffer is based off of
 * the camera preview size and the format of the camera image.
 *
 * @return a new preview buffer of the appropriate size for the current camera settings
 */
private byte[] createPreviewBuffer(Size previewSize) {
    int bitsPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.NV21);
    long sizeInBits = previewSize.getHeight() * previewSize.getWidth() * bitsPerPixel;
    int bufferSize = (int) Math.ceil(sizeInBits / 8.0d) + 1;

    //
    // NOTICE: This code only works when using play services v. 8.1 or higher.
    //

    // Creating the byte array this way and wrapping it, as opposed to using .allocate(),
    // should guarantee that there will be an array to work with.
    byte[] byteArray = new byte[bufferSize];
    ByteBuffer buffer = ByteBuffer.wrap(byteArray);
    if (!buffer.hasArray() || (buffer.array() != byteArray)) {
        // I don't think that this will ever happen.  But if it does, then we wouldn't be
        // passing the preview content to the underlying detector later.
        throw new IllegalStateException("Failed to create valid buffer for camera source.");
    }

    bytesToByteBuffer.put(byteArray, buffer);
    return byteArray;
}
 
Example 9
Source File: CameraSource.java    From Barcode-Reader with Apache License 2.0 6 votes vote down vote up
/**
 * Creates one buffer for the camera preview callback.  The size of the buffer is based off of
 * the camera preview size and the format of the camera image.
 *
 * @return a new preview buffer of the appropriate size for the current camera settings
 */
private byte[] createPreviewBuffer(Size previewSize) {
    int bitsPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.NV21);
    long sizeInBits = previewSize.getHeight() * previewSize.getWidth() * bitsPerPixel;
    int bufferSize = (int) Math.ceil(sizeInBits / 8.0d) + 1;

    //
    // NOTICE: This code only works when using play services v. 8.1 or higher.
    //

    // Creating the byte array this way and wrapping it, as opposed to using .allocate(),
    // should guarantee that there will be an array to work with.
    byte[] byteArray = new byte[bufferSize];
    ByteBuffer buffer = ByteBuffer.wrap(byteArray);
    if (!buffer.hasArray() || (buffer.array() != byteArray)) {
        // I don't think that this will ever happen.  But if it does, then we wouldn't be
        // passing the preview content to the underlying detector later.
        throw new IllegalStateException("Failed to create valid buffer for camera source.");
    }

    mBytesToByteBuffer.put(byteArray, buffer);
    return byteArray;
}
 
Example 10
Source File: CameraSource.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 6 votes vote down vote up
/**
 * Creates one buffer for the camera preview callback.  The size of the buffer is based off of
 * the camera preview size and the format of the camera image.
 *
 * @return a new preview buffer of the appropriate size for the current camera settings
 */
private byte[] createPreviewBuffer(Size previewSize) {
    int bitsPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.NV21);
    long sizeInBits = previewSize.getHeight() * previewSize.getWidth() * bitsPerPixel;
    int bufferSize = (int) Math.ceil(sizeInBits / 8.0d) + 1;
    //
    // NOTICE: This code only works when using play services v. 8.1 or higher.
    //
    // Creating the byte array this way and wrapping it, as opposed to using .allocate(),
    // should guarantee that there will be an array to work with.
    byte[] byteArray = new byte[bufferSize];
    ByteBuffer buffer = ByteBuffer.wrap(byteArray);
    if (!buffer.hasArray() || (buffer.array() != byteArray)) {
        // I don't think that this will ever happen.  But if it does, then we wouldn't be
        // passing the preview content to the underlying detector later.
        throw new IllegalStateException("Failed to create valid buffer for camera source.");
    }
    mBytesToByteBuffer.put(byteArray, buffer);
    return byteArray;
}
 
Example 11
Source File: GraphicOverlay.java    From mlkit-material-android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the camera attributes for size and facing direction, which informs how to transform image
 * coordinates later.
 */
public void setCameraInfo(CameraSource cameraSource) {
  Size previewSize = cameraSource.getPreviewSize();
  if (Utils.isPortraitMode(getContext())) {
    // Swap width and height when in portrait, since camera's natural orientation is landscape.
    previewWidth = previewSize.getHeight();
    previewHeight = previewSize.getWidth();
  } else {
    previewWidth = previewSize.getWidth();
    previewHeight = previewSize.getHeight();
  }
}
 
Example 12
Source File: Camera2Source.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 5 votes vote down vote up
/**
 * We choose a video size with 3x4 aspect ratio. Also, we don't use sizes
 * larger than 1080p, since MediaRecorder cannot handle such a high-resolution video.
 *
 * @param choices The list of available sizes
 * @return The video size
 */
private static Size chooseVideoSize(Size[] choices) {
    for (Size size : choices) {
        if (size.getWidth() == size.getHeight() * 16 / 9) {
            return size;
        }
    }
    Log.e(TAG, "Couldn't find any suitable video size");
    return choices[0];
}
 
Example 13
Source File: Camera2Source.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 5 votes vote down vote up
/**
 * Given {@code choices} of {@code Size}s supported by a camera, choose the smallest one that
 * is at least as large as the respective texture view size, and that is at most as large as the
 * respective max size, and whose aspect ratio matches with the specified value. If such size
 * doesn't exist, choose the largest one that is at most as large as the respective max size,
 * and whose aspect ratio matches with the specified value.
 *
 * @param choices           The list of sizes that the camera supports for the intended output
 *                          class
 * @param textureViewWidth  The width of the texture view relative to sensor coordinate
 * @param textureViewHeight The height of the texture view relative to sensor coordinate
 * @param maxWidth          The maximum width that can be chosen
 * @param maxHeight         The maximum height that can be chosen
 * @param aspectRatio       The aspect ratio
 * @return The optimal {@code Size}, or an arbitrary one if none were big enough
 */
private static Size chooseOptimalSize(Size[] choices, int textureViewWidth, int textureViewHeight, int maxWidth, int maxHeight, Size aspectRatio) {

    // Collect the supported resolutions that are at least as big as the preview Surface
    List<Size> bigEnough = new ArrayList<>();
    // Collect the supported resolutions that are smaller than the preview Surface
    List<Size> notBigEnough = new ArrayList<>();
    int w = aspectRatio.getWidth();
    int h = aspectRatio.getHeight();
    for (Size option : choices) {
        if (option.getWidth() <= maxWidth && option.getHeight() <= maxHeight &&
                option.getHeight() == option.getWidth() * h / w) {
            if (option.getWidth() >= textureViewWidth &&
                    option.getHeight() >= textureViewHeight) {
                bigEnough.add(option);
            } else {
                notBigEnough.add(option);
            }
        }
    }

    // Pick the smallest of those big enough. If there is no one big enough, pick the
    // largest of those not big enough.
    if (bigEnough.size() > 0) {
        return Collections.min(bigEnough, new CompareSizesByArea());
    } else if (notBigEnough.size() > 0) {
        return Collections.max(notBigEnough, new CompareSizesByArea());
    } else {
        Log.e(TAG, "Couldn't find any suitable preview size");
        return choices[0];
    }
}
 
Example 14
Source File: CameraSourcePreview.java    From trust-wallet-android-source with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    int previewWidth = 320;
    int previewHeight = 240;
    if (mCameraSource != null) {
        Size size = mCameraSource.getPreviewSize();
        if (size != null) {
            previewWidth = size.getWidth();
            previewHeight = size.getHeight();
        }
    }

    // Swap width and height sizes when in portrait, since it will be rotated 90 degrees
    if (isPortraitMode()) {
        int tmp = previewWidth;
        previewWidth = previewHeight;
        previewHeight = tmp;
    }

    final int viewWidth = right - left;
    final int viewHeight = bottom - top;

    int childWidth;
    int childHeight;
    int childXOffset = 0;
    int childYOffset = 0;
    float widthRatio = (float) viewWidth / (float) previewWidth;
    float heightRatio = (float) viewHeight / (float) previewHeight;

    // To fill the view with the camera preview, while also preserving the correct aspect ratio,
    // it is usually necessary to slightly oversize the child and to crop off portions along one
    // of the dimensions.  We scale up based on the dimension requiring the most correction, and
    // compute a crop offset for the other dimension.
    if (widthRatio > heightRatio) {
        childWidth = viewWidth;
        childHeight = (int) ((float) previewHeight * widthRatio);
        childYOffset = (childHeight - viewHeight) / 2;
    } else {
        childWidth = (int) ((float) previewWidth * heightRatio);
        childHeight = viewHeight;
        childXOffset = (childWidth - viewWidth) / 2;
    }

    for (int i = 0; i < getChildCount(); ++i) {
        // One dimension will be cropped.  We shift child over or up by this offset and adjust
        // the size to maintain the proper aspect ratio.
        getChildAt(i).layout(
                -1 * childXOffset, -1 * childYOffset,
                childWidth - childXOffset, childHeight - childYOffset);
    }

    try {
        startIfReady();
    } catch (IOException e) {
        Log.e(TAG, "Could not start camera source.", e);
    } catch (SecurityException se) {
        Log.e(TAG, "Does not have permission to start the camera.", se);
    }
}
 
Example 15
Source File: CameraSourcePreview.java    From Android-face-filters with Apache License 2.0 4 votes vote down vote up
@Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int width = 320;
        int height = 240;
        if (mCameraSource != null) {
            Size size = mCameraSource.getPreviewSize();
            if (size != null) {
                width = size.getWidth();
                height = size.getHeight();
            }
        }

        // Swap width and height sizes when in portrait, since it will be rotated 90 degrees
        if (isPortraitMode()) {
            int tmp = width;
            width = height;
            height = tmp;
        }

        final int layoutWidth = right - left;
        final int layoutHeight = bottom - top;

        // Computes height and width for potentially doing fit width.
        int childWidth = layoutWidth;
        int childHeight = layoutHeight;
                //(int)(((float) layoutWidth / (float) width) * height);
/*
        // If height is too tall using fit width, does fit height instead.
        if (childHeight > layoutHeight) {
            childHeight = layoutHeight;
            childWidth = (int)(((float) layoutHeight / (float) height) * width);
        }*/

        for (int i = 0; i < getChildCount(); ++i) {
            getChildAt(i).layout(0, 0, childWidth, childHeight);
        }

        try {
            startIfReady();
        } catch (IOException e) {
            Log.e(TAG, "Could not start camera source.", e);
        }
    }
 
Example 16
Source File: CameraSourcePreview.java    From Barcode-Reader with Apache License 2.0 4 votes vote down vote up
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    int previewWidth = 320;
    int previewHeight = 240;
    if (mCameraSource != null) {
        Size size = mCameraSource.getPreviewSize();
        if (size != null) {
            previewWidth = size.getWidth();
            previewHeight = size.getHeight();
        }
    }

    // Swap width and height sizes when in portrait, since it will be rotated 90 degrees
    if (isPortraitMode()) {
        int tmp = previewWidth;
        previewWidth = previewHeight;
        previewHeight = tmp;
    }

    final int viewWidth = right - left;
    final int viewHeight = bottom - top;

    int childWidth;
    int childHeight;
    int childXOffset = 0;
    int childYOffset = 0;
    float widthRatio = (float) viewWidth / (float) previewWidth;
    float heightRatio = (float) viewHeight / (float) previewHeight;

    // To fill the view with the camera preview, while also preserving the correct aspect ratio,
    // it is usually necessary to slightly oversize the child and to crop off portions along one
    // of the dimensions.  We scale up based on the dimension requiring the most correction, and
    // compute a crop offset for the other dimension.
    if (widthRatio > heightRatio) {
        childWidth = viewWidth;
        childHeight = (int) ((float) previewHeight * widthRatio);
        childYOffset = (childHeight - viewHeight) / 2;
    } else {
        childWidth = (int) ((float) previewWidth * heightRatio);
        childHeight = viewHeight;
        childXOffset = (childWidth - viewWidth) / 2;
    }

    for (int i = 0; i < getChildCount(); ++i) {
        // One dimension will be cropped.  We shift child over or up by this offset and adjust
        // the size to maintain the proper aspect ratio.
        getChildAt(i).layout(
                -1 * childXOffset, -1 * childYOffset,
                childWidth - childXOffset, childHeight - childYOffset);
    }

    try {
        startIfReady();
    } catch (IOException e) {
        Log.e(TAG, "Could not start camera source.", e);
    }
}
 
Example 17
Source File: CameraSourcePreview.java    From mobikul-standalone-pos with MIT License 4 votes vote down vote up
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    int previewWidth = 320;
    int previewHeight = 240;
    if (mCameraSource != null) {
        Size size = mCameraSource.getPreviewSize();
        if (size != null) {
            previewWidth = size.getWidth();
            previewHeight = size.getHeight();
        }
    }

    // Swap width and height sizes when in portrait, since it will be rotated 90 degrees
    if (isPortraitMode()) {
        int tmp = previewWidth;
        previewWidth = previewHeight;
        previewHeight = tmp;
    }

    final int viewWidth = right - left;
    final int viewHeight = bottom - top;

    int childWidth;
    int childHeight;
    int childXOffset = 0;
    int childYOffset = 0;
    float widthRatio = (float) viewWidth / (float) previewWidth;
    float heightRatio = (float) viewHeight / (float) previewHeight;

    // To fill the view with the camera preview, while also preserving the correct aspect ratio,
    // it is usually necessary to slightly oversize the child and to crop off portions along one
    // of the dimensions.  We scale up based on the dimension requiring the most correction, and
    // compute a crop offset for the other dimension.
    if (widthRatio > heightRatio) {
        childWidth = viewWidth;
        childHeight = (int) ((float) previewHeight * widthRatio);
        childYOffset = (childHeight - viewHeight) / 2;
    } else {
        childWidth = (int) ((float) previewWidth * heightRatio);
        childHeight = viewHeight;
        childXOffset = (childWidth - viewWidth) / 2;
    }

    for (int i = 0; i < getChildCount(); ++i) {
        // One dimension will be cropped.  We shift child over or up by this offset and adjust
        // the size to maintain the proper aspect ratio.
        getChildAt(i).layout(
                -1 * childXOffset, -1 * childYOffset,
                childWidth - childXOffset, childHeight - childYOffset);
    }

    try {
        startIfReady();
    } catch (IOException e) {
        Log.e(TAG, "Could not start camera source.", e);
    } catch (SecurityException se) {
        Log.e(TAG, "Does not have permission to start the camera.", se);
    }
}
 
Example 18
Source File: CameraSourcePreview.java    From ETHWallet with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    int previewWidth = 320;
    int previewHeight = 240;
    if (mCameraSource != null) {
        Size size = mCameraSource.getPreviewSize();
        if (size != null) {
            previewWidth = size.getWidth();
            previewHeight = size.getHeight();
        }
    }

    // Swap width and height sizes when in portrait, since it will be rotated 90 degrees
    if (isPortraitMode()) {
        int tmp = previewWidth;
        previewWidth = previewHeight;
        previewHeight = tmp;
    }

    final int viewWidth = right - left;
    final int viewHeight = bottom - top;

    int childWidth;
    int childHeight;
    int childXOffset = 0;
    int childYOffset = 0;
    float widthRatio = (float) viewWidth / (float) previewWidth;
    float heightRatio = (float) viewHeight / (float) previewHeight;

    // To fill the view with the camera preview, while also preserving the correct aspect ratio,
    // it is usually necessary to slightly oversize the child and to crop off portions along one
    // of the dimensions.  We scale up based on the dimension requiring the most correction, and
    // compute a crop offset for the other dimension.
    if (widthRatio > heightRatio) {
        childWidth = viewWidth;
        childHeight = (int) ((float) previewHeight * widthRatio);
        childYOffset = (childHeight - viewHeight) / 2;
    } else {
        childWidth = (int) ((float) previewWidth * heightRatio);
        childHeight = viewHeight;
        childXOffset = (childWidth - viewWidth) / 2;
    }

    for (int i = 0; i < getChildCount(); ++i) {
        // One dimension will be cropped.  We shift child over or up by this offset and adjust
        // the size to maintain the proper aspect ratio.
        getChildAt(i).layout(
                -1 * childXOffset, -1 * childYOffset,
                childWidth - childXOffset, childHeight - childYOffset);
    }

    try {
        startIfReady();
    } catch (IOException e) {
        Log.e(TAG, "Could not start camera source.", e);
    } catch (SecurityException se) {
        Log.e(TAG, "Does not have permission to start the camera.", se);
    }
}
 
Example 19
Source File: CameraSourcePreview.java    From esp-idf-provisioning-android with Apache License 2.0 4 votes vote down vote up
@RequiresPermission(Manifest.permission.CAMERA)
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    int previewWidth = 320;
    int previewHeight = 240;
    if (mCameraSource != null) {
        Size size = mCameraSource.getPreviewSize();
        if (size != null) {
            previewWidth = size.getWidth();
            previewHeight = size.getHeight();
        }
    }

    // Swap width and height sizes when in portrait, since it will be rotated 90 degrees
    if (isPortraitMode()) {
        int tmp = previewWidth;
        previewWidth = previewHeight;
        previewHeight = tmp;
    }

    final int viewWidth = right - left;
    final int viewHeight = bottom - top;

    int childWidth;
    int childHeight;
    int childXOffset = 0;
    int childYOffset = 0;
    float widthRatio = (float) viewWidth / (float) previewWidth;
    float heightRatio = (float) viewHeight / (float) previewHeight;

    // To fill the view with the camera preview, while also preserving the correct aspect ratio,
    // it is usually necessary to slightly oversize the child and to crop off portions along one
    // of the dimensions.  We scale up based on the dimension requiring the most correction, and
    // compute a crop offset for the other dimension.
    if (widthRatio > heightRatio) {
        childWidth = viewWidth;
        childHeight = (int) ((float) previewHeight * widthRatio);
        childYOffset = (childHeight - viewHeight) / 2;
    } else {
        childWidth = (int) ((float) previewWidth * heightRatio);
        childHeight = viewHeight;
        childXOffset = (childWidth - viewWidth) / 2;
    }

    for (int i = 0; i < getChildCount(); ++i) {
        // One dimension will be cropped.  We shift child over or up by this offset and adjust
        // the size to maintain the proper aspect ratio.
        getChildAt(i).layout(
                -1 * childXOffset, -1 * childYOffset,
                childWidth - childXOffset, childHeight - childYOffset);
    }

    try {
        startIfReady();
    } catch (IOException e) {
        Log.e(TAG, "Could not start camera source.", e);
    }
}
 
Example 20
Source File: CameraSourcePreview.java    From VehicleInfoOCR with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    int width = 320;
    int height = 240;
    if (cameraSource != null) {
        Size size = cameraSource.getPreviewSize();
        if (size != null) {
            width = size.getWidth();
            height = size.getHeight();
        }
    }

    // Swap width and height sizes when in portrait, since it will be rotated 90 degrees
    if (isPortraitMode()) {
        int tmp = width;
        width = height;
        height = tmp;
    }

    final int layoutWidth = right - left;
    final int layoutHeight = bottom - top;

    // Computes height and width for potentially doing fit width.
    int childWidth;
    int childHeight;
    int childXOffset = 0;
    int childYOffset = 0;
    float widthRatio = (float) layoutWidth / (float) width;
    float heightRatio = (float) layoutHeight / (float) height;

    // To fill the view with the camera preview, while also preserving the correct aspect ratio,
    // it is usually necessary to slightly oversize the child and to crop off portions along one
    // of the dimensions.  We scale up based on the dimension requiring the most correction, and
    // compute a crop offset for the other dimension.
    if (widthRatio > heightRatio) {
        childWidth = layoutWidth;
        childHeight = (int) ((float) height * widthRatio);
        childYOffset = (childHeight - layoutHeight) / 2;
    } else {
        childWidth = (int) ((float) width * heightRatio);
        childHeight = layoutHeight;
        childXOffset = (childWidth - layoutWidth) / 2;
    }

    for (int i = 0; i < getChildCount(); ++i) {
        // One dimension will be cropped.  We shift child over or up by this offset and adjust
        // the size to maintain the proper aspect ratio.
        getChildAt(i).layout(
                -1 * childXOffset, -1 * childYOffset,
                childWidth - childXOffset, childHeight - childYOffset);
    }

    try {
        startIfReady();
    } catch (IOException e) {
        Log.e(TAG, "Could not start camera source.", e);
    }
}