What is the proper solution to process the frame of camera on android?



  • Qt5.9.1
    os : win 10 64bits
    Android sdk : 19
    Phone : GTS-6310
    android version : 4.1.2

    I am trying to process camera frame of android by QCamera and QAbstractVideoSurface. What I try

    class camera_frame_grabber : public QAbstractVideoSurface
    {
        Q_OBJECT
    public:
        camera_frame_grabber(QObject *parent = nullptr);
    
        QList<QVideoFrame::PixelFormat>
        supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const override;
    
        bool present(const QVideoFrame &frame) override;
    
    signals:
        void frame_available(QImage frame);
    };
    
    camera_frame_grabber::camera_frame_grabber(QObject *parent) :
        QAbstractVideoSurface(parent)
    {
    
    }
    
    QList<QVideoFrame::PixelFormat>
    camera_frame_grabber::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
    {
        Q_UNUSED(handleType);
        return QList<QVideoFrame::PixelFormat>()
                << QVideoFrame::Format_ARGB32
                << QVideoFrame::Format_ARGB32_Premultiplied
                << QVideoFrame::Format_RGB32
                << QVideoFrame::Format_RGB24
                << QVideoFrame::Format_RGB565
                << QVideoFrame::Format_RGB555
                << QVideoFrame::Format_ARGB8565_Premultiplied
                << QVideoFrame::Format_BGRA32
                << QVideoFrame::Format_BGRA32_Premultiplied
                << QVideoFrame::Format_BGR32
                << QVideoFrame::Format_BGR24
                << QVideoFrame::Format_BGR565
                << QVideoFrame::Format_BGR555
                << QVideoFrame::Format_BGRA5658_Premultiplied
                << QVideoFrame::Format_AYUV444
                << QVideoFrame::Format_AYUV444_Premultiplied
                << QVideoFrame::Format_YUV444
                << QVideoFrame::Format_YUV420P
                << QVideoFrame::Format_YV12
                << QVideoFrame::Format_UYVY
                << QVideoFrame::Format_YUYV
                << QVideoFrame::Format_NV12
                << QVideoFrame::Format_NV21
                << QVideoFrame::Format_IMC1
                << QVideoFrame::Format_IMC2
                << QVideoFrame::Format_IMC3
                << QVideoFrame::Format_IMC4
                << QVideoFrame::Format_Y8
                << QVideoFrame::Format_Y16
                << QVideoFrame::Format_Jpeg
                << QVideoFrame::Format_CameraRaw
                << QVideoFrame::Format_AdobeDng;
    }
    
    bool camera_frame_grabber::present(const QVideoFrame &frame)
    {
        if(frame.isValid()) {        
            QVideoFrame clone_frame(frame);
            //if I do not call map, the app wouldn't crash
            clone_frame.map(QAbstractVideoBuffer::ReadOnly);
            clone_frame.unmap();
        }
        return false;
    }
    

    I use the frame buffer as following

    camera_frame_grabber frame_grabber;
    QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
    for(QCameraInfo const &cameraInfo : cameras) {    
        cam_.reset(new QCamera(cameraInfo));            
        cam_->setViewfinder(&frame_grabber_);                        
        pimpl_->cam_->start();
        break;
    }
    

    Always crash on android, but works perfect on windows. I guess this is another bug of android

    Link of complete codes



  • I forgot the error message, put at pastebin



  • I think Android may get confused with what formats it can handle. I'd try reducing your list of formats in supportedPixelFormats() to just Format_NV21



  • @mvuori I change it to Format_NV21, still crash as before, extracting a frame from camera is not an trivial job




Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.