Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for cameras that do not have 640x480 in USBTest0 #175

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions libuvccamera/src/main/java/com/serenegiant/usb/UVCCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,23 @@ public void setPreviewSize(final int width, final int height, final int min_fps,
}
}

/**
* Get list of sizes for the current frame format
*/
public List<Size> getSupportedSizeList() {
final int type = (mCurrentFrameFormat > 0) ? 6 : 4;
return getSupportedSize(type, mSupportedSize);
}

/**
* Get list of sizes for a specific frame format
* @param frameFormat either FRAME_FORMAT_YUYV(0) or FRAME_FORMAT_MJPEG(1)
*/
public List<Size> getSupportedSizeList(int frameFormat) {
final int type = (frameFormat > 0) ? 6 : 4;
return getSupportedSize(type, mSupportedSize);
}

public static List<Size> getSupportedSize(final int type, final String supportedSize) {
final List<Size> result = new ArrayList<Size>();
if (!TextUtils.isEmpty(supportedSize))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@

import com.serenegiant.common.BaseActivity;
import com.serenegiant.usb.CameraDialog;
import com.serenegiant.usb.IFrameCallback;
import com.serenegiant.usb.Size;
import com.serenegiant.usb.USBMonitor;
import com.serenegiant.usb.USBMonitor.OnDeviceConnectListener;
import com.serenegiant.usb.USBMonitor.UsbControlBlock;
import com.serenegiant.usb.UVCCamera;

import java.nio.ByteBuffer;
import java.util.List;

public class MainActivity extends BaseActivity implements CameraDialog.CameraDialogParent {
private static final boolean DEBUG = true; // TODO set false when production
private static final String TAG = "MainActivity";
Expand Down Expand Up @@ -148,12 +153,60 @@ public void run() {
final UVCCamera camera = new UVCCamera();
camera.open(ctrlBlock);
if (DEBUG) Log.i(TAG, "supportedSize:" + camera.getSupportedSize());

List<Size> mjpeg_camera_sizes = camera.getSupportedSizeList(UVCCamera.FRAME_FORMAT_MJPEG);
List<Size> yuv_camera_sizes = camera.getSupportedSizeList(UVCCamera.FRAME_FORMAT_YUYV);

// Pick the size that is closest to our required resolution
int required_width = 640;
int required_height = 480;
int required_area = required_width * required_height;

int preview_width = 0;
int preview_height = 0;
int error = Integer.MAX_VALUE; // trying to get this as small as possible

for (Size s : mjpeg_camera_sizes) {
// calculate the area for each camera size
int s_area = s.width * s.height;
// calculate the difference between this size and the target size
int abs_error = Math.abs(s_area - required_area);
// check if the abs_error is smaller than what we have already
// then use the new size
if (abs_error < error){
preview_width = s.width;
preview_height = s.height;
error = abs_error;
}
}

try {
camera.setPreviewSize(UVCCamera.DEFAULT_PREVIEW_WIDTH, UVCCamera.DEFAULT_PREVIEW_HEIGHT, UVCCamera.FRAME_FORMAT_MJPEG);
camera.setPreviewSize(preview_width, preview_height, UVCCamera.FRAME_FORMAT_MJPEG);
} catch (final IllegalArgumentException e) {
try {
// fallback to YUV mode
camera.setPreviewSize(UVCCamera.DEFAULT_PREVIEW_WIDTH, UVCCamera.DEFAULT_PREVIEW_HEIGHT, UVCCamera.DEFAULT_PREVIEW_MODE);

// find closest matching size
// Pick the size that is closest to our required resolution
int yuv_preview_width = 0;
int yuv_preview_height = 0;
int yuv_error = Integer.MAX_VALUE; // trying to get this as small as possible

for (Size s : yuv_camera_sizes) {
// calculate the area for each camera size
int s_area = s.width * s.height;
// calculate the difference between this size and the target size
int abs_error = Math.abs(s_area - required_area);
// check if the abs_error is smaller than what we have already
// then use the new size
if (abs_error < yuv_error) {
yuv_preview_width = s.width;
yuv_preview_height = s.height;
yuv_error = abs_error;
}
}

camera.setPreviewSize(yuv_preview_width, yuv_preview_height, UVCCamera.FRAME_FORMAT_YUYV);
} catch (final IllegalArgumentException e1) {
camera.destroy();
return;
Expand Down