Results 1 to 3 of 3

Thread: QML Camera - take a Square photo

  1. #1
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QML Camera - take a Square photo

    I am trying to implement it with QQuickImageProvider but I am not understanding why it is not working. My code is below. If you could help me.

    .h
    Qt Code:
    1. #ifndef MANIPULAIMAGEM_H
    2. #define MANIPULAIMAGEM_H
    3.  
    4. #include <QObject>
    5. #include <QImage>
    6. #include <QQuickImageProvider>
    7. #include <QQmlEngine>
    8. #include <QQmlContext>
    9.  
    10. class manipulaImagem : public QObject, public QQuickImageProvider
    11. {
    12. Q_OBJECT
    13.  
    14. public slots:
    15. QString recortarFotoPerfil(const QString &imagem, QRect rectRecorte);
    16.  
    17. public:
    18. manipulaImagem(QObject *parent = 0);
    19.  
    20. QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize);
    21.  
    22. private:
    23. void alocaImagem(const QString &imagem, QRect rectRecorte);
    24.  
    25. QImage imagemEditada;
    26. };
    27.  
    28. #endif // MANIPULAIMAGEM_H
    To copy to clipboard, switch view to plain text mode 

    .cpp
    Qt Code:
    1. #include "manipulaimagem.h"
    2.  
    3. #include <QDebug>
    4.  
    5. manipulaImagem::manipulaImagem(QObject *parent) : QQuickImageProvider(QQmlImageProviderBase::Image)
    6. {
    7.  
    8. }
    9.  
    10. QImage manipulaImagem::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
    11. {
    12. if(imagemEditada.isNull())
    13. {
    14. qDebug() << "Request image: (image is null)";
    15. }
    16. else
    17. {
    18. qDebug() << "Request image: image is OK";
    19. }
    20.  
    21. return imagemEditada;
    22. }
    23.  
    24. void manipulaImagem::alocaImagem(const QString &imagem, QRect rectRecorte)
    25. {
    26. QUrl caminhoImagem(imagem);
    27. QQmlEngine *engine = QQmlEngine::contextForObject(this)->engine();
    28. QQmlImageProviderBase *imageProviderBase = engine->imageProvider(caminhoImagem.host());
    29. QQuickImageProvider *imageProvider = static_cast<QQuickImageProvider*>(imageProviderBase);
    30.  
    31. QSize imageSize;
    32. QString imageId = caminhoImagem.path().remove(0, 1);
    33. imagemEditada = imageProvider->requestImage(imageId, &imageSize, imageSize);
    34.  
    35. if(imagemEditada.isNull())
    36. {
    37. qDebug() << "Loading image failed";
    38. }
    39. else
    40. {
    41. qDebug() << "Loading image OK";
    42. }
    43. }
    44.  
    45. QString manipulaImagem::recortarFotoPerfil(const QString &imagem, QRect rectRecorte)
    46. {
    47. this->alocaImagem(imagem, rectRecorte);
    48.  
    49. QString a = "image://ProvedorImagens/imagemEditada";
    50.  
    51. if(imagemEditada.isNull())
    52. {
    53. qDebug() << "Imagem is null";
    54. }
    55. else
    56. {
    57. qDebug() << "Imagem is loaded";
    58. }
    59.  
    60. return a;
    61. }
    To copy to clipboard, switch view to plain text mode 

    .qml
    Qt Code:
    1. ManipulaImagem {
    2. id: manipulaImagem
    3. }
    4.  
    5. Camera {
    6. id: camera
    7.  
    8. captureMode: Camera.CaptureStillImage
    9.  
    10. imageCapture {
    11. onImageCaptured: {
    12. previewImage.source = manipulaImagem.recortarFotoPerfil(preview, viewfinder.mapRectToSource(Qt.rect(viewfinder.x, viewfinder.y, viewfinder.width, viewfinder.height)));
    13. }
    14. }
    15. }
    16.  
    17. Rectangle {
    18. id: previewRectangle
    19.  
    20. visible: false
    21.  
    22. anchors.fill: parent
    23.  
    24. Image {
    25. id: previewImage
    26.  
    27. fillMode: Image.PreserveAspectFit
    28.  
    29. anchors.top: parent.top
    30.  
    31. width: parent.width
    32. height: parent.width
    33. }
    To copy to clipboard, switch view to plain text mode 

    The output of this code is:

    Loading image OK

    Imagem is loaded

    Request image: (image is null)

    QML Image: Failed to get image from provider: image://provedorimagens/imagemEditada

    What happens is that when I call the functions the image is not null, but when I try to return the QImage using the provider it cant return the image. I dont know why but for the image provider the image is null.

    How could I solve that?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML Camera - take a Square photo

    Where did you register your image provider instance?
    Maybe you create one instance in C++ and registered this one, instead of registering the instance you created in QML?

    Cheers,
    _

  3. #3
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QML Camera - take a Square photo

    Hello anda_skoa

    That is right. I am targeting to the wrong pointer.

    Thank you very much.

Similar Threads

  1. Photo Desktop Calendar
    By gorio in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2014, 15:47
  2. Replies: 0
    Last Post: 1st July 2013, 08:59
  3. photo thumbnail viewer
    By chezifresh in forum Qt Programming
    Replies: 12
    Last Post: 3rd January 2012, 21:25
  4. need help with photo gallery
    By rishiraj in forum Newbie
    Replies: 1
    Last Post: 9th May 2010, 05:05
  5. Replies: 8
    Last Post: 2nd August 2006, 16:19

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.