Android模拟器摄像头-网络摄像头方向 [英] Android emulator camera - webcam orientation

查看:618
本文介绍了Android模拟器摄像头-网络摄像头方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mac网络摄像头在模拟器中模拟Android前置摄像头.不幸的是,即使仿真器是纵向的,相机也似乎是横向的-截图.

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

我的模拟器配置: Nexus 5X,Android Nougat 7.1.1,API级别25,启动方向:纵向,前置摄像头:webcam0,后置摄像头:模拟

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?

推荐答案

我最终通过检测我是否在模拟器中运行来解决了这个问题:

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

这篇关于Android模拟器摄像头-网络摄像头方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