Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android emulator camera - webcam orientation

I am using my Mac webcam to simulate an Android front-camera in the emulator. Unfortunately the camera seems in landscape orientation - screenshot, even though the emulator is in portrait one.

Note that the camera behaves properly on a real device (i.e. it has a portrait orientation).

My Emulator configuration: Nexus 5X, Android Nougat 7.1.1, API Level 25, Startup Orientation: portrait, front-camera: webcam0, back-camera: Emulated

How can I use the webcam with proper orientation?

like image 448
Ohad Navon Avatar asked Apr 25 '17 08:04

Ohad Navon


People also ask

How do I access the camera of an Android device?

As Android's own Camera application does, the recommended way to access the camera is to open Camera on a separate thread that's launched from onCreate(). This approach is a good idea since it can take a while and might bog down the UI thread.

How to capture images and video using the Android framework?

The Android framework supports capturing images and video through the android.hardware.camera2 API or camera Intent. Here are the relevant classes: android.hardware.camera2 This package is the primary API for controlling device cameras.

Can apps running in the background use the camera on Android?

On Android 9 (API level 28) and later, apps running in the background cannot access the camera. Therefore, you should use the camera either when your app is in the foreground or as part of a foreground service.

Can I use camera features on my Android device?

The first thing to understand when setting out to use camera features on Android devices is that not all camera features are supported on all devices. In addition, devices that support a particular feature may support them to different levels or with different options.


1 Answers

Ultimately I solved this by detecting if I'm running in an emulator:

public static boolean isEmulator() {
  return Build.FINGERPRINT.startsWith("generic")
    || Build.FINGERPRINT.startsWith("unknown")
    || Build.MODEL.contains("google_sdk")
    || Build.MODEL.contains("Emulator")
    || Build.MODEL.contains("Android SDK built for x86")
    || Build.MANUFACTURER.contains("Genymotion")
    || (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
    || "google_sdk".equals(Build.PRODUCT);
}

And then applying a transform to the preview texture:

Matrix matrix = new Matrix();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point displaySize = new Point();
display.getRealSize(size);
RectF viewRect = new RectF(0, 0, mCameraPreview.getWidth(), mCameraPreview.getHeight());
float centerX = viewRect.centerX();
float centerY = viewRect.centerY();
float scale = (float) mCameraPreview.getWidth() / (float) displaySize.x;
matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(270, centerX, centerY);
mCameraPreview.setTransform(matrix);
like image 151
Ohad Navon Avatar answered Sep 28 '22 14:09

Ohad Navon