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

Why am i getting this error: corrupt jpeg data: premature end of data segment



  • Hello everyone. I convert some data I read from the database into a picture. Some pictures look fine but in others i get this error : corrupt jpeg data, and I can take a part of the picture. I think it was caused by pixel differences. Is the problem really caused by the pixel difference ? I would be happy if you help. Thank you. My codes are below :

    QSqlQuery qry(db);

    if(qry.exec("select pim.pers_image from pers_genel pg with (nolock) left join pers_genel_image pim with (nolock) on pim.pers_num=pg.pers_no where pg.pers_no=1"))

    {
    while(qry.next())
    {
    QByteArray dataarray=qry.value(0).toByteArray();
    QPıxmap outPıxmap=QPıxmap();
    outPıxmap.loadFromData(dataarray);
    ui->label_3->setPixmap(outPixmap);
    }
    }
    else
    {
    qDebug()<<db.lastError();
    }



  • @Mucahit what if you try that same query with some other external tool (i.e. query manager) and try opening the resulting file?
    Is it still corrupted?



  • No, file is opening. My server machine is Windows 10 operating system. The system I am working on is running Raspbian on raspberry Pi Compute Module 3 and above. Sorry i forgot to write above maybe it might work for you.



  • @Mucahit What I tried to say was that if you could test that same query in that same device/OS and check that the ending result file is really opening fine.

    The idea was to rule out that the image was somehow stored corrupted in the DB, and then your Qt app was not the problem.

    I'd say that given you'll be working with some resource constrained device, would it be possible to add a hash column to your DB so you can retrieve that value in your query and check that what you received is actually what's stored in the DB?



  • Frankly, I don't think there is a problem in the database part. Because when I low the pixel of the same image I want to take and reload it into the database, I show the picture very well in my qt application. so I thought it was a pixel problem but i'm not sure.



  • @Mucahit

    Have you seen this?

    https://wiki.qt.io/How_to_Store_and_Retrieve_Image_on_SQLite

    I would agree with @Pablo-J-Rogina. Try loading the image using an external tool (a quick google search reveals this: https://www.yohz.com/siv8_faq_view.htm) or using a simple webserver (here is some php code that should allow you to do something similar https://stackoverflow.com/questions/10850605/display-image-from-sql).

    If the image turns out badly/wrong, you know where the problem lies, if not then things would get tricky.



  • Yes, I already developed my project using the resource you sent. But as I said, I can take some pictures properly but usually i can take some bad. I did test and I get the bad pictures from an external machine and pictures are very good. I think that when the pixel increases, the qbytearray variable cannot hold all the data and therefore shows only half of the image. Do you think it's the right idea? Because when I drop the pixel of the same image, I get a very smooth image.


  • Lifetime Qt Champion

    @Mucahit said in Why am i getting this error: corrupt jpeg data: premature end of data segment:

    I think that when the pixel increases, the qbytearray variable cannot hold all the data and therefore shows only half of the image

    This is very doubtful. How big are your images? How do you store them in the database? What format are these pictures?

    "I don't think there is a problem in the database part" - you believe there is no problem, but believing is not the same as knowing. You should really do what other suggested.


Log in to reply