线程中WICImage与Bitmap数据转换
最近项目开发, 要用到线程中对图像进行缩放和二值化处理
为了省事, 图像缩放用的WICImage.ImagingFactory接口, 二值化用的是bitmap.PixelFormat := pf1bit后直接bitmap.Canvas.Draw(0, 0, wicimage)(这么做是因为bitmap在处理透明通道时会强制变为黑色, 而项目需要为白色, 所以只能draw)
运行时发现, 经常性的出现图像数据丢失, 经过排查发现数据丢失都是出现在wicimage向bitmap做数据转换时出现的
跟踪源码, 发现wicimage中的数据处理全部使用32位RGBA格式, 与外部进行图像对象交换时, 都要先吧数据存放到一个bitmap中, 再转换为其他格式
但是, 由于TBitmap不是线程安全的, 在线程中进行绘画操作要先canvas.lock才行, 而wicimage没有相关处理, 所以, 问题出现了
解决方案是, 自己吧wicimage中, 转换为中间bitmap的方法复制出来成为独立函数, 直接吧数据输出到目的bitmap, 省略draw那一步
这样就可以自己在外面随意控制bitmap的lock了
其实还有更好的办法是, 直接吧wicimage数据输出为1bit的位图, 但是我没搞定, 只要不是1bit的都可以, 唯独1bit的总是失败, 所以还是使用默认的32bit外面再使用bitmap的PixelFormat做转换了
代码如下:
function WICBitmap2Bitmap(AWICBitmap: IWICBitmap; ABMP: TBitmap): Boolean;
var
nLWicBitmap: IWICBitmapSource;
nStride: Cardinal;
nBuffer: array of Byte;
nBitmapInfo: TBitmapInfo;
nWidth, nHeight: UInt32;
begin
Result := False;
if AWICBitmap = nil then
Exit;
if ABMP = nil then
Exit; AWICBitmap.GetSize(nWidth, nHeight);
nStride := nWidth * ;
SetLength(nBuffer, nStride * nHeight); WICConvertBitmapSource(GUID_WICPixelFormat32bppBGRA, AWICBitmap, nLWicBitmap);
nLWicBitmap.CopyPixels(nil, nStride, Length(nBuffer), @nBuffer[]); FillChar(nBitmapInfo, sizeof(nBitmapInfo), );
with nBitmapInfo.bmiHeader do
begin
biSize := SizeOf(nBitmapInfo);
biWidth := nWidth;
biHeight := -nHeight;
biPlanes := ;
biBitCount := ;
end; with ABMP do
begin
PixelFormat := pf32bit;
SetSize(nWidth, nHeight);
{DC par not used (ABMP.Canvas.Handle) since Usage = DIB_RGB_COLORS}
SetDIBits(, Handle, , nHeight, @nBuffer[], nBitmapInfo, DIB_RGB_COLORS);
AlphaFormat := afDefined;
end;
Result := True;
end; function Bitmap2WICBitmap(ABMP: TBitmap; var AWICBitmap: IWicBitmap): Boolean;
var
nPixelFormat: TGUID;
nBitmapInfo: TBitmapInfo;
nBuffer: array of byte;
nWidth, nHeight: Int32;
begin
Result := False; if ABMP.AlphaFormat = afDefined then
nPixelFormat := GUID_WICPixelFormat32bppBGRA
else
nPixelFormat := GUID_WICPixelFormat32bppBGR; ABMP.PixelFormat := pf32bit; nWidth := ABMP.Width;
nHeight := ABMP.Height; SetLength(nBuffer, nWidth * * nHeight); FillChar(nBitmapInfo, sizeof(nBitmapInfo), );
with nBitmapInfo.bmiHeader do
begin
biSize := SizeOf(nBitmapInfo);
biWidth := nWidth;
biHeight := -nHeight;
biPlanes := ;
biBitCount := ;
end;
// Forces evaluation of Bitmap.Handle before Bitmap.Canvas.Handle
GetDIBits(ABMP.Canvas.Handle, ABMP.Handle, , nHeight, @nBuffer[],
nBitmapInfo, DIB_RGB_COLORS); TWICImage.ImagingFactory.CreateBitmapFromMemory(nWidth, nHeight, nPixelFormat,
nWidth * , Length(nBuffer), @nBuffer[], AWICBitmap);
end;
线程中WICImage与Bitmap数据转换的更多相关文章
- Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据
Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据 2014-06-11 10:45:14 阅读375次 我们在JNI中处理得到的BMP图片Raw数据,我们应该如何 ...
- Android 实现在线程中联网
其实我们要牢记的是,对数据流的操作都是阻塞的,在一般情况下,我们是不需要考虑这个问题的,但是在Android 实现联网的时候,我们必须考虑到这个问题.比如:从网络上下载一张图片: Java代码: pu ...
- 老问题:Android子线程中更新UI的3种方法
在Android项目中经常有碰到这样的问题,在子线程中完成耗时操作之后要更新UI,下面就自己经历的一些项目总结一下更新的方法: 方法一:用Handler 1.主线程中定义Handler: Handle ...
- 【多线程补充】SimpleDateFormat非线程安全与线程中、线程组中异常的处理
1.SimpleDateFormat非线程安全的问题 类SimpleDateFormat主要负责日期的转换与格式化,但在多线程环境中,使用此类容易造成数据转换及处理的不正确,因为SimpleDateF ...
- Android中高效的显示图片之二——在非UI线程中处理图片
在“加载大图”文章中提到的BitmapFactory.decode*方法,如果源数据是在磁盘.网络或其它任何不是在内存中的位置,那么它都不应该在UI线程中执行.因为它的加载时间不可预测且依赖于一系列因 ...
- 线程中更新ui方法汇总
一.为何写作此文 你是不是经常看到很多书籍中说:不能在子线程中操作ui,不然会报错.你是不是也遇到了如下的疑惑(见下面的代码): @Override protected void onCreate ...
- Android子线程中更新UI的4种方法
方法一:用Handler 1.主线程中定义Handler: Handler mHandler = new Handler() { @Override public void handleMessage ...
- 在非UI线程中自制Dispatcher
在C#中,Task.Run当然是一个很好的启动新并行任务的机制,但是因为使用这个方法时,每次新的任务都会在一个新的线程中(其实就是线程池中的线程)运行 这样会造成某些情形下现场调度的相对困难,即使我隔 ...
- 线程中调用python win32com
在python的线程中,调用win32com.client.Dispatch 调用windows下基于COM组件的应用接口, 需要在调用win32com.client.Dispatch前,调用pyth ...
随机推荐
- js生成json数据
<script src="~/static/js/jquery.min.js"></script><script type="text/ja ...
- 【转】NAS群晖DSM 5.0-4458安装教程
需要准备: 1.一个闲置的U盘,容量大于64M即可. 墙裂建议用如下U盘,可以隐藏成灰群晖,小巧方便. 闪迪(SanDisk)酷豆(CZ33)8GB U盘¥29.9京东商城 2.一台显示器,用于 ...
- 4.ElasticSearch的基本api操作
1. ElasticSearch的Index 1. 索引初始化 在创建索引之前 对索引进行初始化操作 指定shards数量和replicas数量 curl -XPUT 'http://192.168. ...
- git的使用(入门篇)
1.Git 的安装 Window 下的安装 从 http://git-scm.com/download 上下载window版的客户端,然后一直下一步下一步安装git即可,请注意,如果你不熟悉每个选项的 ...
- Gradle sync failed 异常
今天开发过程中出现如下异常 Gradle sync failed: Connection timed out: connect. If you are behind an HTTP proxy, pl ...
- Mongodb常用的性能监控命令
1.显示服务器状态:db.serverStatus() 2.mongodb可以通过profile来监控数据,进行优化. 查看当前是否开启profile功能:db.getProfilingLe ...
- BootStrap Table和Mybatis Plus实现服务端分页
一.后台java代码(Mybatis Plus分页) (1)Mybatis Plus分页的配置,在mybatis的xml文件中增加如下配置(Mybatis Plus官方文档:http://baomid ...
- Jquery的树插件jqxTreeGrid的使用小结
一.引入相应的js <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" t ...
- Ubuntu远程登陆、SSH图形界面、WOL远程唤醒
本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 实现目标:通过路由器配置路由路径,将拨号获取的公网IP地址指向局域网Ubuntu服务器.家里有 ...
- Vue源码后记-vFor列表渲染(3)
这一节肯定能完! 经过DOM字符串的AST转化,再通过render变成vnode,最后就剩下patch到页面上了. render函数跑完应该是在这里: function mountComponent( ...