Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Corrupt JPEG data error



  • Hi,
    when I try capture image i get error:
    Corrupt JPEG data: premature end of data segment

    Note:
    Capture rate: 1shot/750ms
    Wait for read image 1 sec.
    Resolution 640x480

    I suppose it could be problem with libjpeg, but I don't know how to force use it in CMake project.



  • 2nd UPDATE:

    Problem was by natural source. The light reflects from window into camera.
    2nd problem was using file as buffer.

    I solved it by using grab image from QCameraViewFinder widget and send by memory.



  • A bit too generic.
    Can you show us some code?


  • Moderators

    @BartoszPaj

    I suppose it could be problem with libjpeg, but I don't know how to force use it in CMake project.

    first of all where does the JPEG data come from?
    second no need to do anything in your CMake project, since the jpeg codec/plugin is already compiled and shipped with Qt.
    Maybe you can save an example to a file to reproduce the issue?


  • Banned

    This post is deleted!


  • @VRonin

    // This is slot connected to QTimer (timeout 750ms)
    
    void FormAuthorizationPanel::takeQrPhoto()
    {
        camera->searchAndLock();
    
        QEventLoop loop;
       QObject::connect(imageCapture,SIGNAL(readyForCaptureChanged(bool)),&loop,SLOT(quit()));
            imageCapture->capture(QString(QCoreApplication::applicationDirPath()+"/"+"QrShot"));
        loop.exec();
    
        qrCaptureMan->grabQrStatus();
        camera->unlock();
    
    }
    
    void QrCaptureManager::grabQrStatus()
    {
        auto f = std::async(std::launch::async,&QrCaptureManager::readQrCode,this);
        qDebug() << "Waiting ...";
        std::future_status status;
        do{
            status = f.wait_for(std::chrono::seconds(1));
            if (status == std::future_status::deferred){
                qDebug() << "deferred\n";
            } else if (status == std::future_status::timeout) {
                qDebug() << "timeout\n";
            } else if (status == std::future_status::ready) {
                if(f.get()){
                    emit QrCodeStatus(true);
                    QrTimer->stop();
                }else{
                    emit QrCodeStatus(false);
                }
    
            }
        }while(status != std::future_status::ready);
    }
    
    bool QrCaptureManager::readQrCode()
    {
        QImage img;
        if(!img.load(QString(QCoreApplication::applicationDirPath()+"/QrShot.jpg"))){
            qDebug() << "Img load error";
            return false;
        }
        decoder = new QZXing();
        decoder->setDecoder(QZXing::DecoderFormat_QR_CODE);
    
        if(decoder->decodeImage(img).isEmpty()){
            qDebug() << "QrCode Failed";
            decoder->deleteLater();
            return false;
        }
        else{
            setUpCardInfo(decoder->decodeImage(img));
            qDebug() << "Qr read correct";
            decoder->deleteLater();
            return true;
        }
    }
    
    // This is how I setup camera 
    
    void FormAuthorizationPanel::setUpCamera(const QCameraInfo &cameraInfo)
    {
        delete imageCapture;
        delete camera;
        delete vfs;
    
        camera = new QCamera(cameraInfo);
        vfs = new QCameraViewfinderSettings();
        vfs->setResolution(640,480);
    
        camera->setViewfinder(ui->widget);
        camera->setViewfinderSettings(*vfs);
    
        imageCapture = new QCameraImageCapture(camera);
        camera->start();
        imageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
        ui->widget->update();
        cameraStatus = true;
    }
    
    

    @raven-worx said in Corrupt JPEG data error:

    @BartoszPaj

    I suppose it could be problem with libjpeg, but I don't know how to force use it in CMake project.

    first of all where does the JPEG data come from?

    If I understand data came from QCameraImageCapture.

    @raven-worx said in Corrupt JPEG data error:

    second no need to do anything in your CMake project, since the jpeg codec/plugin is already compiled and shipped with Qt.
    Maybe you can save an example to a file to reproduce the issue?

    I suggested by this topic: https://stackoverflow.com/questions/33198296/qt-load-incomplete-jpeg-data-into-qpixmap-how-to-verify.
    No, I can't. That's all what I could show code ( Company Terms).



  • Why are you connecting to readyForCaptureChanged?
    try:

    QEventLoop loop;
    int captureID = -1;
    QObject::connect(imageCapture,&QCameraImageCapture::imageSaved,&loop,[&captureID,&loop](int capID)->void{
    if(capID==captureID)
    loop.quit();
    });
    captureID = imageCapture->capture(QString(QCoreApplication::applicationDirPath()+"/"+"QrShot"));
    loop.exec();
    


  • Before, when Timeout was shorter (<500ms) the app could crash. So I was created a loop for stop the camera.

    UPDATE: Thank you for advice. Now I have much better synchro.
    Today I tested app in another device. I'm gonna suppose it could be fault by low quality camera.



  • This post is deleted!


  • 2nd UPDATE:

    Problem was by natural source. The light reflects from window into camera.
    2nd problem was using file as buffer.

    I solved it by using grab image from QCameraViewFinder widget and send by memory.