com.google.android.gms.common.images.Size Java Examples

The following examples show how to use com.google.android.gms.common.images.Size. 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 ETHWallet with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Selects the most suitable preview and picture size, given the desired width and height.
 * <p/>
 * 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 camera        the camera to select a preview size from
 * @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
 */
private static SizePair selectSizePair(Camera camera, int desiredWidth, int desiredHeight) {
    List<SizePair> validPreviewSizes = generateValidPreviewSizeList(camera);

    // 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.
    SizePair selectedPair = null;
    int minDiff = Integer.MAX_VALUE;
    for (SizePair sizePair : validPreviewSizes) {
        Size size = sizePair.previewSize();
        int diff = Math.abs(size.getWidth() - desiredWidth) +
                Math.abs(size.getHeight() - desiredHeight);
        if (diff < minDiff) {
            selectedPair = sizePair;
            minDiff = diff;
        }
    }

    return selectedPair;
}
 
Example #2
Source File: CameraSource.java    From VehicleInfoOCR with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Selects the most suitable preview and picture size, given the desired width and height.
 *
 * <p>Even though we only need to find 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 camera the camera to select a preview size from
 * @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
 */
private static SizePair selectSizePair(Camera camera, int desiredWidth, int desiredHeight) {
    List<SizePair> validPreviewSizes = generateValidPreviewSizeList(camera);

    // 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.
    SizePair selectedPair = null;
    int minDiff = Integer.MAX_VALUE;
    for (SizePair sizePair : validPreviewSizes) {
        Size size = sizePair.previewSize();
        int diff =
                Math.abs(size.getWidth() - desiredWidth) + Math.abs(size.getHeight() - desiredHeight);
        if (diff < minDiff) {
            selectedPair = sizePair;
            minDiff = diff;
        }
    }

    return selectedPair;
}
 
Example #3
Source File: CameraSourcePreview.java    From flutter_mobile_vision with MIT License 6 votes vote down vote up
private void startIfReady() throws IOException, SecurityException, MobileVisionException {
    if (startRequested && surfaceAvailable) {
        cameraSource.start(surfaceView.getHolder());
        if (overlay != null) {
            Size size = cameraSource.getPreviewSize();
            int min = Math.min(size.getWidth(), size.getHeight());
            int max = Math.max(size.getWidth(), size.getHeight());
            if (isPortraitMode()) {
                // Swap width and height sizes when in portrait, since it will be rotated by
                // 90 degrees
                overlay.setCameraInfo(min, max, cameraSource.getCameraFacing());
            } else {
                overlay.setCameraInfo(max, min, cameraSource.getCameraFacing());
            }
            overlay.clear();
        }
        startRequested = false;
    }
}
 
Example #4
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 #5
Source File: CameraSourcePreview.java    From VehicleInfoOCR with GNU General Public License v3.0 6 votes vote down vote up
@SuppressLint("MissingPermission")
private void startIfReady() throws IOException {
    if (startRequested && surfaceAvailable) {
        cameraSource.start(surfaceView.getHolder());
        if (overlay != null) {
            Size size = cameraSource.getPreviewSize();
            int min = Math.min(size.getWidth(), size.getHeight());
            int max = Math.max(size.getWidth(), size.getHeight());
            if (isPortraitMode()) {
                // Swap width and height sizes when in portrait, since it will be rotated by
                // 90 degrees
                overlay.setCameraInfo(min, max, cameraSource.getCameraFacing());
            } else {
                overlay.setCameraInfo(max, min, cameraSource.getCameraFacing());
            }
            overlay.clear();
        }
        startRequested = false;
    }
}
 
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 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 #8
Source File: CameraSource.java    From VehicleInfoOCR 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
 */
@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 #9
Source File: CameraSourcePreview.java    From esp-idf-provisioning-android with Apache License 2.0 6 votes vote down vote up
@RequiresPermission(Manifest.permission.CAMERA)
private void startIfReady() throws IOException, SecurityException {

    if (mStartRequested && mSurfaceAvailable) {

        mCameraSource.start(mSurfaceView.getHolder());

        if (mOverlay != null) {

            Size size = mCameraSource.getPreviewSize();
            int min = Math.min(size.getWidth(), size.getHeight());
            int max = Math.max(size.getWidth(), size.getHeight());

            if (isPortraitMode()) {
                // Swap width and height sizes when in portrait, since it will be rotated by
                // 90 degrees
                mOverlay.setCameraInfo(min, max, mCameraSource.getCameraFacing());
            } else {
                mOverlay.setCameraInfo(max, min, mCameraSource.getCameraFacing());
            }
            mOverlay.clear();
        }
        mStartRequested = false;
    }
}
 
