Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. cannot read image that was achieved from QAbstractVideoSurface implemented class

cannot read image that was achieved from QAbstractVideoSurface implemented class

Scheduled Pinned Locked Moved Solved Mobile and Embedded
8 Posts 2 Posters 778 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jafar.Sag.Sibil
    wrote on last edited by Jafar.Sag.Sibil
    #1

    I wrote the following code and want to write it into my filesystem of android (the write operation was finished correctly) after writing I tried to open that file but I couldn't open it why?

    videosurface.cpp

    bool VideoSurfacer::present ( const QVideoFrame & f )
    {
    	QVideoFrame frame ( f );
    	if ( frame.isValid ( ) && frame.map ( QAbstractVideoBuffer::MapMode::ReadOnly ) )
    		{
    						i++;
    			QString path = QStandardPaths::writableLocation ( QStandardPaths::GenericDataLocation ) + "/download/" + QString::number ( i ) + ".jpg";
    			// however you can use QVideoFrame::imageFormatFromPixelFormat(frame.pixelFormat()) but sometimes it returns invalid which make our image null
    			QImage img ( frame.bits ( ), frame.width ( ), frame.height ( ), frame.bytesPerLine ( ), QImage::Format_Grayscale8 );
                            img.isNull();//false
    			img.convertToFormat ( QImage::Format_RGB32 ).save ( path, "JPG" );
    			frame.unmap ( );
    
    		}
    	return true;
    }
    
    bool VideoSurfacer::isFormatSupported ( const QVideoSurfaceFormat & format ) const
    {
    	bool size				 = format.frameSize ( ) == QSize ( 800, 600 );
    	bool valid			 = format.isValid ( );
    	bool pixelFormat = supportedPixelFormats ( ).contains ( format.pixelFormat ( ) );
    	return valid && pixelFormat && size;
    }
    
    QVideoSurfaceFormat VideoSurfacer::nearestFormat ( const QVideoSurfaceFormat & format ) const
    {
    	QVideoSurfaceFormat newFormat ( format );
    	newFormat.setFrameRate ( 30 );
    	newFormat.setMirrored ( true );
    	newFormat.setFrameSize ( 800, 600 );
    	return newFormat;
    }
    
    QList<QVideoFrame::PixelFormat> VideoSurfacer::supportedPixelFormats ( QAbstractVideoBuffer::HandleType type ) const
    {
    	if ( type == QAbstractVideoBuffer::NoHandle )
    		{
    			// however you set the pixel format something other than first item of this list but the present method retrieve the first item of this list pixelformat but other things like view size and frame rate affect the retrieved frames
    			return QList<QVideoFrame::PixelFormat> ( ) << QVideoFrame::Format_NV21;
    		}
    	else
    		{
    			return QList<QVideoFrame::PixelFormat> ( );
    		}
    }
    

    main.cpp

    	QAbstractVideoSurface * surfacer = new VideoSurfacer;
    
    	QCamera cam ( QCameraInfo::availableCameras ( ).at ( 1 ) );
    	cam.setCaptureMode ( QCamera::CaptureMode::CaptureViewfinder );
    	cam.setViewfinder ( surfacer );
    
    	QCameraViewfinderSettings settings = cam.viewfinderSettings ( );
    	settings.setMaximumFrameRate ( 30 );
    	settings.setMinimumFrameRate ( 30 );
    	settings.setResolution ( 800, 600 );
    	settings.setPixelFormat ( QVideoFrame::PixelFormat::Format_NV21 );
    	cam.setViewfinderSettings ( settings );
    
    	cam.start ( );
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #8

      No, NV21 is one YUV color space format.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Did you check that the image is valid both before and after the conversion ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jafar.Sag.Sibil
          wrote on last edited by
          #3

          both of those (before and after conversion) returns false of method isNull (code is edited please reread present method)

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Jafar.Sag.Sibil
            wrote on last edited by Jafar.Sag.Sibil
            #4

            I find the problem I convert that image to rgb32 and save it however android device cannot open it but computer with imv command can open it and show my image correctly

            and that is solved too I just move the pictures and reopen them in that time images are open without any problem only show gray style

            I've tried many other formats many of those make my image invalid (isnull() = true) some other like Mono,MonoLSB,Alpha8 shows an incorrect image and some other like Indexed8 returns a same result (gray style) by grayscaled format

            How can I have my nv21 video frames colourful

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #5

              You likely have to do the conversion NV21 -> RGB yourself.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Jafar.Sag.Sibil
                wrote on last edited by
                #6

                I want color in my image if nv21 is gray how can I do that

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jafar.Sag.Sibil
                  wrote on last edited by Jafar.Sag.Sibil
                  #7

                  I think I understand you. You mean NV21 pixel format is gray and my codes don't have any problem.
                  So which pixel format I have to use that is supported for all android devices. I tried entire of them no one of those call present method other than nv21

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    No, NV21 is one YUV color space format.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved