ADB(Android Debug Bridge) + QT 控制安卓手机采集 /上传到上位机/QGraphicViews显示

1、ADB主要控制指令(可控制台直接测试)
"adb shell input keyevent 224" (点亮屏幕)
"adb shell input keyevent 82" (菜单键 = 解锁屏幕)
"adb shell am start -a android.media.action.STILL_IMAGE_CAMERA" (打开相机APP)
"adb shell input keyevent 27" (拍照)
"adb shell cd /sdcard/DCIM/Camera/"(切换到相册路径下)
"adb shell input keyevent "\"KEYCODE_F5\"" (刷新目录,灵活使用可提高pull效率)
"adb shell ls /sdcard/DCIM/Camera/ | head -n 1 | tr -d '\n' "(获取图库路径下所有子目录和图片文件)
"adb pull /sdcard/DCIM/Camera/" + myFileName + " " + target(点亮屏幕,@myFileName代表需要读取的图片名字,@target代表上位机的存储位置)
"adb shell input keyevent 3" (home键)
"adb shell input keyevent 223" (熄灭屏幕)
2、自动旋转问题
手机采集的jpg图片带有EXIF信息(陀螺仪输入旋转角度),可使得图像预览时根据手机的当前方位进行旋转。
QImage通过QImageReader间接读图。
可以利用QImageReader的setAutoTransform(true)属性来设置自动旋转,使得上位机视图与手机采集视图保持一致。
void ADBForm::on_btn_grabImage_clicked()
{
......//ADB采图部分跳过
QImageReader reader(picName);
reader.setAutoTransform(true);
QImage image = reader.read();
if (image.isNull())
{
qDebug() << QString("imread error");
return;
}
emit sigGetNewImage(image);//发送图像信号
}
3、显示视图居中和自动缩放问题
利用QGraphicViews和QGraphicScene配合完成
QGraphicsScene mScene; // 提前在.h中声明
ADBForm::ADBForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::ADBForm),
{
ui->setupUi(this);
......
ui->graphicsView->setScene(&mScene); // 将mScene与UI中的QGraphicView控件绑定
connect(this, &ADBForm::sigGetNewImage, this, &ADBForm::updateImage); // 将信号与槽绑定
}
void ADBForm::updateImage(QImage image) //显示槽函数
{
mScene.clear(); // 重要!!必须先清除,否则会造成mScene一直膨胀
mScene.addPixmap(QPixmap::fromImage(image)); // 将QImage转换为QPixmap,再加入到mScene中
mScene.setSceneRect(0, 0, image.width(), image.height()); // 设置mScene居中
ui->graphicsView->fitInView(ui->graphicsView->sceneRect(), Qt::KeepAspectRatio); // 锁定长宽比将mScence缩放为合适大小
}

浙公网安备 33010602011771号