将jpeg图像转换为Byte数组 [英] Convert jpeg image to Byte array

查看:818
本文介绍了将jpeg图像转换为Byte数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,专家,

该怎么做??

谢谢

Hi Experts,

How to do this??

Thanks

推荐答案

1.使用所选的程序包-GDI +,CxImage等加载图像
2.从图像创建HBITMAP
3.在HBITMAP上调用GetDIBits

将图像数据插入PDF时,必须执行相同的步骤.这是我用于此任务的代码:(注意:您还必须添加代码以初始化和关闭GDI +)

1. Load image using package of choice - GDI+, CxImage, etc
2. Create HBITMAP from image
3. Call GetDIBits on the HBITMAP

When inserting image data into PDFs, one must perform the same steps. Here''s the code I use for this task: (Note: you also have to add code to initialize and shutdown GDI+)

// BMP, GIF, JPEG, PNG, TIFF, Exif, WMF, and EMF
HBITMAP mLoadImg(WCHAR *szFilename)
{
    HBITMAP result=NULL;

    Gdiplus::Bitmap* bitmap = new Gdiplus::Bitmap(szFilename,false);
    bitmap->GetHBITMAP(NULL, &result);
    delete bitmap;
    return result;
}

// Returns the DI (Device Independent) bits of the Bitmap
// Here I use 24 bit since it's suppported in pdf
// result is width*height*3 bytes (24 bit)
char unsigned *myGetDibBits24(HBITMAP hBmpSrc)
{
    BITMAPINFO bi;
    BITMAP bm;
    BOOL bRes;
    char unsigned *buf, *result;
    long outIndex, inIndex;
    long width, height, x, y;

    HDC memDC;
    HBITMAP oldBmp;

    memDC = CreateCompatibleDC(NULL);
    oldBmp = (HBITMAP)SelectObject(memDC, hBmpSrc);

    GetObject(hBmpSrc, sizeof(bm), &bm);
    width = bm.bmWidth;
    height = bm.bmHeight;

    bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
    bi.bmiHeader.biWidth = width;
    bi.bmiHeader.biHeight = -height;
    bi.bmiHeader.biPlanes = 1;
    bi.bmiHeader.biBitCount = 32;
    bi.bmiHeader.biCompression = BI_RGB;
    bi.bmiHeader.biSizeImage = 0;//bm.bmWidth * 4 * bm.bmHeight;
    bi.bmiHeader.biClrUsed = 0;
    bi.bmiHeader.biClrImportant = 0;

    buf = new unsigned char[width * 4 * height];
    bRes = GetDIBits(memDC, hBmpSrc, 0, bm.bmHeight, buf, &bi, DIB_RGB_COLORS);

    SelectObject(memDC, oldBmp);
    if (!bRes)
    {
        delete(buf);
        buf = NULL;
    }
    DeleteDC(memDC);

    result = new unsigned char[width*height*3];

    outIndex = 0;
    inIndex = 0;
    for (y=0; y<height;>    {
        inIndex = y * bm.bmWidthBytes;

        for (x=0; x<width;>        {
            result[outIndex++] = buf[inIndex+2];
            result[outIndex++] = buf[inIndex+1];
            result[outIndex++] = buf[inIndex];
            inIndex += 4;
        }
    }
    delete(buf);

    return result;
}


从技术上讲,jpeg图像只是字节数组(与其他任何文件一样).
但是,我想,您实际上是想获得对像素值的访问权限.我认为最简单的方法是使用CImage类加载图像,然后使用 CImage :: GetBits [^ ]方法.
Technically speaking a jpeg image is just an array of bytes (like any other file).
However, I suppose, you actually want to gain access to pixel values. I think the easy way is loading the image using the CImage class and then using the CImage::GetBits[^] method.


这篇关于将jpeg图像转换为Byte数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