You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UIAutomator横屏设备截图仍为竖屏?求无需旋转的解决方案

解决UIAutomator横屏截图显示竖屏的问题

这个问题我之前帮不少开发者解决过,UIAutomator默认的截图逻辑确实容易在横屏模式下生成竖屏状态的截图,不用手动旋转的话,有几个靠谱的解决办法:

方法1:利用takeScreenshot的旋转参数直接适配

UIAutomator的UiDevice提供了带旋转参数的截图方法,直接传入当前设备的屏幕旋转角度,就能生成和屏幕显示一致的截图,这是最直接的方案:

UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
// 获取当前设备的实际旋转角度
int currentRotation = device.getDisplayRotation();
// 指定保存路径
File screenshotFile = new File(Environment.getExternalStorageDirectory(), "landscape_screenshot.png");
// 第三个参数传入当前旋转角度,生成适配方向的截图
device.takeScreenshot(screenshotFile, 1.0f, currentRotation);

这里的currentRotation会返回当前屏幕的旋转状态(比如横屏时是Surface.ROTATION_90或ROTATION_270),截图方法会根据这个参数自动调整图片方向,无需后续处理。

方法2:设置图片EXIF旋转信息

有时候截图本身是横屏的,但图片查看器没有读取图片的EXIF旋转元数据,才会显示成竖屏。这种情况可以在截图后手动设置正确的EXIF方向:

UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
File screenshotFile = new File(Environment.getExternalStorageDirectory(), "screenshot_exif.png");
// 先获取原始截图
device.takeScreenshot(screenshotFile);

// 根据屏幕旋转设置EXIF方向
ExifInterface exif = new ExifInterface(screenshotFile.getAbsolutePath());
int rotation = device.getDisplayRotation();
int exifOrientation;
switch (rotation) {
    case Surface.ROTATION_0:
        exifOrientation = ExifInterface.ORIENTATION_NORMAL;
        break;
    case Surface.ROTATION_90:
        exifOrientation = ExifInterface.ORIENTATION_ROTATE_90;
        break;
    case Surface.ROTATION_180:
        exifOrientation = ExifInterface.ORIENTATION_ROTATE_180;
        break;
    case Surface.ROTATION_270:
        exifOrientation = ExifInterface.ORIENTATION_ROTATE_270;
        break;
    default:
        exifOrientation = ExifInterface.ORIENTATION_NORMAL;
}
exif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(exifOrientation));
exif.saveAttributes();

设置完成后,图片查看器会根据EXIF信息自动旋转图片,显示为正确的横屏状态。

方法3:手动旋转Bitmap截图

如果需要直接操作截图的Bitmap对象,也可以在获取截图后手动旋转:

UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
// 获取原始截图Bitmap
Bitmap originalBitmap = device.takeScreenshot();
int rotation = device.getDisplayRotation();

// 根据旋转角度创建变换矩阵
Matrix matrix = new Matrix();
switch (rotation) {
    case Surface.ROTATION_90:
        matrix.postRotate(90);
        break;
    case Surface.ROTATION_180:
        matrix.postRotate(180);
        break;
    case Surface.ROTATION_270:
        matrix.postRotate(270);
        break;
}

// 生成旋转后的Bitmap
Bitmap rotatedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(), originalBitmap.getHeight(), matrix, true);

// 保存旋转后的图片到文件
try (FileOutputStream outStream = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "rotated_screenshot.png"))) {
    rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
} catch (IOException e) {
    e.printStackTrace();
}

// 回收Bitmap避免内存泄漏
originalBitmap.recycle();
rotatedBitmap.recycle();

这个方法适合需要对截图做进一步处理的场景,比如裁剪、叠加元素等。

内容的提问来源于stack exchange,提问作者Shane Gleeson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:23:30