QT android例程中camera C++无法预览、取景的处理

 QT例程只能在 电脑正常 预览,取景,在ANDROID 能拍照,但不能预览、取景;

几年前写过修改 QtAndroid 的 JAVA 类, 其关联文件较多,在些的修改只要新建一个类就可以了,

 新建类   MyVideoSurface  

#ifndef MYVIDEOSURFACE_H
#define MYVIDEOSURFACE_H

#include <QAbstractVideoSurface>
#include <QDebug>
#include <QVideoSurfaceFormat>
#include <QVideoFrame>

class MyVideoSurface : public QAbstractVideoSurface
{
    Q_OBJECT
public:
    MyVideoSurface(QObject *parent = nullptr);

    QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType type = QAbstractVideoBuffer::NoHandle) const Q_DECL_OVERRIDE;
    bool isFormatSupported(const QVideoSurfaceFormat &) const Q_DECL_OVERRIDE;    //将视频流中像素格式转换成格式对等的图片格式,若无对等的格式,返回QImage::Format_Invalid
    bool start(const QVideoSurfaceFormat &) Q_DECL_OVERRIDE;                       //只要摄像头开,就会调用
    bool present(const QVideoFrame &) Q_DECL_OVERRIDE;                             //每一帧画面将回到这里处理
    void stop() Q_DECL_OVERRIDE;

signals:
    void frameAvailable(QVideoFrame cloneFrame);
};

#endif // MYVIDEOSURFACE_H




#include "myvideosurface.h"

MyVideoSurface::MyVideoSurface(QObject *parent) :QAbstractVideoSurface(parent)
{

}

//支持的像素格式
QList<QVideoFrame::PixelFormat> MyVideoSurface::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
{
    if(handleType == QAbstractVideoBuffer::NoHandle){
        return QList<QVideoFrame::PixelFormat>() << QVideoFrame::Format_RGB32
                                                 << QVideoFrame::Format_ARGB32
                                                 << QVideoFrame::Format_ARGB32_Premultiplied
                                                 << QVideoFrame::Format_RGB565
                                                 << QVideoFrame::Format_NV21
                                                 << QVideoFrame::Format_RGB555;
    }
    else {
        return QList<QVideoFrame::PixelFormat>();
    }
}

//将视频流中像素格式转换成格式对等的图片格式,若无对等的格式,返回QImage::Format_Invalid
bool MyVideoSurface::isFormatSupported(const QVideoSurfaceFormat &videoformat) const
{
    //imageFormatFromPixelFormat()-----返回与视频帧像素格式等效的图像格式
    //pixelFormat()-----返回视频流中帧的像素格式
    return QVideoFrame::imageFormatFromPixelFormat(videoformat.pixelFormat()) != QImage::Format_Invalid;
}

//这些虚函数,会自动被调用,start检测图像是否可以对等转换,每一帧有没有
bool MyVideoSurface::start(const QVideoSurfaceFormat &videoformat)
{ 
    QAbstractVideoSurface::start(videoformat);
    return false;
}

bool MyVideoSurface::present(const QVideoFrame &frame)
{
//    qDebug() << frame.size();
    if (frame.isValid()){
        QVideoFrame cloneFrame(frame);                                      //每一帧视频都会进入present中,内部机制
        emit frameAvailable(cloneFrame);                                    //直接把视频帧发送出去
        return true;
    }
    stop();
    return false;
}

void MyVideoSurface::stop()
{
    QAbstractVideoSurface::stop();
}

// 修改文件 #include "camera.h"

#include "myvideosurface.h"  // 新加的
....
class Camera : public QMainWindow
{
    Q_OBJECT

public:
    Camera();

....... 

private:
    MyVideoSurface *mySurface; // 新加的
};

#endif

// 修改文件 "camera.cpp" 

// 修改代码
void Camera::setCamera(const QCameraInfo &cameraInfo)
{
......

    connect(ui->exposureCompensation, &QAbstractSlider::valueChanged, this, &Camera::setExposureCompensation);

    m_camera->setViewfinder(mySurface);  // 修改处理

    connect(mySurface, SIGNAL(frameAvailable(QVideoFrame)), ui->viewfinder, SLOT(rcvFrame(QVideoFrame)), Qt::QueuedConnection);// 添加处理

    updateCameraState(m_camera->state());
    updateLockStatus(m_camera->lockStatus(), QCamera::UserRequest);

.......

    ui->captureWidget->setTabEnabled(1, (m_camera->isCaptureModeSupported(QCamera::CaptureVideo)));

    updateCaptureMode();

    m_camera->load();// 添加处理
    m_camera->start();

//    QList<QCameraViewfinderSettings> cvs= m_camera->supportedViewfinderSettings();
//    for(int i=0;i<cvs.size();i++)
//    {
//        qDebug()<<"CVS:"<<cvs[i].pixelFormat()<<cvs[i].resolution()<<cvs[i].pixelAspectRatio()<<cvs[i].minimumFrameRate()<<cvs[i].maximumFrameRate();
//    }

    //设置摄像头参数// 添加处理
    QCameraViewfinderSettings camerasettings = m_camera->viewfinderSettings();
  //  camerasettings.setResolution(1920,1080); // 手机处理能力不强时,不要设置过大
    camerasettings.setResolution(1280,720);
    camerasettings.setPixelFormat(QVideoFrame::Format_NV21);
    camerasettings.setMaximumFrameRate(30);
    camerasettings.setMinimumFrameRate(25);// 不要小于20,会不畅
    m_camera->setViewfinderSettings(camerasettings);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Qt是一个跨平台的应用程序开发框架,提供了丰富的库和工具,用于开发图形用户界面和其他应用程序。Qt Widget是Qt框架的一个模块,用于创建和管理图形界面元素,如窗口、按钮、输入框等。 在Qt,使用Widget编写一个简单的例程非常简单。首先,我们需要创建一个Qt Widget项目。然后,在主窗口添加各种需要的控件,例如按钮、文本框等。接下来,可以通过设置各个控件的属性和信号槽来实现交互逻辑。最后,编译并运行程序,即可看到创建的窗口和控件。 例如,我们可以编写一个计算器的例程。首先,在主窗口放置一些按钮和一个文本框,用于输入和显示计算结果。然后,编写相应的函数来实现按钮的点击事件。例如,当用户点击数字按钮时,将该数字添加到文本框;当用户点击运算符按钮时,将运算符添加到文本框。最后,编写一个计算函数来实现实际的计算逻辑,将计算结果显示到文本框。 通过以上步骤,我们就可以完成一个简单的Qt Widget例程。当用户运行程序时,会出现一个计算器界面,可以进行简单的数学运算。这只是一个简单的示例,Qt Widget可以用于开发各种类型的应用程序,例如文本编辑器、图像处理工具等。 总之,Qt Widget是Qt框架用于创建图形用户界面的模块,通过添加控件、处理事件和信号槽来实现交互逻辑,可以用于开发各种功能丰富的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值