Android emulator camera - webcam orientation

前端 未结 2 1203
悲&欢浪女
悲&欢浪女 2021-02-14 04:31

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 i

2条回答
  •  情话喂你
    2021-02-14 05:30

    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);
    

提交回复
热议问题