HObject转换为QImage

在Qt中需要先将HObject转换为QImage,然后再使用。

cpp 复制代码
int calcBytesPerLine(int width, int bitCounts)
{
    //把图像的宽度数值换算成4的整数倍
    int step = 0;
    if (bitCounts >= 8)
    {
        step = (width * bitCounts / 8 + 3) / 4 * 4;
    }
    else
    {
        step = (width * bitCounts + 31) / 32 * 4;
    }

    return step;
}


QImage MainWindow::HObject2QImage(const HalconCpp::HObject &Hobj) //Halcon中的HObject类型转QImage类型
{
    HalconCpp::HTuple hv_Chn = HalconCpp::HTuple();
    HalconCpp::HTuple hv_Length;
    HalconCpp::HTuple hv_Type;
    HalconCpp::HObject ho_Img;
    QImage image;

    ConvertImageType(Hobj, &ho_Img, "byte");
    CountChannels(ho_Img, &hv_Chn);
    TupleLength(hv_Chn, &hv_Length);
    if (hv_Length.L() == 0)
    {
        return image;
    }

    HalconCpp::HTuple hv_Width;
    HalconCpp::HTuple hv_Height;
    int width = 0;
    int height = 0;
    int step = 0;

    if (hv_Chn[0].I() == 1)
    {
        HalconCpp::HTuple hv_Ptr;
        GetImagePointer1(ho_Img, &hv_Ptr, &hv_Type, &hv_Width, &hv_Height);
        width = (int)hv_Width;
        height = (int)hv_Height;
        step = calcBytesPerLine(width, 8);
        BYTE *p = (BYTE *)hv_Ptr[0].L(); //必须是L(),不能是I()
        QImage temp = QImage(width, height, QImage::Format_Grayscale8);
        image = temp.copy();
        BYTE *data8 = image.bits();
        int pix = 0;

        for (int i = 0; i < height; i++)
        {
            for (int k = 0; k < width; k++)
            {
                pix = step * i + k;
                data8[pix + 0] = p[width * i + k];
            }
        }
    }
    else if (hv_Chn[0].I() == 3)
    {
        HalconCpp::HTuple hv_PtrR, hv_PtrG, hv_PtrB;
        GetImagePointer3(ho_Img, &hv_PtrR, &hv_PtrG, &hv_PtrB, &hv_Type, &hv_Width, &hv_Height);
        width = (int)hv_Width;
        height = (int)hv_Height;
        step = calcBytesPerLine(width, 24); //三通道
        BYTE *pr = (BYTE *)hv_PtrR[0].L();  //必须是L(),不能是I()
        BYTE *pg = (BYTE *)hv_PtrG[0].L();
        BYTE *pb = (BYTE *)hv_PtrB[0].L();

        QImage temp = QImage(width, height, QImage::Format_RGB888);
        image = temp.copy();
        BYTE *data24 = image.bits();
        int pix = 0;

        for (int i = 0; i < height; i++)
        {
            for (int k = 0; k < width; k++)
            {
                pix = step * i + k * 3;
                data24[pix + 0] = pr[width * i + k]; //QImage格式是RGB,不是BGR
                data24[pix + 1] = pg[width * i + k];
                data24[pix + 2] = pb[width * i + k];
            }
        }
    }

    return image;
}
相关推荐
锦亦之22334 小时前
QT+OSG+OSG-earth如何在窗口显示一个地球
开发语言·qt
柳鲲鹏7 小时前
编译成功!QT/6.7.2/Creator编译Windows64 MySQL驱动(MinGW版)
开发语言·qt·mysql
三玖诶7 小时前
如何在 Qt 的 QListWidget 中逐行添加和显示数据
开发语言·qt
阳光开朗_大男孩儿13 小时前
DBUS属性原理
linux·服务器·前端·数据库·qt
Alphapeople14 小时前
Qt Modbus
开发语言·qt
竹林海中敲代码14 小时前
Qt Creator 集成开发环境 常见问题
qt·qt工具常见问题
竹林海中敲代码18 小时前
Qt安卓开发连接手机调试(红米K60为例)
android·qt·智能手机
长沙红胖子Qt19 小时前
关于 Qt运行加载内存较大崩溃添加扩大运行内存 的解决方法
开发语言·qt·qt扩大运行内存
gopher951119 小时前
qt相关面试题
开发语言·qt·面试
三玖诶1 天前
在 Qt 中使用 QLabel 设置 GIF 动态背景
开发语言·qt·命令模式