如何用Delphi实现把一张彩色JPG图片转换为黑白JPG图片,或灰度化 哪位高手能把实现代码贴出来,不胜感激。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 暗淡或者灰度一个图片: function NewColor(ACanvas: TCanvas; clr: TColor; Value: integer): TColor;varr, g, b: integer;beginif Value > 100 then Value := 100;clr := ColorToRGB(clr);r := Clr and $000000FF;g := (Clr and $0000FF00) shr 8;b := (Clr and $00FF0000) shr 16;r := r + Round((255 - r) * (value / 100));g := g + Round((255 - g) * (value / 100));b := b + Round((255 - b) * (value / 100));Result := Windows.GetNearestColor(ACanvas.Handle, RGB(r, g, b));end;procedure DimBitmap(ABitmap: TBitmap; Value: integer);varx, y: integer;LastColor1, LastColor2, Color: TColor;beginif Value > 100 then Value := 100;LastColor1 := -1;LastColor2 := -1;for y := 0 to ABitmap.Height - 1 dofor x := 0 to ABitmap.Width - 1 dobeginColor := ABitmap.Canvas.Pixels[x, y];if Color = LastColor1 thenABitmap.Canvas.Pixels[x, y] := LastColor2elsebeginLastColor2 := NewColor(ABitmap.Canvas, Color, Value);ABitmap.Canvas.Pixels[x, y] := LastColor2;LastColor1 := Color;end;end;end;function GrayColor(ACanvas: TCanvas; clr: TColor; Value: integer): TColor;varr, g, b, avg: integer;beginif Value > 100 then Value := 100;clr := ColorToRGB(clr);r := Clr and $000000FF;g := (Clr and $0000FF00) shr 8;b := (Clr and $00FF0000) shr 16;Avg := (r + g + b) div 3;Avg := Avg + Value;if Avg > 240 then Avg := 240;Result := Windows.GetNearestColor(ACanvas.Handle, RGB(Avg, avg, avg));end;procedure GrayBitmap(ABitmap: TBitmap; Value: integer);varx, y: integer;LastColor1, LastColor2, Color: TColor;beginLastColor1 := 0;LastColor2 := 0;for y := 0 to ABitmap.Height dofor x := 0 to ABitmap.Width dobeginColor := ABitmap.Canvas.Pixels[x, y];if Color = LastColor1 thenABitmap.Canvas.Pixels[x, y] := LastColor2elsebeginLastColor2 := GrayColor(ABitmap.Canvas, Color, Value);ABitmap.Canvas.Pixels[x, y] := LastColor2;LastColor1 := Color;end;end;end; 楼上贴的还是对Bitmap格式进行操作呀,如何对JPG格式操作呢? 先把jpg转成bmp格式,tbitmap中有个mask方法可以把彩色的转为黑白的,最后在把bmp转回为jpg格式。 记得加入 use jpegprocedure TForm1.Button1Click(Sender: TObject);var MyJpeg: TJpegImage;begin MyJpeg:= TJpegImage.Create; MyJpeg.LoadFromFile('c:\a.jpg'); MyJpeg.Grayscale :=True; MyJpeg.SaveToFile('c:\b.jpg'); MyJpeg.Free ;end; xzhifei 说的方法不行呀,MyJpeg.Grayscale:=Ture;有什么用呢?b.jpg和a.jp完全一样,没有被黑白化。 转成BMP再处理罗,二值化的源代码到处都有,只是都是针对BMP的 给你贴一个公式,这个公式就是将彩色图片转换成灰度的Y=0.3RED+0.59GREEN+0.11Blue 这个连接,你去看看吧http://www.bobpowell.net/grayscale.htm 对了,我再给你贴一个网址,上面有imagefilelib 支持JPEG 2000, PNG, MNG, TIFF, ICO, CUR and PCX images. RAS, PNM, PGM, PPM, HIPS ,你自己看看就知道了。他们有程序,进行图片的处理,效果很好,应该对你有启发了! 如果你不介意使用第三方控件的话,或许你可以试试ibqsImage 图像处理组件,是免费的 [怎么修改这个错误Error] test.pas(36): Not enough actual parameters 红旗下面用delphi7编程(不是kylix哟) 共享ERP源码下载地址 请教一个问题,关于文本文件的! 高手请指点!如何用delphi连接mysql的数据库!! 如何判断在DBGRID控件上,鼠标移动到哪一列上面? 有关窗口的释放 一个很大的问题 大家快进来讨论 没分了 。 嘿 EnumChildWindows 不是枚举所有的子窗口吗?为何我只能得到最后一个? 关于hbrBackground一些疑问 什么控件能显示ani动画? 这两种声明有何不同?
function NewColor(ACanvas: TCanvas; clr: TColor; Value: integer): TColor;varr, g, b: integer;beginif Value > 100 then Value := 100;clr := ColorToRGB(clr);r := Clr and $000000FF;g := (Clr and $0000FF00) shr 8;b := (Clr and $00FF0000) shr 16;r := r + Round((255 - r) * (value / 100));g := g + Round((255 - g) * (value / 100));b := b + Round((255 - b) * (value / 100));Result := Windows.GetNearestColor(ACanvas.Handle, RGB(r, g, b));end;procedure DimBitmap(ABitmap: TBitmap; Value: integer);varx, y: integer;LastColor1, LastColor2, Color: TColor;beginif Value > 100 then Value := 100;LastColor1 := -1;LastColor2 := -1;for y := 0 to ABitmap.Height - 1 dofor x := 0 to ABitmap.Width - 1 dobeginColor := ABitmap.Canvas.Pixels[x, y];if Color = LastColor1 thenABitmap.Canvas.Pixels[x, y] := LastColor2elsebeginLastColor2 := NewColor(ABitmap.Canvas, Color, Value);ABitmap.Canvas.Pixels[x, y] := LastColor2;LastColor1 := Color;end;end;end;function GrayColor(ACanvas: TCanvas; clr: TColor; Value: integer): TColor;varr, g, b, avg: integer;beginif Value > 100 then Value := 100;clr := ColorToRGB(clr);r := Clr and $000000FF;g := (Clr and $0000FF00) shr 8;b := (Clr and $00FF0000) shr 16;Avg := (r + g + b) div 3;Avg := Avg + Value;if Avg > 240 then Avg := 240;Result := Windows.GetNearestColor(ACanvas.Handle, RGB(Avg, avg, avg));end;procedure GrayBitmap(ABitmap: TBitmap; Value: integer);varx, y: integer;LastColor1, LastColor2, Color: TColor;beginLastColor1 := 0;LastColor2 := 0;for y := 0 to ABitmap.Height dofor x := 0 to ABitmap.Width dobeginColor := ABitmap.Canvas.Pixels[x, y];if Color = LastColor1 thenABitmap.Canvas.Pixels[x, y] := LastColor2elsebeginLastColor2 := GrayColor(ABitmap.Canvas, Color, Value);ABitmap.Canvas.Pixels[x, y] := LastColor2;LastColor1 := Color;end;end;end;
var
MyJpeg: TJpegImage;
begin
MyJpeg:= TJpegImage.Create;
MyJpeg.LoadFromFile('c:\a.jpg');
MyJpeg.Grayscale :=True;
MyJpeg.SaveToFile('c:\b.jpg');
MyJpeg.Free ;
end;
Y=0.3RED+0.59GREEN+0.11Blue