2

我有一个 Delphi 应用程序,它从文件中读取 JPEG 帧并将它们写入 AVI 文件。每个 JPEG 帧都被解码为一个 TBitmap 对象,然后使用由用于 AVI 操作的 Windows API DLL (AVIFIL32.DLL) 提供的服务,通过 Delphi 6 VFW.PAS 单元写入一个输出 AVI 文件。

问题是我最终在输出文件中出现了所有黑框。我知道在调用 AVIStreamWrite() 之前用作源的 TBitmaps 具有有效数据,因为我已使用 TBitmap.SaveToFile() 方法将它们写入磁盘并且源图像看起来很好。但我仍然以黑框结束。我从 TBitmap 获取位的方式有问题,因为如果我检查它指向的内存区域,GetDIBBits() 返回的指针显示所有零。

下面是我的代码。我究竟做错了什么?此外,如果我尝试释放 CreateDIBSection() 返回的第一个 pBits,我会遇到访问冲突。这是正常的还是有一些适当的方法可以释放它?

// aryBytes contains a JPEG frame as a Byte array.

/*
    Gets the next JPEG frame from a video input file and writes it to
    the output AVI file (stream).

    If the HRESULT from the desired operation is a failure code a
    formatted Exception will be raised.
*/
procedure makeAviCallWithJPEGFrame(
                fullVideoInFilename: string;
                const aryBytes: TDynamicByteArray;
                theAviMaker: TAviWriterWithCompression);
var
    hr: HRESULT;
    bmi: TBitmapInfo;
    pImg: PJpegDecode;
    jpegDecodeErr: TJpegDecodeError;
    hbm: HBITMAP;
    pBits, pBits2: Pointer;
    theBitmap: Graphics.TBitmap;
    iNumScanLinesCopied: integer;
    numBytesInBitmap: Integer;
begin
    if not Assigned(theAviMaker) then
        raise Exception.Create('(makeAviCallWithJPEGFrame) The AVI maker is unassigned.');

    pImg        := nil;
    pBits       := nil;
    pBits2      := nil;
    hbm         := 0;
    theBitmap   := nil;

    try
        jpegDecodeErr := JpegDecode(@aryBytes[0], Length(aryBytes), pImg);

        if jpegDecodeErr <> JPEG_SUCCESS then
            raise Exception.Create('(makeAviCallWithJPEGFrame) The bitmap failed to decode with error code: ' + IntToStr(Ord(jpegDecodeErr)));

        // Get the DIB bits.
        // theBitmap.SaveToFile('c:\temp\robomoviemaker.bmp');

        if not Assigned(pImg) then
            raise Exception.Create('(makeAviCallWithJPEGFrame) The bitmap decoded from the first JPEG frame in the video file is unassigned: ' + fullVideoInFilename);

        pImg^.ToBMI(bmi);

        theBitmap := pImg.ToBitmap;
//  This always shows a valid image so I know the bitmap contains image data.
// theBitmap.SaveToFile('c:\temp\test-bitmap.bmp');
        // Now create a DIB section.
        hbm := CreateDIBSection(theBitmap.Handle, bmi, DIB_RGB_COLORS, pBits, 0, 0);

        if hbm = ERROR_INVALID_PARAMETER then
            raise Exception.Create('(makeAviCallWithJPEGFrame) One of the parameters passed to CreateDIBSection is invalid.');

        if hbm = 0 then
            raise Exception.Create('(makeAviCallWithJPEGFrame) The call to CreateDIBSection failed.');

        // Height may be negative to indicate orientation so we
        //  take the absolute value of it.

        // Allocate memory to receive the bits.
        numBytesInBitmap :=
            Abs(bmi.bmiHeader.biHeight)
            * Abs(bmi.bmiHeader.biWidth)
            * bmi.bmiHeader.biBitCount;

        numBytesInBitmap := numBytesInBitmap div 8;

        pBits2 := AllocMem(numBytesInBitmap);

        if not Assigned(pBits2) then
            hr := LongInt(AVIERR_MEMORY)
        else
        begin
            // Get the DIB bits first.
            iNumScanLinesCopied :=
            // hr :=
                GetDIBits(
                    theBitmap.Canvas.Handle,
                    theBitmap.Handle,
                    0,
                    Abs(bmi.bmiHeader.biHeight),
                    pBits2,
                    bmi, // @pVidInfoHdr.bmiHeader^,
                    DIB_RGB_COLORS);

            if iNumScanLinesCopied <= 0 then
                // Flag it as internal error.
                hr := LongInt(AVIERR_INTERNAL)
            else
                // Add the bitmap to the output AVI file.
                hr := theAviMaker.addVideoFrame(hbm);
        end; // if not Assigned(pBits2) then

        checkAviResult('TfrmMain_moviemaker_.cmdTestClick', hr, 'Error during add video frame call');
    finally
        if Assigned(pBits2) then
            FreeMem(pBits2);

        if Assigned(pImg) then
        begin
            pImg^.Free;
            pImg := nil;
        end;

        if Assigned(theBitmap) then
            FreeAndNil(theBitmap);

        if hbm > 0 then
            DeleteObject(hbm);
    end; // try (2)
end;

// ---------------------------------------------------------------
4

0 回答 0