Qt 之 Camera 相机拍照并截图实现

背景

项目中,用到了拍照并截图,上传图片去后端拿题库答案,刚开始的实现是从屏幕中截图,然后保存成图片再上传,后来发现错了,因为是相当于屏幕截图,而实际上应该是拿相机拍照的图片做裁剪,来做后端的图片。项目中用到的是qml来实现。

类说明

window下相关类及使用

QCamera类
QCamera类是直接对应摄像头的类,包括如下方法:
1、QCamera()
创建摄像头类对象实例。

2、setViewfinder(viewfinder)
设置取景器,取景器就是将图像实时在屏幕显示,就跟相机的屏幕一样,参数是取景器类的实例对象。

3、setViewfinderSettings(viewFinderSettings)
设置取景器相关参数如分辨率等,请见QCameraViewfinderSettings类的介绍。

4、setCaptureMode(mode)
设置捕获图像(拍照)的保存模式,参数mode类型为枚举类型QCamera.CaptureModes,包括如下取值:

※ CaptureViewfinder:相机仅配置为显示取景器
※ CaptureStillImage:相机配置为静态帧捕获
※ CaptureVideo:相机配置为视频捕获

以上三个值可以通过or操作组合起来使用,经老猿验证,如果是拍照以上三个值都支持,CaptureVideo在windows系列操作系统下不支持。

5、start()打开相机
相机打开后取景器就能显示镜头范围内的内容。

6、stop()关闭相机
关闭相机后,取景器不再显示视频。

QCameraViewfinderSettings类
QCameraViewfinderSettings类是专门用于设置取景器参数的,常用的设置方法(读取方法名去掉set后首字母改为小写就可以了)如下:
1、setResolution(width,height)
以像素为单位设置取景器的分辨率。

2、setPixelAspectRatio(int horizontal, int vertical)
设置取景器的纵横比

3、setMinimumFrameRate(qreal rate)
设置取景器的最小帧速率(以每秒帧数为单位)

4、setMaximumFrameRate(qreal rate)
设置取景器的最大帧速率(以每秒帧数为单位)

5、setPixelFormat(QVideoFrame.PixelFormat format)
设置取景器图像的像素格式,即在内存中的存放格式(或编码方式),其类型为枚举类型enum QVideoFrame.PixelFormat

QCameraViewfinder类
QCameraViewfinder类是取景器对应类,这个类主要的方法就是构造方法,带一个参数指向取景器放置的父对象

QCameraImageCapture类
QCameraImageCapture是用于捕获图像的,主要有如下方法:
1、setCaptureDestination(CaptureDestinations destination)
setCaptureDestination方法设置捕获的图像是输出到文件还是在内存中缓存,参数destination是枚举类型CaptureDestinations ,有如下2个取值:
*※ CaptureToFile:对应值为 1,表示输出到文件
*※ CaptureToBuffer:,对应值为2,表示输出到缓存,可以在缓存中进一步处理
2、capture( QString filename )
capture从当前视频中捕获一帧作为图像保存,保存到参数指定的文件中。

常用代码:

查询和设置摄像头分辨率的API
QCamera::supportedViewfinderResolutions()
QCamera::setViewfinderSettings()
设置摄像头帧率、比例、分辨率、格式的类:QCameraViewfinderSettings

//打印相机支持的分辨率和帧率
for(int i=0;i< m_camera->supportedViewfinderResolutions().length();i++)
       qDebug() << "resolution " << m_camera->supportedViewfinderResolutions().at(i);

for(int i=0;i< m_camera->supportedViewfinderFrameRateRanges().length();i++)
        qDebug() << "framerate" << m_camera->supportedViewfinderFrameRateRanges().at(i).maximumFrameRate << m_camera->supportedViewfinderFrameRateRanges().at(i).minimumFrameRate;

//设置相机预览的分辨率
    QCameraViewfinderSettings set;
    set.setResolution(1280, 720);
    m_camera->setViewfinderSettings(set);

linux下相关类及使用

Camera类
VideoOutput类 用法基本等同于windows
常用代码:

	//打印支持的分辨率和帧率
	console.log("camera.supportedViewfinderFrameRateRanges()")
    var cl = camera.supportedViewfinderFrameRateRanges();
    for(var i = 0;i < cl.length;i++){
       console.log(cl[i].minimumFrameRate,cl[i].maximumFrameRate)
   }
 
   console.log("camera.supportedViewfinderResolutions()")
   var cl2 = camera.supportedViewfinderResolutions();
   for(var j = 0;j < cl2.length;j++){
      console.log(cl2[j].width,cl2[j].height)
   }
Camera {
        id: camera
        deviceId:QtMultimedia.availableCameras[0].deviceId
        captureMode: Camera.CaptureStillImage
        imageCapture {
            id:imgCapture
            onImageCaptured: {
            }
            onImageSaved:
            {
            }
        }
    }
    
{
   //测试分辨率
   //camera.viewfinder.resolution = Qt.size(2592, 1296);
   //camera.viewfinder.resolution = Qt.size(3264, 2448);
}

    VideoOutput {
        id: viewfinder
        anchors.fill: parent
        source: camera
        //autoOrientation: true
        orientation: 360
        MouseArea
        {
            anchors.fill: parent
            onClicked:
            {
            	//默认用法
            	//camera.imageCapture.capture()
            	//指定相机拍照后的路径
                camera.imageCapture.captureToLocation(filepath)
                var imagePath = filepath+".jpg";
            }
        }
    }

//videoOutput取景框的实际内容大小通过下面获得

 viewfinder.contentRect.width
 viewfinder.contentRect.height

使用注意事项

查询和设置摄像头分辨率时,需要在摄像头启动后调用,
即在调用QCamera::start()后,
可以使用QCamera::stateChanged(QCamera::State state)信号,
如果收到摄像头状态为QCamera::ActiveState后,再调用上述API

另外付:图片的裁剪实现

clipImage(const QString &fileName, const QString &newFilename,
                            const QRect rect, const int parewidth, const int pareheight)
{
    QImage img;
    if(!img.load(fileName)){
        return false;
    }

    int width = img.width();
    int height = img.height();

    int x = rect.x()*width/parewidth;
    int y = rect.y()*height/pareheight;
    int w = rect.width()*width/parewidth;
    int h = rect.height()*height/pareheight;

    qDebug() << rect << parewidth << pareheight;
    qDebug() << width << height << x<<y<<w<<h;

    QImage pimg = img.copy(x, y, w, h);
    bool b = pimg.save(newFilename);
    return  true;
}

另有截图实现,改天上传之csdn

你可能感兴趣的:(《Qt,项目实战经历全记录》,qt)