Example #10
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 #11
Source File: CameraSourcePreview.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 6 votes vote down vote up
@RequiresPermission(Manifest.permission.CAMERA)
private void startIfReady() throws IOException, SecurityException {
    if (mStartRequested && mSurfaceAvailable) {
        mCameraSource.start(mSurfaceView.getHolder());
        if (mOverlay != null) {
            Size size = mCameraSource.getPreviewSize();
            int min = Math.min(size.getWidth(), size.getHeight());
            int max = Math.max(size.getWidth(), size.getHeight());
            if (isPortraitMode()) {
                // Swap width and height sizes when in portrait, since it will be rotated by
                // 90 degrees
                mOverlay.setCameraInfo(min, max, mCameraSource.getCameraFacing());
            } else {
                mOverlay.setCameraInfo(max, min, mCameraSource.getCameraFacing());
            }
            mOverlay.clear();
        }
        mStartRequested = false;
    }
}
 
Example #12
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 #13
Source File: CameraSource.java    From samples-android with Apache License 2.0 6 votes vote down vote up
/**
 * Selects the most suitable preview and picture size, given the desired width and height.
 * <p/>
 * 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 camera        the camera to select a preview size from
 * @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
 */
private static SizePair selectSizePair(Camera camera, int desiredWidth, int desiredHeight) {
    List<SizePair> validPreviewSizes = generateValidPreviewSizeList(camera);

    // 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.
    SizePair selectedPair = null;
    int minDiff = Integer.MAX_VALUE;
    for (SizePair sizePair : validPreviewSizes) {
        Size size = sizePair.previewSize();
        int diff = Math.abs(size.getWidth() - desiredWidth) +
                Math.abs(size.getHeight() - desiredHeight);
        if (diff < minDiff) {
            selectedPair = sizePair;
            minDiff = diff;
        }
    }

    return selectedPair;
}
 
Example #14
Source File: CameraSource.java    From mobikul-standalone-pos with MIT License 6 votes vote down vote up
/**
 * Selects the most suitable preview and picture size, given the desired width and height.
 * <p/>
 * 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 camera        the camera to select a preview size from
 * @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
 */
private static SizePair selectSizePair(Camera camera, int desiredWidth, int desiredHeight) {
    List<SizePair> validPreviewSizes = generateValidPreviewSizeList(camera);

    // 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.
    SizePair selectedPair = null;
    int minDiff = Integer.MAX_VALUE;
    for (SizePair sizePair : validPreviewSizes) {
        Size size = sizePair.previewSize();
        int diff = Math.abs(size.getWidth() - desiredWidth) +
                Math.abs(size.getHeight() - desiredHeight);
        if (diff < minDiff) {
            selectedPair = sizePair;
            minDiff = diff;
        }
    }

    return selectedPair;
}
 
Example #15
Source File: FlutterMobileVisionDelegate.java    From flutter_mobile_vision with MIT License 6 votes vote down vote up
private void launchStart() {
    Map<Integer, List> map = new HashMap<>();

    int[] cameras = new int[]{
            CameraSource.CAMERA_FACING_BACK,
            CameraSource.CAMERA_FACING_FRONT
    };

    for (int facing : cameras) {

        List<Size> sizeList = CameraSource.getSizesForCameraFacing(facing);

        List<Map<String, Object>> list = new ArrayList<>();
        for (Size size : sizeList) {
            Map<String, Object> ret = new HashMap<>();
            ret.put("width", size.getWidth());
            ret.put("height", size.getHeight());
            list.add(ret);
        }

        map.put(facing, list);
    }

    finishWithSuccess(map);
}
 
Example #16
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 #17
Source File: CameraSourcePreview.java    From Barcode-Reader with Apache License 2.0 6 votes vote down vote up
@RequiresPermission(Manifest.permission.CAMERA)
private void startIfReady() throws IOException, SecurityException {
    if (mStartRequested && mSurfaceAvailable) {
        mCameraSource.start(mSurfaceView.getHolder());
        if (mOverlay != null) {
            Size size = mCameraSource.getPreviewSize();
            int min = Math.min(size.getWidth(), size.getHeight());
            int max = Math.max(size.getWidth(), size.getHeight());
            if (isPortraitMode()) {
                // Swap width and height sizes when in portrait, since it will be rotated by
                // 90 degrees
                mOverlay.setCameraInfo(min, max, mCameraSource.getCameraFacing());
            } else {
                mOverlay.setCameraInfo(max, min, mCameraSource.getCameraFacing());
            }
            mOverlay.clear();
        }
        mStartRequested = false;
    }
}
 
Example #18
Source File: CameraSourcePreview.java    From flutter_barcode_scanner with MIT License 6 votes vote down vote up
@RequiresPermission(Manifest.permission.CAMERA)
private void startIfReady() throws IOException, SecurityException {
    if (mStartRequested && mSurfaceAvailable) {
        mCameraSource.start(mSurfaceView.getHolder());
        if (mOverlay != null) {
            Size size = mCameraSource.getPreviewSize();
            int min = Math.min(size.getWidth(), size.getHeight());
            int max = Math.max(size.getWidth(), size.getHeight());
            if (isPortraitMode()) {
                // Swap width and height sizes when in portrait, since it will be rotated by
                // 90 degrees
                mOverlay.setCameraInfo(min, max, mCameraSource.getCameraFacing());
            } else {
                mOverlay.setCameraInfo(max, min, mCameraSource.getCameraFacing());
            }
            mOverlay.clear();
        }
        mStartRequested = false;
    }
}
 
