本文介绍了SetMetadataByName失败,错误0x88982F81的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨大家好, 我正在尝试将XMP元数据添加到tiff文件(此时此类型没有元数据)。 不幸的是,操作 hr = piFrameQWriter-> SetMetadataByName(L" /",& value); [见下文] 失败,错误为WINCODEC_ERR_UNSUPPORTEDOPERATION(0x88982F81)。 有任何建议吗? // AddMetadata.cpp:定义控制台应用程序的入口点。 // #include " stdafx.h" #include < wincodec.h> #include < wincodecsdk.h> #include < atlbase.h> #pragma 评论( lib , " windowscodecs.lib" ) void AddMetadataToFile (); $ struct SErrorInfo { int nLine ; HRESULT hr ; SErrorInfo (): nLine ( - 1), hr (0){} SErrorInfo ( int n , HRESULT h ): nLine ( n ), hr ( h ){} }; #define TESTHR \ 如果(! SUCCEEDED ( hr ))\ {\ throw new SErrorInfo ( __ LINE __ , hr ); \ } $ void AddMetadataToFile () { //工厂指针 CComPtr < IWICImagingFactory > pFactory ; //创建COM成像工厂 HRESULT hr = CoCreateInstance ( CLSID_WICImagingFactory , NULL , CLSCTX_INPROC_SERVER , IID_PPV_ARGS (& pFactory ) ); TESTHR ; //加载图片 //创建解码器 但是CComPtr < IWICBitmapDecoder > pDecoder ; hr = pFactory - > CreateDecoderFromFilename ( TEXT (" .. \\Images\\Green.tif" ), //要解码的图像 NULL ,&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //不喜欢特定供应商 GENERIC_READ , // Desired读取文件的访问权 WICDecodeMetadataCacheOnDemand ,&NBSP; //需要时缓存元数据 & pDecoder &NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //指向解码器的指针 ); TESTHR ; //从解码器中检索图像的第一帧 CComPtr < IWICBitmapFrameDecode > pFrame ; hr = pDecoder - > GetFrame (0,& pFrame ); TESTHR ; //枚举元数据 获取查询阅读器 CComPtr < IWICMetadataQueryReader > pQueryReader ; hr = pFrame - > GetMetadataQueryReader (& pQueryReader ); TESTHR ; //获取枚举器 CComPtr < IEnumString > pEnum ; hr = pQueryReader - > GetEnumerator (& pEnum ); TESTHR ; //枚举元数据 hr = pEnum - > 重置(); TESTHR ; CComBSTR resstr ; ULONG uFetched = 0; 而(( hr = pEnum - > 下一步(1,& resstr ,& uFetched ))== S_OK &&& style ="color:#010001"> uFetched == 1) { wprintf_s ( L "%s \ n" ,( const wchar_t *) resstr ); } //创建并初始化图像编码器 CComPtr < IWICStream > piFileStream ; UINT count = 0; //创建文件流。 hr = pFactory - > CreateStream (& piFileStream ); TESTHR ; //初始化我们的新文件流。 hr = piFileStream - > InitializeFromFilename ( L " .. \\Images\\Image2.tif" , GENERIC_WRITE ); TESTHR ; //创建编码器。 CComPtr < IWICBitmapEncoder > piEncoder ; hr = pFactory - > CreateEncoder ( GUID_ContainerFormatTiff , NULL ,& piEncoder ); TESTHR ; //初始化编码器 hr = piEncoder - > 初始化( piFileStream , WICBitmapEncoderNoCache ); TESTHR ; //复制解码的帧信息 CComPtr < IWICBitmapFrameEncode > piFrameEncode ; hr = piEncoder - > CreateNewFrame (& piFrameEncode , NULL ); TESTHR ; hr = piFrameEncode - > 初始化( NULL ); TESTHR ; UINT width , height = 0; hr = pFrame - > GetSize (& width ,& height ); TESTHR ; hr = piFrameEncode - > SetSize ( width , height ); TESTHR ; double dpiX , dpiY = 0.0; pFrame - > GetResolution (& dpiX ,& dpiY ); TESTHR ; WICPixelFormatGUID pixelFormat = {0}; pFrame - > GetPixelFormat (& pixelFormat ); TESTHR ; hr = piFrameEncode - > SetPixelFormat (& pixelFormat ); TESTHR ; / /检查新的帧格式 bool formatsEqual = FALSE ; if ( SUCCEEDED ( hr )) { GUID srcFormat ; GUID destFormat ; hr = pDecoder - > GetContainerFormat (& srcFormat ); if ( SUCCEEDED ( hr )) { hr = piEncoder - > GetContainerFormat (& destFormat ); } if ( SUCCEEDED ( hr )) { if ( srcFormat == destFormat ) formatsEqual = true ; else formatsEqual = false ; } } if (! formatsEqual ) { hr = HRESULT_FROM_WIN32 ( ERROR_BAD_FORMAT ); } TESTHR ; //复制元数据 CComPtr < IWICMetadataBlockReader > piBlockReader ; hr = pFrame - > QueryInterface ( IID_PPV_ARGS (& piBlockReader )); TESTHR ; CComPtr < IWICMetadataBlockWriter > piBlockWriter ; hr = piFrameEncode - > QueryInterface ( IID_PPV_ARGS (& piBlockWriter )); TESTHR ; hr = piBlockWriter - > InitializeFromBlockReader ( piBlockReader ); TESTHR ; 添加其他元数据 CComPtr < IWICMetadataQueryWriter > pXMPWriter ; GUID vendor = GUID_VendorMicrosoft ; hr = pFactory - > CreateQueryWriter ( GUID_MetadataFormatXMP ,& 供应商,& pXMPWriter ); TESTHR ; //将元数据写入XMP编写器 PROPVARIANT value ; PropVariantInit (& value ); value 。 vt = VT_LPWSTR ; 值。 pwszVal = L "元数据测试图像。"; hr = pXMPWriter - > SetMetadataByName ( L " / dc:title" ,& value ); PropVariantClear (& 价值); TESTHR ; CComPtr < IWICMetadataQueryWriter > piFrameQWriter ; hr = piFrameEncode - > GetMetadataQueryWriter (& piFrameQWriter ); TESTHR ; PropVariantInit (& value ); value 。 vt = VT_UNKNOWN ; value 。 punkVal = pXMPWriter ; value 。 punkVal - > AddRef (); hr = piFrameQWriter - > SetMetadataByName ( L " /" ,& value ); PropVariantClear (& 价值); TESTHR ; //完成编码图像 hr = piFrameEncode - > WriteSource ( static_cast < IWICBitmapSource *>( pFrame ), NULL ); TESTHR ; hr = piFrameEncode - > 提交(); TESTHR ; pFrame = NULL ; piFrameEncode = NULL ; piFrameQWriter = NULL ; piEncoder - > 提交(); TESTHR ; } $ int _ tmain ( int argc , _ TCHAR * argv []) { CoInitialize ( NULL ); 尝试 { AddMetadataToFile (); } catch ( SErrorInfo * p ) { printf_s ("行%d处的异常,代码为0x%08X \ n \ n , p - > nLine , p - > hr ); } catch (...) { printf_s (" Exception\\\\ nn" ); } CoUninitialize (); return 0; } 解决方案 我不是元数据专家,但我找到了两个可能完成你想要做的事情的例子。 参见"示例"和"示例"。 的部分内容IWICMetadataQueryWriter接口 另请参阅"第5部分:添加其他元数据" How-to:Re的部分 - 使用元数据对JPEG图像进行编码 我知道后者是针对JPEG的,但是看看通过TIFF图像尝试得到的结果。 Hi folks,I'm trying to add XMP metadata to a tiff file (which has no metadata of this type at this point).Unfortunately, the operationhr = piFrameQWriter->SetMetadataByName(L"/", &value); [see below]fails with error WINCODEC_ERR_UNSUPPORTEDOPERATION (0x88982F81).Any suggestions?// AddMetadata.cpp : Defines the entry point for the console application.//#include"stdafx.h"#include<wincodec.h>#include<wincodecsdk.h>#include<atlbase.h>#pragmacomment ( lib,"windowscodecs.lib" )voidAddMetadataToFile();structSErrorInfo{int nLine ;HRESULT hr ;SErrorInfo () :nLine ( -1 ), hr ( 0 ) {}SErrorInfo ( intn, HRESULTh ) : nLine (n ), hr ( h ) {}} ;#defineTESTHR \if ( ! SUCCEEDED (hr ) ) \{ \throw newSErrorInfo ( __LINE__,hr ) ; \}voidAddMetadataToFile(){// The factory pointerCComPtr<IWICImagingFactory>pFactory ;// Create the COM imaging factoryHRESULT hr =CoCreateInstance (CLSID_WICImagingFactory,NULL,CLSCTX_INPROC_SERVER,IID_PPV_ARGS(&pFactory) ) ;TESTHR ;// Load image// Create a decoderCComPtr<IWICBitmapDecoder>pDecoder ;hr = pFactory ->CreateDecoderFromFilename (TEXT ( "..\\Images\\Green.tif" ), // Image to be decodedNULL, // Do not prefer a particular vendorGENERIC_READ, // Desired read access to the fileWICDecodeMetadataCacheOnDemand, // Cache metadata when needed& pDecoder // Pointer to the decoder ) ;TESTHR ;// Retrieve the first frame of the image from the decoderCComPtr<IWICBitmapFrameDecode>pFrame ;hr = pDecoder ->GetFrame ( 0, &pFrame ) ;TESTHR ;// Enum metadata// Get the query readerCComPtr<IWICMetadataQueryReader>pQueryReader ;hr = pFrame ->GetMetadataQueryReader ( &pQueryReader ) ;TESTHR ;// Get enumeratorCComPtr<IEnumString>pEnum ;hr = pQueryReader ->GetEnumerator ( &pEnum ) ;TESTHR ;// Enumerate metadatahr = pEnum ->Reset() ;TESTHR ;CComBSTR resstr ;ULONG uFetched = 0 ;while ( ( hr =pEnum -> Next ( 1, &resstr, & uFetched ) ) ==S_OK && uFetched == 1 ){wprintf_s ( L"%s\n", (constwchar_t*) resstr ) ;}// Create and Initialize the Image EncoderCComPtr<IWICStream>piFileStream ;UINT count = 0;// Create a file stream.hr = pFactory ->CreateStream ( &piFileStream ) ;TESTHR ;// Initialize our new file stream.hr = piFileStream ->InitializeFromFilename (L"..\\Images\\Image2.tif",GENERIC_WRITE ) ;TESTHR ;// Create the encoder.CComPtr<IWICBitmapEncoder>piEncoder ;hr = pFactory ->CreateEncoder ( GUID_ContainerFormatTiff,NULL, & piEncoder ) ;TESTHR ;// Initialize the encoderhr = piEncoder ->Initialize ( piFileStream,WICBitmapEncoderNoCache ) ;TESTHR ;// Copy Decoded Frame InformationCComPtr<IWICBitmapFrameEncode>piFrameEncode ;hr = piEncoder ->CreateNewFrame ( &piFrameEncode, NULL ) ;TESTHR ;hr = piFrameEncode->Initialize(NULL);TESTHR ;UINT width,height = 0;hr = pFrame ->GetSize ( & width, &height ) ;TESTHR ;hr = piFrameEncode->SetSize(width,height);TESTHR ;double dpiX,dpiY = 0.0;pFrame -> GetResolution ( &dpiX, & dpiY ) ;TESTHR ;WICPixelFormatGUIDpixelFormat = { 0 };pFrame -> GetPixelFormat(&pixelFormat);TESTHR ;hr = piFrameEncode->SetPixelFormat(&pixelFormat);TESTHR ;// Check new frame formatbool formatsEqual =FALSE;if (SUCCEEDED(hr)){GUID srcFormat;GUID destFormat;hr = pDecoder ->GetContainerFormat(&srcFormat);if (SUCCEEDED(hr)){hr = piEncoder->GetContainerFormat(&destFormat);}if (SUCCEEDED(hr)){if (srcFormat ==destFormat)formatsEqual =true;elseformatsEqual =false;}}if ( ! formatsEqual ){hr = HRESULT_FROM_WIN32 (ERROR_BAD_FORMAT ) ;}TESTHR ;// Copy the MetadataCComPtr<IWICMetadataBlockReader>piBlockReader ;hr = pFrame->QueryInterface ( IID_PPV_ARGS ( &piBlockReader ) ) ;TESTHR ;CComPtr<IWICMetadataBlockWriter>piBlockWriter ;hr = piFrameEncode ->QueryInterface ( IID_PPV_ARGS ( &piBlockWriter ) ) ;TESTHR ;hr = piBlockWriter ->InitializeFromBlockReader (piBlockReader ) ;TESTHR ;// Add Additional MetadataCComPtr<IWICMetadataQueryWriter>pXMPWriter ;GUID vendor =GUID_VendorMicrosoft;hr = pFactory ->CreateQueryWriter (GUID_MetadataFormatXMP, & vendor, &pXMPWriter ) ;TESTHR ;// Write metadata to the XMP writerPROPVARIANT value;PropVariantInit(&value);value.vt =VT_LPWSTR;value.pwszVal =L"Metadata Test Image.";hr = pXMPWriter->SetMetadataByName(L"/dc:title", &value);PropVariantClear(&value);TESTHR ;CComPtr<IWICMetadataQueryWriter>piFrameQWriter ;hr = piFrameEncode ->GetMetadataQueryWriter ( &piFrameQWriter ) ;TESTHR ;PropVariantInit(&value);value.vt =VT_UNKNOWN;value.punkVal =pXMPWriter;value.punkVal->AddRef();hr = piFrameQWriter->SetMetadataByName(L"/", &value);PropVariantClear(&value);TESTHR ;// Finalize the Encoded Imagehr = piFrameEncode ->WriteSource ( static_cast<IWICBitmapSource*> (pFrame ), NULL ) ;TESTHR ;hr = piFrameEncode->Commit();TESTHR ;pFrame = NULL ;piFrameEncode =NULL ;piFrameQWriter =NULL ;piEncoder->Commit();TESTHR ;}int_tmain(intargc, _TCHAR*argv[]){CoInitialize(NULL);try{AddMetadataToFile();}catch ( SErrorInfo *p ){printf_s ( "Exception at line %d with code 0x%08X\n\n",p -> nLine,p -> hr ) ;}catch ( ... ){printf_s ( "Exception\n\n" ) ;}CoUninitialize () ;return 0;} 解决方案 I am not a metadata expert, but I found two examples that might accomplish what you are trying to do.See the "Example" section of theIWICMetadataQueryWriter interfaceSee also the "Part 5: Add Additional Metadata" section ofHow-to: Re-encode a JPEG Image with MetadataI know the latter is for JPEG, but see what result you get by trying it with your TIFF image. 这篇关于SetMetadataByName失败,错误0x88982F81的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-14 22:47