unit Unit2;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,math,FMX.Surfaces,
  FMX.Controls.Presentation, FMX.StdCtrls, FMX.Objects;

type
  TForm2 = class(TForm)
    Button1: TButton;
    Image1: TImage;
    OpenDialog1: TOpenDialog;
    Image2: TImage;
    Timer1: TTimer;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);

private
    class function  BitmapCompress(ABitmap: TBitmap): TBitmap;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.fmx}

{ TForm2 }

class function TForm2.BitmapCompress(ABitmap: TBitmap): TBitmap; //耗时耗内存,待优化
var
  SaveParams:TBitmapCodecSaveParams;
  Astream,bstream:TMemorystream;
  Surf: TBitmapSurface;
  intoldSize:Int64;
  CompressQuality:integer;
  BBitmap:TBitmap;
  ratio:double;
begin
   Astream:=TMemorystream.Create;
   Bstream:=TMemorystream.Create;
   BBitmap:=TBitmap.Create;
   ABitmap.SaveToStream(Astream);
   //ratio:=Astream.Size/1024;
  // ratio:= 10000/ratio;
   //CompressQuality:=ceil(10000/ratio);
  // if Astream.Size/1024>4096 then
  //    CompressQuality:=5
    if Astream.Size/1024>2048 then    //大图小于15之后失真严重
      CompressQuality:=15
    else if Astream.Size/1024>1024 then
      CompressQuality:=20
    else if Astream.Size/1024>512 then
      CompressQuality:=50
    else if Astream.Size/1024>256 then
      CompressQuality:=60
    else if Astream.Size/1024>128 then
      CompressQuality:=70
    else if Astream.Size/1024<50 then
      CompressQuality:=100
    else
      CompressQuality:=80;
   //showmessage(CompressQuality.ToString);
   SaveParams.Quality:=CompressQuality;
   Surf:=TBitmapSurface.Create;
   Surf.Assign(Abitmap);
   try
      if not TBitmapCodecManager.SaveToStream(bStream, Surf, 'jpg',@SaveParams) then
      begin
        showmessage('图片压缩失败!');
        exit;
      end;
    except
      showmessage('图片压缩意外错误!');
      exit;
    end;
    try
      if Astream.Size<Bstream.Size then
      begin
       // Astream.SaveToFile('d:\2.jpg');
        BBitmap.LoadFromStream(Astream);
      end
      else
      begin
       // bstream.SaveToFile('d:\2.jpg');
        BBitmap.LoadFromStream(bstream);
      end;
    except
      showmessage('图片存储失败!');
      exit;
    end;
    BBitmap.SaveToFile('d:\1111.jpg');
    result:= BBitmap;
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  ABitmap:Tbitmap;
  StartTime,EndTime:cardinal;
begin

ABitmap :=FMX.Graphics.TBitmap.Create;
    opendialog1.filter:='图片文件(*.bmp;*.png;*.jpeg;*.jpg)|*.bmp;*.png;*.jpeg;*.jpg';
    if OpenDialog1.Execute then
    begin
    //StartTime:=GetTickCount;
      ABitmap.LoadFromFile(OpenDialog1.FileName);
     //  Timer1.Enabled:=true;
      image1.Bitmap.LoadFromFile(OpenDialog1.FileName);
     image2.Bitmap.Assign(BitmapCompress(ABitmap));
    end;
  //Timer1.Enabled:=false;
end;

procedure TForm2.Timer1Timer(Sender: TObject);
var
  BeginCount,Endcount,StartCount:Cardinal;
begin

//    BeginCount:=GetTickCount;//只适用于windows,还没有找到移动端方法计算程序运行时间

end;

end.