Example #19
Source File: CameraSource.java    From ETHWallet 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 #20
Source File: CameraSourcePreview.java    From Android-face-filters with Apache License 2.0 6 votes vote down vote up
private void startIfReady() throws IOException {
    if (mStartRequested && mSurfaceAvailable) {
        mCameraSource.start(mSurfaceView.getHolder());
        if (mOverlay != null) {
            Size size = mCameraSource.getPreviewSize();
            int min = Math.min(size.getWidth(), size.getHeight());
            int max = Math.max(size.getWidth(), size.getHeight());
            if (isPortraitMode()) {
                // Swap width and height sizes when in portrait, since it will be rotated by
                // 90 degrees
                mOverlay.setCameraInfo(min, max, mCameraSource.getCameraFacing());
            } else {
                mOverlay.setCameraInfo(max, min, mCameraSource.getCameraFacing());
            }
            mOverlay.clear();
        }
        mStartRequested = false;
    }
}
 
Example #21
Source File: CameraSource.java    From flutter_mobile_vision with MIT License 6 votes vote down vote up
/**
 * Selects the most suitable preview and picture size, given the desired width and height.
 * <p/>
 * 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 camera        the camera to select a preview size from
 * @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
 */
private static SizePair selectSizePair(Camera camera, int desiredWidth, int desiredHeight) {
    List<SizePair> validPreviewSizes = generateValidPreviewSizeList(camera);

    // 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.
    SizePair selectedPair = null;
    int minDiff = Integer.MAX_VALUE;
    for (SizePair sizePair : validPreviewSizes) {
        Size size = sizePair.previewSize();
        int diff = Math.abs(size.getWidth() - desiredWidth) +
                Math.abs(size.getHeight() - desiredHeight);
        if (diff < minDiff) {
            selectedPair = sizePair;
            minDiff = diff;
        }
    }

    return selectedPair;
}
 
Example #22
Source File: PreferenceUtils.java    From mlkit-material-android with Apache License 2.0 5 votes vote down vote up
@Nullable
public static CameraSizePair getUserSpecifiedPreviewSize(Context context) {
  try {
    String previewSizePrefKey = context.getString(R.string.pref_key_rear_camera_preview_size);
    String pictureSizePrefKey = context.getString(R.string.pref_key_rear_camera_picture_size);
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    return new CameraSizePair(
        Size.parseSize(sharedPreferences.getString(previewSizePrefKey, null)),
        Size.parseSize(sharedPreferences.getString(pictureSizePrefKey, null)));
  } catch (Exception e) {
    return null;
  }
}
 
Example #23
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 #24
Source File: CameraSource.java    From flutter_barcode_scanner with MIT License 5 votes vote down vote up
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;

    byte[] byteArray = new byte[bufferSize];
    ByteBuffer buffer = ByteBuffer.wrap(byteArray);
    if (!buffer.hasArray() || (buffer.array() != byteArray)) {
        throw new IllegalStateException("Failed to create valid buffer for camera source.");
    }

    mBytesToByteBuffer.put(byteArray, buffer);
    return byteArray;
}
 
Example #25
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 #26
Source File: CameraSource.java    From flutter_mobile_vision with MIT License 5 votes vote down vote up
public SizePair(Camera.Size previewSize,
                Camera.Size pictureSize) {
    mPreview = new Size(previewSize.width, previewSize.height);
    if (pictureSize != null) {
        mPicture = new Size(pictureSize.width, pictureSize.height);
    }
}
 
Example #27
Source File: CameraSource.java    From flutter_barcode_scanner with MIT License 5 votes vote down vote up
public SizePair(android.hardware.Camera.Size previewSize,
                android.hardware.Camera.Size pictureSize) {
    mPreview = new Size(previewSize.width, previewSize.height);
    if (pictureSize != null) {
        mPicture = new Size(pictureSize.width, pictureSize.height);
    }
}
 
Example #28
Source File: Utils.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 5 votes vote down vote up
public static Size[] sizeToSize(android.util.Size[] sizes) {
    Size[] size = new Size[sizes.length];
    for(int i=0; i<sizes.length; i++) {
        size[i] = new Size(sizes[i].getWidth(), sizes[i].getHeight());
    }
    return size;
}
 
Example #29
Source File: CameraSource.java    From ETHWallet with GNU General Public License v3.0 5 votes vote down vote up
public SizePair(Camera.Size previewSize,
                Camera.Size pictureSize) {
    mPreview = new Size(previewSize.width, previewSize.height);
    if (pictureSize != null) {
        mPicture = new Size(pictureSize.width, pictureSize.height);
    }
}
 
Example #30
Source File: CameraSource.java    From samples-android with Apache License 2.0 5 votes vote down vote up
public SizePair(Camera.Size previewSize,
                Camera.Size pictureSize) {
    mPreview = new Size(previewSize.width, previewSize.height);
    if (pictureSize != null) {
        mPicture = new Size(pictureSize.width, pictureSize.height);
    }
}