delphi BitmapCompress的更多相关文章

  1. 学习笔记:7z在delphi的应用

    最近做个发邮件的功能,需要将日志文件通过邮件发送回来用于分析,但是日志文件可能会超级大,测算下来一天可能会有800M的大小.所以压缩是不可避免了,delphi中的默认压缩算法整了半天不太好使,就看了看 ...

  2. delphi连接sql存储过程

    针对返回结果为参数的 一. 先建立自己的存储过程 ALTER PROCEDURE [dbo].[REName] ) AS BEGIN select ROW_NUMBER() over(order by ...

  3. delphi 2010与delphi XE破解版的冲突

    在系统中同时安装了Dephi 2010LITE版与Delphi XE lite后,总是会有一个有问题 是因为两者都是读取C:\ProgramData\Embarcadero目录下的license文件, ...

  4. [Delphi] Delphi版本号对照

    VER300    Delphi Seattle / C++Builder Seattle    23    230    (Delphi:Win32/Win64/OSX/iOS32/iOS64/An ...

  5. delphi tidhttp 超时设置无效的解决方法

    现在delphi都发布到xe8了,tidhttp还有缺陷,那就是超时设置在没有网络或者连不上服务器的时候是无效的,不管你设置为多少都要10-20秒.connectTimeout和readTimeout ...

  6. Delphi Code Editor 之 编辑器选项

    Delphi Code Editor 之 编辑器选项 可从Code Editor的右键菜单中选择“Properties”菜单项来查看编辑器选项.也可以从主菜单[Tools | Editor Optio ...

  7. Delphi使用ADO进行数据库编程

    Delphi是一个可视化的编程工具,ADO编程也是这样,所以话不多言,直接通过代码.截图和语言来说明. 我的数据库是Oracle,为了测试,先建一个表:create table practice(un ...

  8. 怎么使用Delphi获取当前的时间,精确到毫秒

    先介绍一个可能比较常用的方法,获取当前时间 var datetime: string; begin datetime:= FormatDateTime('yyyy-mm-dd hh:mm:ss', N ...

  9. Delphi在创建和使用DLL的时候如果使用到string,请引入ShareMem单元

    当使用了长字符串类型的参数.变量时,如string,要引用ShareMem. 虽然Delphi中的string功能很强大,但若是您编写的Dll文件要供其它编程语言调用时,最好使用PChar类型.如果您 ...

随机推荐

  1. Android5.1图库Gallery2代码分析数据加载流程

    图片数据加载流程. Gallery---->GalleryActivity------>AlbumSetPage------->AlbumPage--------->Photo ...

  2. 【python】一个备份把文件备份到邮箱的python实现

    公司服务器弄了跳板机,从服务器上拉文件变得好麻烦,弄了个脚本从服务器上向邮箱发送文件,还蛮方便哈- #!/usr/bin/env python2.7 #! coding:UTF-8 import sm ...

  3. jQuery常用的查找Dom元素方法

    废话不多说,先来个总结,然后下面是demo 一. 同级节点之间的检索(检索深度N=0) next()是在兄弟节点中,往后匹配; prev()是在兄弟节点中,往前匹配. 二. 父级/子级节点的检索(检索 ...

  4. COCOS2DX遇到的问题

    1 新增类无法在别的类里面include到的问题: 去项目-> 配置属性-> C/C++-> 常规-> 附加包含目录中增加类的文件夹路径 2 新增类出现 extensions\ ...

  5. python字符串,列表,字典的常用方法

    本篇内容 字符串的常用方法 列表的常用方法 字典的常用方法 字符串的常用方法 center 字符居中显示,指定字符串长度,填充指定的填充字符 string = "40kuai" p ...

  6. js 日期控件laydate使用

    官网  http://sentsin.com/layui/laydate/ 1. 下载官网上的压缩包,解压后只需要复制laydate 文件夹到你的项目中; 2. 在页面引入  <script t ...

  7. STL中的所有算法(70个)

    STL中的所有算法(70个)----9种类型(略有修改by crazyhacking) 参考自: http://www.cppblog.com/mzty/archive/2007/03/14/1981 ...

  8. 如何实现简单的位数组(bit array)(转)

    源:如何实现简单的位数组(bit array) 在 comp.lang.c 上面看到一则不错的 FAQ,<How can I implement sets or arrays of bits?& ...

  9. Spring接口编程_设值注入和构造注入

    说明: UserManagerImp是设值注入,UserManagerImp2是构造注入 接口不注入,也就是在Spring配置文件中没有接口的<bean>,但是定义的时候是用接口 priv ...

  10. iOS开发——NSDate(待续...)

    1.获取当前系统时间,毫秒级 - (void)viewDidLoad { [super viewDidLoad]; NSString *currentTime = [self getCurrentTi ...