澳门新萄京一些常用的图像处理方法,将HTML导出
分类:澳门新萄京最大平台

采纳 canvas 实现数据压缩

2016/03/15 · HTML5 · 1 评论 · Canvas

原版的书文出处: EtherDream   

前端完结 SVG 转 PNG

2015/11/16 · JavaScript · PNG, SVG

初藳出处: 百度FEX - zhangbobell   

将HTML导出生成word文书档案,

 

正文实例为我们享受了js图片预览和上传的求实代码,供我们参照他事他说加以考查,具体内容如下

public class ImageProcessHelper {

前言

HTTP 扶助 GZip 压缩,可节约不知凡几传输财富。但可惜的是,独有下载才有,上传并不帮衬。

假设上传也能减小,那就完备了。特别符合多量文件提交的场面,举个例子和讯,正是很好的例子。

即使正规不扶持「上传压缩」,但还能够团结来贯彻。

前言

svg 是生机勃勃种矢量图形,在 web 上利用很成千成万,可是众多时候由于使用的现象,平常要求将 svg 转为 png 格式,下载到本地等。随着浏览器对 HTML 5 的支撑度更加高,大家得以把 svg 转为 png 的劳作交给浏览器来产生。

前言:

项目支出中遇见了急需将HTML页面包车型客车剧情导出为一个word文书档案,所以有了这里小说。

本来,项目耗费又时间某个殷切,第不常间想到的是用插件,所以百度了下。下边就介绍四个导出word文档的艺术。

var dailiApply = { change: function  { evt.preventDefault(); var pic = document.getElementById, file = document.getElementById; var ext=file.value.substring(file.value.lastIndexOf; // gif在IE浏览器暂时无法显示 if(ext!='png'&&ext!='jpg'&&ext!='jpeg'){ alert("图片的格式必须为png或者jpg或者jpeg格式!"); return; } var isIE = navigator.userAgent.match!= null, isIE6 = navigator.userAgent.match!= null; if; var reallocalpath = document.selection.createRange().text; // IE6浏览器设置img的src为本地路径可以直接显示图片 if  { pic.src = reallocalpath; }else { // 非IE6版本的IE由于安全问题直接设置img的src无法显示本地图片,但是可以通过滤镜来实现 pic.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='image',src=""   reallocalpath   "")"; // 设置img的src为base64编码的透明图片 取消显示浏览器默认图片 pic.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='; } }else { var file_arr = file.files; var ul = $(".weui_uploader_files"); if { for { if(file_arr.hasOwnProperty { var f = file_arr[key]; var url = URL.createObjectURL; var reader = new FileReader; reader.readAsDataURL { reader._onload = function { // 拼接显示预览图片的html var list = $("

////////////////////////////////////////////////////////////////////

Flash

首要推荐方案当然是 Flash,究竟它提供了压缩 API。除了 zip 格式,还扶持 lzma 这种一级压缩。

因为是原生接口,所以质量超高。而且对应的 swf 文件,也要命小。

貌似方法

  1. 创建 imageimage,src = xxx.svg;
  2. 创立 canvas,dragImage 将图纸贴到 canvas 上;
  3. 动用 toDataUrl 函数,将 canvas 的象征为 url;
  4. new image, src = url, download = download.png;

但是,在调换的时候一时一时会蒙受如下的如下的四个问题:

法一:通过jquery.wordexport.js导出word

备注:兼容IE9以上

粗粗浏览了下jquery.wordexport.js插件的代码,掌握到了通过该插件能够导出文本和图纸,而图片首先通过canvas的款式

绘制,文本则需求再信任FileSaver.js插件,FileSaver.js插件则首要透过H5的文书操作新特点new Blob(卡塔尔(قطر‎和new FileReader(卡塔尔(英语:State of Qatar)

来兑现文件的导出。

插件源码:

FileSaver.js

澳门新萄京 1 1 /* FileSaver.js 2 * A saveAs() FileSaver implementation. 3 * 1.3.2 4 * 2016-06-16 18:25:19 5 * 6 * By Eli Grey, 7 * License: MIT 8 * See 9 */ 10 11 /*global self */ 12 /*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */ 13 14 /*! @source */ 15 16 var saveAs = saveAs || (function(view) { 17 "use strict"; 18 // IE <10 is explicitly unsupported 19 if (typeof view === "undefined" || typeof navigator !== "undefined" && /MSIE [1-9]./.test(navigator.userAgent)) { 20 return; 21 } 22 var 23 doc = view.document 24 // only get URL when necessary in case Blob.js hasn't overridden it yet 25 , get_URL = function() { 26 return view.URL || view.webkitURL || view; 27 } 28 , save_link = doc.createElementNS("", "a") 29 , can_use_save_link = "download" in save_link 30 , click = function(node) { 31 var event = new MouseEvent("click"); 32 node.dispatchEvent(event); 33 } 34 , is_safari = /constructor/i.test(view.HTMLElement) 35 , is_chrome_ios =/CriOS/[d] /.test(navigator.userAgent) 36 , throw_outside = function(ex) { 37 (view.setImmediate || view.setTimeout)(function() { 38 throw ex; 39 }, 0); 40 } 41 , force_saveable_type = "application/octet-stream" 42 // the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to 43 , arbitrary_revoke_timeout = 1000 * 40 // in ms 44 , revoke = function(file) { 45 var revoker = function() { 46 if (typeof file === "string") { // file is an object URL 47 get_URL().revokeObjectURL(file); 48 } else { // file is a File 49 file.remove(); 50 } 51 }; 52 setTimeout(revoker, arbitrary_revoke_timeout); 53 } 54 , dispatch = function(filesaver, event_types, event) { 55 event_types = [].concat(event_types); 56 var i = event_types.length; 57 while (i--) { 58 var listener = filesaver["on" event_types[i]]; 59 if (typeof listener === "function") { 60 try { 61 listener.call(filesaver, event || filesaver); 62 } catch (ex) { 63 throw_outside(ex); 64 } 65 } 66 } 67 } 68 , auto_bom = function(blob) { 69 // prepend BOM for UTF-8 XML and text/* types (including HTML) 70 // note: your browser will automatically convert UTF-16 U FEFF to EF BB BF 71 if (/^s*(?:text/S*|application/xml|S*/S* xml)s*;.*charsets*=s*utf-8/i.test(blob.type)) { 72 return new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type}); 73 } 74 return blob; 75 } 76 , FileSaver = function(blob, name, no_auto_bom) { 77 if (!no_auto_bom) { 78 blob = auto_bom(blob); 79 } 80 // First try a.download, then web filesystem, then object URLs 81 var 82 filesaver = this 83 , type = blob.type 84 , force = type === force_saveable_type 85 , object_url 86 , dispatch_all = function() { 87 dispatch(filesaver, "writestart progress write writeend".split(" ")); 88 } 89 // on any filesys errors revert to saving with object URLs 90 , fs_error = function() { 91 if ((is_chrome_ios || (force && is_safari)) && view.FileReader) { 92 // Safari doesn't allow downloading of blob urls 93 var reader = new FileReader(); 94 reader.onloadend = function() { 95 var url = is_chrome_ios ? reader.result : reader.result.replace(/^data:[^;]*;/, 'data:attachment/file;'); 96 var popup = view.open(url, '_blank'); 97 if(!popup) view.location.href = url; 98 url=undefined; // release reference before dispatching 99 filesaver.readyState = filesaver.DONE; 100 dispatch_all(); 101 }; 102 reader.readAsDataURL(blob); 103 filesaver.readyState = filesaver.INIT; 104 return; 105 } 106 // don't create more object URLs than needed 107 if (!object_url) { 108 object_url = get_URL().createObjectURL(blob); 109 } 110 if (force) { 111 view.location.href = object_url; 112 } else { 113 var opened = view.open(object_url, "_blank"); 114 if (!opened) { 115 // Apple does not allow window.open, see 116 view.location.href = object_url; 117 } 118 } 119 filesaver.readyState = filesaver.DONE; 120 dispatch_all(); 121 revoke(object_url); 122 } 123 ; 124 filesaver.readyState = filesaver.INIT; 125 126 if (can_use_save_link) { 127 object_url = get_URL().createObjectURL(blob); 128 setTimeout(function() { 129 save_link.href = object_url; 130 save_link.download = name; 131 click(save_link); 132 dispatch_all(); 133 revoke(object_url); 134 filesaver.readyState = filesaver.DONE; 135 }); 136 return; 137 } 138 139 fs_error(); 140 } 141 , FS_proto = FileSaver.prototype 142 , saveAs = function(blob, name, no_auto_bom) { 143 return new FileSaver(blob, name || blob.name || "download", no_auto_bom); 144 } 145 ; 146 // IE 10 (native saveAs) 147 if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) { 148 return function(blob, name, no_auto_bom) { 149 name = name || blob.name || "download"; 150 151 if (!no_auto_bom) { 152 blob = auto_bom(blob); 153 } 154 return navigator.msSaveOrOpenBlob(blob, name); 155 }; 156 } 157 158 FS_proto.abort = function(){}; 159 FS_proto.readyState = FS_proto.INIT = 0; 160 FS_proto.WRITING = 1; 161 FS_proto.DONE = 2; 162 163 FS_proto.error = 164 FS_proto.onwritestart = 165 FS_proto.onprogress = 166 FS_proto.onwrite = 167 FS_proto.onabort = 168 FS_proto.onerror = 169 FS_proto.onwriteend = 170 null; 171 172 return saveAs; 173 }( 174 typeof self !== "undefined" && self 175 || typeof window !== "undefined" && window 176 || this.content 177 )); 178 // `self` is undefined in Firefox for Android content script context 179 // while `this` is nsIContentFrameMessageManager 180 // with an attribute `content` that corresponds to the window 181 182 if (typeof module !== "undefined" && module.exports) { 183 module.exports.saveAs = saveAs; 184 } else if ((typeof define !== "undefined" && define !== null) && (define.amd !== null)) { 185 define([], function() { 186 return saveAs; 187 }); 188 } View Code

jquery.wordexport.js

澳门新萄京 2 1 if (typeof jQuery !== "undefined" && typeof saveAs !== "undefined") { 2 (function($) { 3 $.fn.wordExport = function(fileName) { 4 fileName = typeof fileName !== 'undefined' ? fileName : "jQuery-Word-Export"; 5 var static = { 6 mhtml: { 7 top: "Mime-Version: 1.0nContent-Base: " location.href "nContent-Type: Multipart/related; boundary="NEXT.ITEM-BOUNDARY";type="text/html"nn--NEXT.ITEM-BOUNDARYnContent-Type: text/html; charset="utf-8"nContent-Location: " location.href "nn<!DOCTYPE html>n<html>n_html_</html>", 8 head: "<head>n<meta http-equiv="Content-Type" content="text/html; charset=utf-8">n<style>n_styles_n</style>n</head>n", 9 body: "<body>_body_</body>" 10 } 11 }; 12 var options = { 13 maxWidth: 624 14 }; 15 // Clone selected element before manipulating it 16 var markup = $(this).clone(); 17 18 // Remove hidden elements from the output 19 markup.each(function() { 20 var self = $(this); 21 if (self.is(':hidden')) 22 self.remove(); 23 }); 24 25 // Embed all images using Data URLs 26 var images = Array(); 27 var img = markup.find('img'); 28 for (var i = 0; i < img.length; i ) { 29 // Calculate dimensions of output image 30 var w = Math.min(img[i].width, options.maxWidth); 31 var h = img[i].height * (w / img[i].width); 32 // Create canvas for converting image to data URL 33 var canvas = document.createElement("CANVAS"); 34 canvas.width = w; 35 canvas.height = h; 36 // Draw image to canvas 37 var context = canvas.getContext('2d'); 38 context.drawImage(img[i], 0, 0, w, h); 39 // Get data URL encoding of image 40 var uri = canvas.toDataURL("image/png/jpg"); 41 $(img[i]).attr("src", img[i].src); 42 img[i].width = w; 43 img[i].height = h; 44 // Save encoded image to array 45 images[i] = { 46 type: uri.substring(uri.indexOf(":") 1, uri.indexOf(";")), 47 encoding: uri.substring(uri.indexOf(";") 1, uri.indexOf(",")), 48 location: $(img[i]).attr("src"), 49 data: uri.substring(uri.indexOf(",") 1) 50 }; 51 } 52 53 // Prepare bottom of mhtml file with image data 54 var mhtmlBottom = "n"; 55 for (var i = 0; i < images.length; i ) { 56 mhtmlBottom = "--NEXT.ITEM-BOUNDARYn"; 57 mhtmlBottom = "Content-Location: " images[i].location "n"; 58 mhtmlBottom = "Content-Type: " images[i].type "n"; 59 mhtmlBottom = "Content-Transfer-Encoding: " images[i].encoding "nn"; 60 mhtmlBottom = images[i].data "nn"; 61 } 62 mhtmlBottom = "--NEXT.ITEM-BOUNDARY--"; 63 64 //TODO: load css from included stylesheet 65 66 //var styles=' /* Font Definitions */@font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 680460288 22 0 262145 0;} @font-face{font-family:"Cambria Math";panose-1:2 4 5 3 5 4 6 3 2 4;mso-font-charset:1;mso-generic-font-family:roman;mso-font-format:other;mso-font-pitch:variable;mso-font-signature:0 0 0 0 0 0;} @font-face{font-family:"@宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 680460288 22 0 262145 0;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-unhide:no;mso-style-qformat:yes;mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;mso-pagination:widow-orphan;font-size:14.0pt;font-family:宋体;mso-bidi-font-family:燕体;}p.MsoHeader, li.MsoHeader, div.MsoHeader{mso-style-noshow:yes;mso-style-priority:99;mso-style-link:"页眉 Char";margin:0cm;margin-bottom:.0001pt;text-align:center;mso-pagination:widow-orphan;layout-grid-mode:char;font-size:9.0pt;font-family:大篆;mso-bidi-font-family:大篆;}p.MsoFooter, li.MsoFooter, div.MsoFooter{mso-style-noshow:yes;mso-style-priority:99;mso-style-link:"页脚 Char";margin:0cm;margin-bottom:.0001pt;mso-pagination:widow-orphan;layout-grid-mode:char;font-size:9.0pt;font-family:金鼎文;mso-bidi-font-family:楷体;}p.MsoAcetate, li.MsoAcetate, div.MsoAcetate{mso-style-noshow:yes;mso-style-priority:99;mso-style-link:"注解框文本 Char";margin:0cm;margin-bottom:.0001pt;mso-pagination:widow-orphan;font-size:9.0pt;font-family:黑体;mso-bidi-font-family:黑体;}span.Char{mso-style-name:"页眉 Char";mso-style-noshow:yes;mso-style-priority:99;mso-style-unhide:no;mso-style-locked:yes;mso-style-link:页眉;font-family:金鼎文;mso-ascii-font-family:小篆;mso-fareast-font-family:黑体;mso-hansi-font-family:石籀文;}span.Char0{mso-style-name:"页脚 Char";mso-style-noshow:yes;mso-style-priority:99;mso-style-unhide:no;mso-style-locked:yes;mso-style-link:页脚;font-family:石籀文;mso-ascii-font-family:石籀文;mso-fareast-font-family:钟鼓文;mso-hansi-font-family:黑体;}span.Char1{mso-style-name:"批注框文本 Char";mso-style-noshow:yes;mso-style-priority:99;mso-style-unhide:no;mso-style-locked:yes;mso-style-link:讲解框文本;font-family:陶文;mso-ascii-font-family:燕体;mso-fareast-font-family:金鼎文;mso-hansi-font-family:草书;}p.msochpdefault, li.msochpdefault, div.msochpdefault{mso-style-name:msochpdefault;mso-style-unhide:no;mso-margin-top-alt:auto;margin-right:0cm;mso-margin-bottom-alt:auto;margin-left:0cm;mso-pagination:widow-orphan;font-size:10.0pt;font-family:石籀文;mso-bidi-font-family:钟鼓文;}span.msonormal0{mso-style-name:msonormal;mso-style-unhide:no;}.MsoChpDefault{mso-style-type:export-only;mso-default-props:yes;font-size:10.0pt;mso-ansi-font-size:10.0pt;mso-bidi-font-size:10.0pt;mso-ascii-font-family:"Times New 罗曼";mso-hansi-font-family:"Times New 罗曼";mso-font-kerning:0pt;}/* Page Definitions */ @page WordSection1{size:595.3pt 841.9pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;mso-header-margin:42.55pt;mso-footer-margin:49.6pt;mso-paper-source:0;}div.WordSection1{page:WordSection1;}'; 67 68 var styles=""; 69 70 // Aggregate parts of the file together 71 var fileContent = static.mhtml.top.replace("_html_", static.mhtml.head.replace("_styles_", styles) static.mhtml.body.replace("_body_", markup.html())) mhtmlBottom; 72 73 // Create a Blob with the file contents 74 var blob = new Blob([fileContent], { 75 type: "application/msword;charset=utf-8" 76 }); 77 saveAs(blob, fileName ".doc"); 78 }; 79 })(jQuery); 80 } else { 81 if (typeof jQuery === "undefined") { 82 console.error("jQuery Word Export: missing dependency (jQuery)"); 83 } 84 if (typeof saveAs === "undefined") { 85 console.error("jQuery Word Export: missing dependency (FileSaver.js)"); 86 } 87 } View Code

插件调用:

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title>生成word文档</title>
 6 </head>
 7 <body lang=ZH-CN style='tab-interval:21.0pt'>
 8 <div class="word">
 9     <p align="center">10 </div>
11 <input type="button" value="导出word">
12 <script src="http://www.xb-cj.com/uploads/allimg/191212/0P6155615-2.jpg"></script>
13 <script type="text/javascript" src="js/FileSaver.js"></script>
14 <script type="text/javascript" src="js/jquery.wordexport.js"></script>
15 <script>
16     $(function(){
17         $("input[type='button']").click(function(event) {
18             $(".word").wordExport('生成word文档');
19         });
20     })
21 </script>
22 </body>
23 </html>

从来调用wordExport(卡塔尔国接口就可以导出word文档,传的参数为导出的word文件名。

补充:

由此大家例行写的外联样式设置样式是无用的,通过个人的试行发掘必要写内联样式工夫见效,而单位也亟需依据word的构造

单位pt设置。

而jquery.wordexport.js插件是要布置了个style样式让大家补充样式设置的:

澳门新萄京 3

然则个人推行了下,设置的体裁却一点计策也施展不出生效,只好通过内联设置才生效。

截图:

澳门新萄京 4澳门新萄京 5

" "" "X

    private ImageProcessHelper() {

JavaScript

Flash 慢慢淘汰,但代表的 HTML5,却从未提供压缩 API。只能本身用 JS 达成。

那固然平价,但运行速度就慢多了,况且相应的 JS 也十分的大。

尽管代码有 50kb,而数据压缩后只小 10kb,那就不足了。除非量大,才有意义。

主题材料 1 :浏览器对 canvas 限定

Canvas 的 W3C 的专门的学问上从不聊到 canvas 的最大高/宽度和面积,可是每一种厂家的浏览器出于浏览器品质的设想,在分化的平台上设置了最大的高/宽度只怕是渲染面积,超越了那一个阈值渲染的结果会是白手。测量试验了二种浏览器的 canvas 质量如下:

  • chrome (版本 46.0.2490.80 (64-bit))
    • 最大范围:268, 435, 456 px^2 = 16, 384 px * 16, 384 px
    • 最大宽/高:32, 767 px
  • firefox (版本 42.0)
    • 最大规模:32, 767 px * 16, 384 px
    • 最大宽/高:32, 767px
  • safari (版本 9.0.1 (11601.2.7.2))
    • 最大范围: 268, 435, 456 px^2 = 16, 384 px * 16, 384 px
  • ie 10(版本 10.0.9200.17414)
    • 最大宽/高: 8, 192px * 8, 192px

在相像的 web 应用中,大概超级少会超越这个限定。可是,假如领先了那一个节制,则 会招致导出为空白或然出于内部存款和储蓄器走漏招致浏览器崩溃。

再就是从单平素说, 导出 png 也是黄金年代项很开销内部存款和储蓄器的操作,粗略猜测一下,导出 16, 384 px * 16, 384 px 的 svg 会消耗 16384 * 16384 * 4 / 1024 / 1024 = 1024 M 的内部存款和储蓄器。所以,在周围这么些极限值的时候,浏览器也会 反应变慢,能还是无法导出成功也跟系统的可用内部存款和储蓄器大小等等皆有提到。

对于那么些主题素材,犹如下三种缓和情势:

  1. 将数据发送给后端,在后端实现 转换;
  2. 前端将 svg 切分成四个图片导出;

先是种方法能够运用 PhantomJS、inkscape、ImageMagick 等工具,相对来说较易,这里大家第生机勃勃索求第两种缓慢解决措施。

法二:通过百度js模板引擎生成word文书档案

关键是经过js模板设置相应的价签,然后XDoc.to(baidu.template(卡塔尔(英语:State of Qatar)卡塔尔(英语:State of Qatar)导出word,而透过百度js模板引擎的补益是也能够导出PDF文件。

完整demo:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="UTF-8">
 5     <script type="text/javascript" src="http://www.xb-cj.com/uploads/allimg/191212/0P6154462-6.jpg"></script>
 6     <script type="text/javascript" src="http://www.xdocin.com/baiduTemplate.js"></script>
 7     <style>
 8         .head{
 9             font-size: 29px;
10             display: block;
11         }
12         .content{
13             display: block;
14         }
15     </style>
16 </head>
17 <body>
18 <input type="button" onclick="gen('pdf')" value="生成PDF"/>
19 <input type="button" onclick="gen('docx')" value="生成Word"/>
20 <br/>
21 <script id="tmpl" type="text/html">
22     <xdoc version="A.3.0">
23         <body>
24         <para heading="1" lineSpacing="28">
25             <text class="head" valign="center" fontName="标宋" fontSize="29"><%=title%></text>
26         </para>
27         <para>
28             <img  src="<%=img%>" sizeType="autosize"/>
29         </para>
30         <para lineSpacing="9">
31             <text class="content" fontName="仿宋" fontSize="18"><%=content%></text>
32         </para>
33         </body>
34     </xdoc>
35 </script>
36 <script src="http://www.xb-cj.com/uploads/allimg/191212/0P6155615-2.jpg"></script>
37 <script type="text/javascript">
38     var type="docx";//pdf
39     var data = {
40         title: "导出" type "文件",
41         img: "http://www.wordlm.com/uploads/allimg/130101/1_130101000405_1.jpg",
42         content: "我这样就可以导出" type "格式的文件了,是不是很方便",
43     };
44     function renderTemplate(){
45         var template=$("#tmpl").html();
46         var html=template.replace(/<%=title%>/,data.title)
47                 .replace(/<%=img%>/,data.img)
48                 .replace(/<%=content%>/,data.content);
49         $("body").append(html);
50     }
51     renderTemplate();
52     function gen(type) {
53         XDoc.to(baidu.template('tmpl', data), type, {}, "_blank");
54     }
55     console.log('http://www.xdocin.com/xml.html');
56 </script>
57 </body>
58 </html>  

此间自个儿透过renderTemplate函数叫js模板渲染到HTML中,实现了文本的显得和导出内容的组合。而因为此地导出的word文书档案是索要特意设置样式的,所以在页面样式展现下大家可以通过增加.class的方法设置。

附部分导出word文档样式设置:

澳门新萄京 6

 

截图:

澳门新萄京 7澳门新萄京 8

 

越来越多参谋:

FileSave.js:

百度导出文书档案模板:

 

前言: 项目费用中相见了供给将HTML页面包车型的士内容导出为二个word文书档案,所以有了这里小说。 当然,项目开采又时间有...

"卡塔尔国; ul.append; // 将转速后的图纸地址坐落于img中 var pic = document.getElementById; //pic.src = this.result; pic.src=url; dailiApply.compress; //images.push; document.getElementById.add伊夫ntListener { $.remove; }; reader._onload(); }else { $.alert; n = 6; } } } }else { $.alert; } } return false; }, /** * @param {Object} f input选拔的图形 必填 * @param {String} quality 图片压缩的成色[0, 1] * @param {String} output_img_type 输出图片的项目 */ compress: function (f, quality, output_img_type) { var mime_type = "image/jpeg"; if(output_img_type!=undefined && output_img_type=="image/png"){ mime_type = "image/png"; } createImageBitmap.then { var max = 1000; // 设置最大分辨率 var c_w = ''; var c_h = ''; var width = imageBitmap.width; var height = imageBitmap.height; // 等比例缩放 if (width > max || height > max卡塔尔 { if { c_w = max; c_h = max * height / width; } else { c_h = max; c_w = max * width / height; } }else { // 不缩放 c_w = width; c_h = height; } var canvas = document.createElement; canvas.width = c_w; canvas.height = c_h; var ctx = canvas.getContext; ctx.drawImage(imageBitmap,0,0, width, height, 0, 0, c_w, c_h); canvas.toBlob{ images.push; },mime_type, quality); }); }, submit: function () { var content = $.val; var xhr = new XMLHttpRequest(); var fd = new FormData(document.getElementById; $.each{ fd.append; fd.append; fd.append("substationproxyId", 8); console.log; console.log { $.ajax({ url: CONFIG.API.addSubProxyRecruit, type: "POST", data: fd, processData: false, // tell jQuery not to process the data contentType: false, // tell jQuery not to set contentType beforeSend: function { $.showLoading.prop }, success: function { console.log; $.hideLoading.prop; if { $.alert { location.reload }else if(json.errorCode == "-101") { $.alert('出错:' json.message, function () { location.href = CONFIG.HTML.login; }); }else { $.alert(json.message, function ; }else { $.alert; } } };

}

其他

是不是不要 JS,而是接收一些接口,直接达成减弱?

实质上,在 HTML5 刚面世时,就留神到了多个职能:canvas 导出图片。能够调换jpg、png 等格式。

倘若在观念的话,相信你也想开了。没有错,正是 png —— 它是无损压缩的。

大家把普通数据当成像素点,画到 canvas 上,然后导出成 png,就是三个特有的滑坡包了~


上边开始索求。。。

svg 切分成八个图片导出

思路:浏览器尽管对 canvas 有尺寸和面积的限定,可是对于 image 成分并未猛烈的限量,也正是率先步生成的 image 其实展现是正规的,大家要做的只是在其次步 dragImage 的时候分多次将 image 成分切分并贴到 canvas 上然后下载下来。 同期,应留意到 image 的载入是一个异步的进度。

重要代码

JavaScript

// 布局 svg Url,此处省略将 svg 经字符过滤后转为 url 的进度。 var svgUrl = DomULX570L.createObjectU帕杰罗L(blob卡塔尔(قطر‎; var svgWidth = document.querySelector('#kity_svg').getAttribute('width'); var svgHeight = document.querySelector('#kity_svg'卡塔尔.getAttribute('height'卡塔尔国; // 分片的肥瘦和冲天,可依附浏览器做适配 var w0 = 8192; var h0 = 8192; // 每行和每列能宽容的分片数 var M = Math.ceil(svgWidth / w0卡塔尔国; var N = Math.ceil(svgHeight / h0卡塔尔国; var idx = 0; loadImage(svgUrl卡塔尔国.then(function(img卡塔尔(英语:State of Qatar) { while(idx < M * N卡塔尔国 { // 要分开的面片在 image 上的坐标和尺寸 var targetX = idx % M * w0, targetY = idx / M * h0, targetW = (idx 1) % M ? w0 : (svgWidth - (M - 1) * w0), targetH = idx >= (N - 1) * M ? (svgHeight - (N - 1) * h0卡塔尔(英语:State of Qatar) : h0; var canvas = document.createElement('canvas'卡塔尔国, ctx = canvas.getContext('2d'卡塔尔国; canvas.width = targetW; canvas.height = targetH; ctx.drawImage(img, targetX, targetY, targetW, targetH, 0, 0, targetW, targetH卡塔尔(英语:State of Qatar); console.log('now it is ' idx卡塔尔(英语:State of Qatar); // 打算在前边多个下载 var a = document.createElement('a'卡塔尔; a.download = 'naotu-' idx '.png'; a.href = canvas.toDataUPRADOL('image/png'卡塔尔国; var clickEvent = new Mouse伊芙nt('click', { 'view': window, 'bubbles': true, 'cancelable': false }卡塔尔(英语:State of Qatar); a.dispatch伊夫nt(click伊夫nt卡塔尔; idx ; } }, function(err卡塔尔(قطر‎ { console.log(err卡塔尔(قطر‎; }卡塔尔; // 加载 image function loadImage(url卡塔尔 { return new Promise(function(resolve, reject卡塔尔国 { var image = new Image(卡塔尔; image.src = url; image.crossOrigin = 'Anonymous'; image.onload = function(卡塔尔 { resolve(this卡塔尔(قطر‎; }; image.onerror = function(err卡塔尔(英语:State of Qatar) { reject(err卡塔尔(قطر‎; }; }卡塔尔(英语:State of Qatar); }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 构造 svg Url,此处省略将 svg 经字符过滤后转为 url 的过程。
var svgUrl = DomURL.createObjectURL(blob);
var svgWidth = document.querySelector('#kity_svg').getAttribute('width');
var svgHeight = document.querySelector('#kity_svg').getAttribute('height');
 
// 分片的宽度和高度,可根据浏览器做适配
var w0 = 8192;
var h0 = 8192;
 
// 每行和每列能容纳的分片数
var M = Math.ceil(svgWidth / w0);
var N = Math.ceil(svgHeight / h0);
 
var idx = 0;
loadImage(svgUrl).then(function(img) {
 
    while(idx < M * N) {
        // 要分割的面片在 image 上的坐标和尺寸
        var targetX = idx % M * w0,
            targetY = idx / M * h0,
            targetW = (idx 1) % M ? w0 : (svgWidth - (M - 1) * w0),
            targetH = idx >= (N - 1) * M ? (svgHeight - (N - 1) * h0) : h0;
 
        var canvas = document.createElement('canvas'),
            ctx = canvas.getContext('2d');
 
            canvas.width = targetW;
            canvas.height = targetH;
 
            ctx.drawImage(img, targetX, targetY, targetW, targetH, 0, 0, targetW, targetH);
 
            console.log('now it is ' idx);
 
            // 准备在前端下载
            var a = document.createElement('a');
            a.download = 'naotu-' idx '.png';
            a.href = canvas.toDataURL('image/png');
 
            var clickEvent = new MouseEvent('click', {
                'view': window,
                'bubbles': true,
                'cancelable': false
            });
 
            a.dispatchEvent(clickEvent);
 
        idx ;
    }
 
}, function(err) {
    console.log(err);
});
 
// 加载 image
function loadImage(url) {
    return new Promise(function(resolve, reject) {
        var image = new Image();
 
        image.src = url;
        image.crossOrigin = 'Anonymous';
        image.onload = function() {
            resolve(this);
        };
 
        image.onerror = function(err) {
            reject(err);
        };
    });
}

说明:

  1. 出于在后面一个下载有浏览器包容性、客户体验等主题材料,在其实中,可能需求将转换后的数据发送到后端,并视作叁个滑坡包下载。
  2. 分片的尺寸这里运用的是 8192 * 9192,在实际上中,为了加强宽容性和心得,能够依靠浏览器和平台做适配,比如在 iOS 下的 safari 的最大范围是 4096 *4096。

上述正是本文的全部内容,希望对我们的求学抱有利于,也冀望大家多多点拨脚本之家。

private static class HelperTemp {

数据调换

数码转像素,并不麻烦。1 个像素能够容纳 4 个字节:

R = bytes[0] G = bytes[1] B = bytes[2] A = bytes[3]

1
2
3
4
R = bytes[0]
G = bytes[1]
B = bytes[2]
A = bytes[3]

实质上有现有的办法,可批量将数据填充成像素:

img = new ImageData(bytes, w, h); context.putImageData(img, w, h)

1
2
img = new ImageData(bytes, w, h);
context.putImageData(img, w, h)

唯独,图片的宽高怎么样设定?

标题 2 :导出包蕴图表的 svg

在导出的时候,还或然会遇到另贰个标题:如若 svg 里面富含图表,你会意识经过上述措施导出的 png 里面,原本的图纸是不显得的。日常感觉是 svg 里面蕴含的图样跨域了,不过假若你把这些图形换花销域的图片,依旧会鬼使神差这种景色。澳门新萄京 9

图表中上有些是导出前的 svg,下图是导出后的 png。svg 中的图片是本域的,在导出后不出示。

private static ImageProcessHelperhelper =new ImageProcessHelper();

尺寸设定

最轻易易行的,便是用 1px 的莫斯中国科学技术大学学。比如有 1000 个像素,则填在 1000 x 1 的图样里。

但万意气风发有 10000 像素,就不可行了。因为 canvas 的尺寸,是有限量的。

不等的浏览器,最大尺寸差异等。有 4096 的,也有 32767 的。。。

以最大 4096 为例,如若每趟都用这几个幅度,显明不创设。

举例有 n = 4100 个像素,大家应用 4096 x 2 的尺寸:

| 1 | 2 | 3 | 4 | ... | 4095 | 4096 | | 4097 | 4098 | 4099 | 4100 | ...... 未利用 ......

1
2
| 1    | 2    | 3    | 4    | ...  | 4095 | 4096 |
| 4097 | 4098 | 4099 | 4100 | ...... 未利用 ......

第二行只用到 4 个,剩下的 4092 个都空着了。

但 4100 = 41 * 100。假使用这一个尺寸,就不会有浪费。

进而,得对 n 分解因数:

n = w * h

1
n = w * h

如此就能够将 n 个像素,恰恰填满 w x h 的图纸。

但 n 是质数的话,就无解了。当时浪费就不可制止了,只是,怎么样技术浪费起码?

于是乎就改成那样一个标题:

怎样用 n m 个点,拼成一个 w x h 的矩形(0

杜撰到 MAX 十分小,穷举就能够。

咱俩遍历 h,总括相应的 w = ceil(n / h卡塔尔(قطر‎, 然后寻觅最周围 n 的 w * h。

var beg = Math.ceil(n / MAX卡塔尔; var end = Math.ceil(Math.sqrt(n卡塔尔(英语:State of Qatar)卡塔尔国; var minSize = 9e9; var bestH = 0, // 最后结果 bestW = 0; for (h = beg; h end; h 卡塔尔(英语:State of Qatar) { var w = Math.ceil(n / h卡塔尔; var size = w * h; if (size minSize) { minSize = size; bestW = w; bestH = h; } if (size == n) { break; } }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var beg = Math.ceil(n / MAX);
var end = Math.ceil(Math.sqrt(n));
 
var minSize = 9e9;
 
var bestH = 0,          // 最终结果
    bestW = 0;
 
for (h = beg; h  end; h ) {
    var w = Math.ceil(n / h);
    var size = w * h;
 
    if (size  minSize) {
        minSize = size;
        bestW = w;
        bestH = h;
    }
    if (size == n) {
        break;
    }
}

因为 w * h 和 h * w 是平等的,所以只需遍历到 sqrt(n卡塔尔(قطر‎ 就能够。

平等,也没有须求从 1 开端,从 n / MAX 就可以。

这么,大家就能够找到最适合的图片尺寸。

自然,延续的空白像素,最后减掉后会相当的小。这一步其实并不非常主要。

主题材料根源

大家根据小说最开首提议的步子,稳步各个核查,会发觉在首先步的时候,svg 中的图片就不显得了。也便是,当 image 成分的 src 为三个 svg,并且 svg 里面包涵图表,那么被含有的图片是不博览会示的,就算那几个图片是本域的。

W3C 关于这一个难题并从未 做表达,最终在  找到了有关这么些主题材料的申明。 意思是:幸免这么做是由于安全构思,svg 里面援用的富有 外界财富 饱含image, stylesheet, script 等都会被截留。

其间还举了三个事例:借使未有这一个范围,假若三个论坛允许顾客上传这样的 svg 作为头像,就有十分大希望现身那样的景色,一人红客上传 svg 作为头像,里面包含代码:<image xlink:href="http://evilhacker.com/myimage.png">(假若那位黑客具备对于 evil骇客.com 的调控权),那么那位黑客就全盘能不负职责下边包车型地铁政工:

  • 倘诺有人查看他的素材,evil骇客.com 就能接受到二遍 ping 的倡议(进而能够得到查看者的 ip);
  • 能够产生对于分歧的 ip 地址的人出示不均等的头像;
  • 能够每四日转变头像的外观(而不用经过论坛管理员的核算)。

看看此间,大约就通晓了一切难题的全进程了,当然还应该有少数原因恐怕是制止图像递归。

}

渲染难题

定下尺寸,大家就能够「渲染数据」了。

而是现实中,总某个意外的坑。canvas 也不例外:

<canvas id="canvas" width="100" heigth="100"></canvas> <script> var ctx = canvas.getContext('2d'卡塔尔国; // 写入的数量 var bytes = [100, 101, 102, 103]; var buf = new Uint8ClampedArray(bytes卡塔尔(英语:State of Qatar); var img = new ImageData(buf, 1, 1卡塔尔国; ctx.putImageData(img, 0, 0卡塔尔(英语:State of Qatar); // 读取的数据 img = ctx.getImageData(0, 0, 1, 1卡塔尔(英语:State of Qatar); console.log(img.data卡塔尔(英语:State of Qatar); // chrome [99, 102, 102, 103] // firefox [101, 101, 103, 103] // ... </script>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<canvas id="canvas" width="100" heigth="100"></canvas>
<script>
  var ctx = canvas.getContext('2d');
 
  // 写入的数据
  var bytes = [100, 101, 102, 103];
 
  var buf = new Uint8ClampedArray(bytes);
  var img = new ImageData(buf, 1, 1);
  ctx.putImageData(img, 0, 0);
 
  // 读取的数据
  img = ctx.getImageData(0, 0, 1, 1);
  console.log(img.data);
  // chrome  [99,  102, 102, 103]
  // firefox [101, 101, 103, 103]
  // ...
</script>

读取的像素,居然和写入的有差错!何况分化的浏览器,偏差还区别等。

原先,浏览器为了巩固渲染品质,有贰个 Premultiplied Alpha 的编写制定。可是,那会就义局地精度!

固然视觉上并不显明,但用于数据存款和储蓄,就至极了。

如何禁止使用它?后生可畏番尝试都没得逞。于是,只好从数额上雕刻了。

意气风发经不应用 Alpha 通道,又会怎么?

// 写入的多寡 var bytes = [100, 101, 102, 255]; ... console.log(img.data); // [100, 101, 102, 255]

1
2
3
4
  // 写入的数据
  var bytes = [100, 101, 102, 255];
  ...
  console.log(img.data);  // [100, 101, 102, 255]

这样,倒是避开了难题。

一句话来说,只好从数量上开头,跳过 Alpha 通道:

// pixel 1 new_bytes[0] = bytes[0] // R new_bytes[1] = bytes[1] // G new_bytes[2] = bytes[2] // B new_bytes[3] = 255 // A // pixel 2 new_bytes[4] = bytes[3] // R new_bytes[5] = bytes[4] // G new_bytes[6] = bytes[5] // B new_bytes[7] = 255 // A ...

1
2
3
4
5
6
7
8
9
10
11
12
13
// pixel 1
new_bytes[0] = bytes[0]     // R
new_bytes[1] = bytes[1]     // G
new_bytes[2] = bytes[2]     // B
new_bytes[3] = 255          // A
 
// pixel 2
new_bytes[4] = bytes[3]     // R
new_bytes[5] = bytes[4]     // G
new_bytes[6] = bytes[5]     // B
new_bytes[7] = 255          // A
 
...

那个时候,就不受 Premultiplied Alpha 的影响了。

由于简单,也能够 1 像素存 1 字节:

// pixel 1 new_bytes[0] = bytes[0] new_bytes[1] = 255 new_bytes[2] = 255 new_bytes[3] = 255 // pixel 2 new_bytes[4] = bytes[1] new_bytes[5] = 255 new_bytes[6] = 255 new_bytes[7] = 255 ...

1
2
3
4
5
6
7
8
9
10
11
12
13
// pixel 1
new_bytes[0] = bytes[0]
new_bytes[1] = 255
new_bytes[2] = 255
new_bytes[3] = 255
 
// pixel 2
new_bytes[4] = bytes[1]
new_bytes[5] = 255
new_bytes[6] = 255
new_bytes[7] = 255
 
...

那样,整个图片最三独有 256 色。借使能导出成「索引型 PNG」的话,也是能够尝试的。

消除办法

思路:由于安全因素,其实首先步的时候,图片已经显得不出去了。那么大家明天考虑的方式是在首先步之后遍历 svg 的布局,将具有的 image 成分的 url、地方和尺寸保存下去。在第三步之后,按梯次贴到 canvas 上。那样,最终导出的 png 图片就能够有 svg 里面的 image。第一代码

JavaScript

// 此处略去变通 svg url 的历程 var svgUrl = DomUEvoqueL.createObjectU中华VL(blob卡塔尔; var svgWidth = document.querySelector('#kity_svg').getAttribute('width'); var svgHeight = document.querySelector('#kity_svg').getAttribute('height'); var embededImages = document.querySelectorAll('#kity_svg image'卡塔尔国; // 由 nodeList 转为 array embededImages = Array.prototype.slice.call(embededImages卡塔尔(英语:State of Qatar); // 加载底层的图 loadImage(svgUrl卡塔尔.then(function(img卡塔尔 { var canvas = document.createElement('canvas'卡塔尔国, ctx = canvas.getContext("2d"卡塔尔(英语:State of Qatar); canvas.width = svgWidth; canvas.height = svgHeight; ctx.drawImage(img, 0, 0卡塔尔国; // 遍历 svg 里面装有的 image 成分embededImages.reduce(function(sequence, svgImg卡塔尔(英语:State of Qatar){ return sequence.then(function(卡塔尔 { var url = svgImg.getAttribute('xlink:href'卡塔尔国 'abc', dX = svgImg.getAttribute('x'卡塔尔(قطر‎, dY = svgImg.getAttribute('y'卡塔尔(英语:State of Qatar), dWidth = svgImg.getAttribute('width'卡塔尔(英语:State of Qatar), dHeight = svgImg.getAttribute('height'卡塔尔国; return loadImage(url卡塔尔.then(function( sImg卡塔尔(قطر‎ { ctx.drawImage(sImg, 0, 0, sImg.width, sImg.height, dX, dY, dWidth, dHeight); }, function(err卡塔尔 { console.log(err卡塔尔(英语:State of Qatar); }卡塔尔国; }, function(err卡塔尔 { console.log(err卡塔尔(英语:State of Qatar); }卡塔尔; }, Promise.resolve(卡塔尔卡塔尔(英语:State of Qatar).then(function(卡塔尔(英语:State of Qatar) { // 策画在前端下载 var a = document.createElement("a"卡塔尔国; a.download = 'download.png'; a.href = canvas.toDataUPRADOL("image/png"卡塔尔(قطر‎; var click伊芙nt = new Mouse伊芙nt("click", { "view": window, "bubbles": true, "cancelable": false }卡塔尔(قطر‎; a.dispatchEvent(click伊芙nt卡塔尔(英语:State of Qatar); }卡塔尔; }, function(err卡塔尔国 { console.log(err卡塔尔国; }卡塔尔// 省略了 loadImage 函数 // 代码和率先个例子同样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 此处略去生成 svg url 的过程
var svgUrl = DomURL.createObjectURL(blob);
var svgWidth = document.querySelector('#kity_svg').getAttribute('width');
var svgHeight = document.querySelector('#kity_svg').getAttribute('height');
 
var embededImages = document.querySelectorAll('#kity_svg image');
// 由 nodeList 转为 array
embededImages = Array.prototype.slice.call(embededImages);
// 加载底层的图
loadImage(svgUrl).then(function(img) {
 
var canvas = document.createElement('canvas'),
ctx = canvas.getContext("2d");
 
canvas.width = svgWidth;
canvas.height = svgHeight;
 
ctx.drawImage(img, 0, 0);
    // 遍历 svg 里面所有的 image 元素
    embededImages.reduce(function(sequence, svgImg){
 
        return sequence.then(function() {
            var url = svgImg.getAttribute('xlink:href') 'abc',
                dX = svgImg.getAttribute('x'),
                dY = svgImg.getAttribute('y'),
                dWidth = svgImg.getAttribute('width'),
                dHeight = svgImg.getAttribute('height');
 
            return loadImage(url).then(function( sImg) {
                ctx.drawImage(sImg, 0, 0, sImg.width, sImg.height, dX, dY, dWidth, dHeight);
            }, function(err) {
                console.log(err);
            });
        }, function(err) {
            console.log(err);
        });
    }, Promise.resolve()).then(function() {
        // 准备在前端下载
        var a = document.createElement("a");
        a.download = 'download.png';
        a.href = canvas.toDataURL("image/png");
 
        var clickEvent = new MouseEvent("click", {
            "view": window,
            "bubbles": true,
            "cancelable": false
        });
 
        a.dispatchEvent(clickEvent);
 
        });
 
      }, function(err) {
        console.log(err);
   })
 
   // 省略了 loadImage 函数
   // 代码和第一个例子相同

说明

  1. 事例中 svg 里面包车型地铁图像是根节点上边包车型大巴,由此用于表示地方的 x, y 直接取来就可以使用,在实际中,那些职分恐怕须要跟任何质量做一些运算之后得出。假如是凭借svg 库构建的,那么能够一贯使用Curry面用于固定的函数,比平昔从尾部运算越发有助于和准确。
  2. 大家这边探讨的是本域的图样的导出难题,跨域的图片由于「污染了」画布,在奉行 toDataUrl 函数的时候会报错。

/**

数据编码

谈到底,正是将图像举办导出。

假诺 canvas 能直接导出成 blob,那是最棒的。因为 blob 可经过 AJAX 上传。

canvas.toBlob(function(blob) { // ... }, 'image/png')

1
2
3
canvas.toBlob(function(blob) {
    // ...
}, 'image/png')

而是,大多浏览器都不支持。只可以导出 data uri 格式:

uri = canvas.toDataURL('image/png') // data:image/png;base64,xxxx

1
uri = canvas.toDataURL('image/png')  // data:image/png;base64,xxxx

但 base64 会增添少长度度。所以,还得解回二进制:

base64 = uri.substr(uri.indexOf(',') 1) binary = atob(base64)

1
2
base64 = uri.substr(uri.indexOf(',') 1)
binary = atob(base64)

此刻的 binary,就是最终数额了吗?

若果将 binary 通过 AJAX 提交的话,会开采实际传输字节,比 binary.length 大。

原来 atob 再次回到的数量,仍然是字符串型的。传输时,就事关字集编码了。

为此还需再转移一次,形成真的的二进制数据:

var len = binary.length var buf = new Uint8Array(len) for (var i = 0; i len; i ) { buf[i] = binary.charCodeAt(i) }

1
2
3
4
5
6
var len = binary.length
var buf = new Uint8Array(len)
 
for (var i = 0; i  len; i ) {
    buf[i] = binary.charCodeAt(i)
}

那会儿的 buf,手艺被 AJAX 原封不动的传导。

结语

在那和大家享用了 在前端将 svg 转为 png 的不二秘诀和进度中或许会遇上的七个难点,一个是浏览器对 canvas 的尺寸约束,另三个是导出图片的标题。当然,那七个难点还会有别的的缓和办法,同一时候鉴于文化所限,本文内容难免有漏洞,迎接大家商量指正。最终谢谢@techird 和 @Naxior 关于那八个难题的座谈。

1 赞 2 收藏 评论

澳门新萄京 10

* 获取处理实例

末尾效果

回顾,我们差十分的少演示下:Demo

找一个大块的文件测验。举个例子 qq.com 首页 HTML,有 637,101 字节。

先接受「每像素 1 字节」的编码,种种浏览器生成的 PNG 大小:

Chrome FireFox Safari
体积 289,460 203,276 478,994
比率 45.4% 31.9% 75.2%

内部火狐压缩率最高,收缩了 2/3 的体量。

扭转的 PNG 看起来是那般的:

澳门新萄京 11

唯独可惜的是,全数浏览器生成的图纸,都不是「256 色索引」的。


再测验「每像素 3 字节」,看看会不会有纠正:

Chrome FireFox Safari
体积 297,239 202,785 384,183
比率 46.7% 31.8% 60.3%

Safari 有了重重的演变,不过 Chrome 却更糟了。

FireFox 有微微的进级,压缩率仍为参天的。

澳门新萄京 12

同风华正茂遗憾的是,固然全体图片并不曾利用 Alpha 通道,但调换的 PNG 仍然是 32位的。

再者,也无法设置压缩等第,使得这种压缩形式,效能并不高。

相比较 Flash 压缩,差别就基本上了:

deflate 压缩 lzma 压缩
体积 133,660 108,015
比率 21.0% 17.0%

与此同一时候 Flash 生成的是通用格式,后端解码时,使用标准库就能够。

而 PNG 还得位图解码、像素管理等手续,很辛勤。

为此,现实中仍然优先接受 Flash,本文只是开脑洞而已。

* Get ImageProcessHelper instance by single

事实上用项

但是这种方法,实际依然有效到过。用在叁个超大日志上传之处(並且不能够用 Flash)。

因为后端并不解析,仅仅储存而已。所以,能够将日志对应的 PNG 下回本地,在协会者本身计算机上解析。

解压更便于,就是将像素还原回数据,这里有个简陋的 Demo。

那般,既减少了宽带,也省去存款和储蓄空间。

3 赞 4 收藏 1 评论

澳门新萄京 13

*

    * @return ImageProcessHelper

*/

    public static ImageProcessHelper getInstance() {

return HelperTemp.helper;

}

///////////////////////////////////////////////////////////////////

//////////////////////////////图片地方//////////////////////////////

/**

* 地方 上下左右中 左上角 左下角 右上角 右下角 中间

* */

    public enum Position {

LEFT,

RIGHT,

TOP,

BOTTOM,

CENTRE,

LEFT_UP,

LEFT_DOWN,

RIGHT_UP,

RIGHT_DOWN,

CENTER;

}

/**

* 图片格式

* */

    public enum Format {

JPEG,

PNG,

WEBP;

}

/**

* Bitmap图片转变到圆角

*

    * @param mBitmapSrc 图片源

    * @param roundPx    float

    * @return Bitmap

*/

    public Bitmap convert2RoundedCorner(Bitmap mBitmapSrc,float roundPx) {

Bitmap newBitmap = Bitmap.createBitmap(mBitmapSrc.getWidth(), mBitmapSrc.getHeight(),

Bitmap.Config.ARGB_8888);

澳门新萄京一些常用的图像处理方法,将HTML导出生成word文档。// 得到画布

        Canvas canvas =new Canvas(newBitmap);

final int color =0xff424242;

final Paint paint =new Paint();

final Rect rect =new Rect(0,0, mBitmapSrc.getWidth(), mBitmapSrc.getHeight());

final RectF rectF =new RectF(rect);

paint.setAntiAlias(true);

canvas.drawARGB(0,0,0,0);

paint.setColor(color);

// 第二个和第八个参数同样则画的是正圆的大器晚成角,不然是椭圆的生机勃勃角

        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

canvas.drawBitmap(mBitmapSrc, rect, rect, paint);

return newBitmap;

}

/**

* Bitmap图片灰度化管理

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap bitmap2Gray(Bitmap mBitmapSrc) {

// 获得图片的长和宽

        int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

// 创制指标灰度图像

        Bitmap bmpGray =null;

bmpGray = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

// 创造画布

        Canvas c =new Canvas(bmpGray);

Paint paint =new Paint();

ColorMatrix cm =new ColorMatrix();

cm.setSaturation(0);

ColorMatrixColorFilter f =new ColorMatrixColorFilter(cm);

paint.setColorFilter(f);

c.drawBitmap(mBitmapSrc,0,0, paint);

return bmpGray;

}

//另风流洒脱种灰度

    public Bitmap convertGreyImgByFloyd(Bitmap img) {

int width = img.getWidth(卡塔尔(قطر‎;//获取位图的宽

        int height = img.getHeight(卡塔尔(英语:State of Qatar);//获取位图的高

        int[] pixels =new int[width * height];//通过位图的轻重创立像素点数组

        img.getPixels(pixels,0, width,0,0, width, height);

int[] gray=new int[height*width];

for (int i =0; i < height; i ) {

for (int j =0; j < width; j ) {

int grey = pixels[width * i j];

int red = ((grey  &0x00FF0000 ) >>16);

gray[width*i j]=red;

}

}

int e=0;

for (int i =0; i < height; i ) {

for (int j =0; j < width; j ) {

int g=gray[width*i j];

if (g>=128) {

pixels[width*i j]=0xffffffff;

e=g-255;

}else {

pixels[width*i j]=0xff000000;

e=g-0;

}

if (j

//侧边像素管理

                    gray[width*i j 1] =3*e/8;

//下

                    gray[width*(i 1) j] =3*e/8;

//右下

                    gray[width*(i 1) j 1] =e/4;

}else if (j==width-1&&i

//下方像素管理

                    gray[width*(i 1) j] =3*e/8;

}else if (j

//侧面像素管理

                    gray[width*(i) j 1] =e/4;

}

}

}

Bitmap mBitmap=Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

mBitmap.setPixels(pixels,0, width,0,0, width, height);

return mBitmap;

}

/**

* 图片线性灰度管理

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap bitmap2LineGrey(Bitmap mBitmapSrc) {

// 获得图像的小幅度和尺寸

        int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

// 创设线性拉升灰度图像

        Bitmap bitmap = mBitmapSrc.copy(Bitmap.Config.ARGB_8888,true);

// 依首轮回对图像的像素实行拍卖

        for (int i =0; i < width; i ) {

for (int j =0; j < height; j ) {

// 获得每点的像素值

                int col = mBitmapSrc.getPixel(i, j);

int alpha = col &0xFF000000;

int red = (col &0x00FF0000) >>16;

int green = (col &0x0000FF00) >>8;

int blue = (col &0x000000FF);

// 扩大了图像的亮度

                red = (int) (1.1 * red 30);

green = (int) (1.1 * green 30);

blue = (int) (1.1 * blue 30);

// 对图像像素越界实行拍卖

                if (red >=255) {

red =255;

}

if (green >=255) {

green =255;

}

if (blue >=255) {

blue =255;

}

// 新的ARGB

                int newColor = alpha | (red <<16) | (green <<8) | blue;

// 设置新图像的兰德奥德赛GB值

                bitmap.setPixel(i, j, newColor);

}

}

return bitmap;

}

/**

* 图像二值化管理

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap gray2Binary(Bitmap mBitmapSrc) {

// 得到图片的上涨的幅度和尺寸

        int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

// 创立二值化图像

        Bitmap binarybm =null;

binarybm = mBitmapSrc.copy(Bitmap.Config.ARGB_8888,true);

// 依第1轮回,对图像的像素举办管理

        for (int i =0; i < width; i ) {

for (int j =0; j < height; j ) {

// 获得当前像素的值

                int col = binarybm.getPixel(i, j);

// 得到阿尔法通道的值

                int alpha = col &0xFF000000;

// 获得图像的像素LacrosseGB的值

                int red = (col &0x00FF0000) >>16;

int green = (col &0x0000FF00) >>8;

int blue = (col &0x000000FF);

// 用公式X = 0.3×智跑 0.59×G 0.11×B总括出X代替本来的XC90GB

                int gray = (int) ((float) red *0.3 (float) green *0.59 (float) blue *0.11);

// 对图像举办二值化管理

                if (gray <=95) {

gray =0;

}else {

gray =255;

}

// 新的ARGB

                int newColor = alpha | (gray <<16) | (gray <<8) | gray;

// 设置新图像的当下像素值

                binarybm.setPixel(i, j, newColor);

}

}

return binarybm;

}

/**

* 高斯歪曲

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap convertToBlur(Bitmap mBitmapSrc) {

// 高斯矩阵

        int[] gauss =new int[]澳门新萄京一些常用的图像处理方法,将HTML导出生成word文档。{1,2,1,2,4,2,1,2,1};

int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

Bitmap newBmp = Bitmap.createBitmap(width, height,

Bitmap.Config.RGB_565);

int pixR =0;

int pixG =0;

int pixB =0;

int pixColor =0;

int newR =0;

int newG =0;

int newB =0;

int delta =16;// 值越小图片会越亮,越大则越暗

        int idx =0;

int[] pixels =new int[width * height];

mBitmapSrc.getPixels(pixels,0, width,0,0, width, height);

for (int i =1, length = height -1; i < length; i ) {

for (int k =1, len = width -1; k < len; k ) {

idx =0;

for (int m = -1; m <=1; m ) {

for (int n = -1; n <=1; n ) {

pixColor = pixels[(i m) * width k n];

pixR = Color.red(pixColor);

pixG = Color.green(pixColor);

pixB = Color.blue(pixColor);

newR = newR pixR * gauss[idx];

newG = newG pixG * gauss[idx];

newB = newB pixB * gauss[idx];

idx ;

}

}

newR /= delta;

newG /= delta;

newB /= delta;

newR = Math.min(255, Math.max(0, newR));

newG = Math.min(255, Math.max(0, newG));

newB = Math.min(255, Math.max(0, newB));

pixels[i * width k] = Color.argb(255, newR, newG, newB);

newR =0;

newG =0;

newB =0;

}

}

newBmp.setPixels(pixels,0, width,0,0, width, height);

return newBmp;

}

/**

* 雕塑效果

*

    * @param BitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap convertToSketch(Bitmap BitmapSrc) {

Bitmap mBitmapSrc = BitmapSrc.copy(Bitmap.Config.ARGB_8888,true);

int pos, row, col, clr;

int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

int[] pixSrc =new int[width * height];

int[] pixNvt =new int[width * height];

// 先对图象的像素管理成灰度颜色后再取反

        mBitmapSrc.getPixels(pixSrc,0, width,0,0, width, height);

for (row =0; row < height; row ) {

for (col =0; col < width; col ) {

pos = row * width col;

pixSrc[pos] = (Color.red(pixSrc[pos])

Color.green(pixSrc[pos]) Color.blue(pixSrc[pos])) /3;

pixNvt[pos] =255 - pixSrc[pos];

}

}

// 对取反的像素举行高斯模糊, 强度能够安装,暂定为5.0

        gaussGray(pixNvt,5.0,5.0, width, height);

// 灰度颜色和混淆后像素举办差值运算

        for (row =0; row < height; row ) {

for (col =0; col < width; col ) {

pos = row * width col;

clr = pixSrc[pos] <<8;

clr /=256 - pixNvt[pos];

clr = Math.min(clr,255);

pixSrc[pos] = Color.rgb(clr, clr, clr);

}

}

mBitmapSrc.setPixels(pixSrc,0, width,0,0, width, height);

return mBitmapSrc;

}

private int gaussGray(int[] psrc,double horz,double vert,

int width,int height) {

int[] dst, src;

double[] n_p, n_m, d_p, d_m, bd_p, bd_m;

double[] val_p, val_m;

int i, j, t, k, row, col, terms;

int[] initial_p, initial_m;

double std_dev;

int row_stride = width;

int max_len = Math.max(width, height);

int sp_p_idx, sp_m_idx, vp_idx, vm_idx;

val_p =new double[max_len];

val_m =new double[max_len];

n_p =new double[5];

n_m =new double[5];

d_p =new double[5];

d_m =new double[5];

bd_p =new double[5];

bd_m =new double[5];

src =new int[max_len];

dst =new int[max_len];

initial_p =new int[4];

initial_m =new int[4];

// 垂直方向

        if (vert >0.0) {

vert = Math.abs(vert) 1.0;

std_dev = Math.sqrt(-(vert * vert) / (2 * Math.log(1.0 /255.0)));

// 初试化常量

            findConstants(n_p, n_m, d_p, d_m, bd_p, bd_m, std_dev);

for (col =0; col < width; col ) {

for (k =0; k < max_len; k ) {

val_m[k] = val_p[k] =0;

}

for (t =0; t < height; t ) {

src[t] = psrc[t * row_stride col];

}

sp_p_idx =0;

sp_m_idx = height -1;

vp_idx =0;

vm_idx = height -1;

initial_p[0] = src[0];

initial_m[0] = src[height -1];

for (row =0; row < height; row ) {

terms = (row <4) ? row :4;

for (i =0; i <= terms; i ) {

val_p[vp_idx] = n_p[i] * src[sp_p_idx - i] - d_p[i]

* val_p[vp_idx - i];

val_m[vm_idx] = n_m[i] * src[sp_m_idx i] - d_m[i]

* val_m[vm_idx i];

}

for (j = i; j <=4; j ) {

val_p[vp_idx] = (n_p[j] - bd_p[j]) * initial_p[0];

val_m[vm_idx] = (n_m[j] - bd_m[j]) * initial_m[0];

}

sp_p_idx ;

sp_m_idx--;

vp_idx ;

vm_idx--;

}

int i1, j1, k1, b;

int bend =1 * height;

double sum;

i1 = j1 = k1 =0;

for (b =0; b < bend; b ) {

sum = val_p[i1 ] val_m[j1 ];

if (sum >255)

sum =255;

else if (sum <0)

sum =0;

dst[k1 ] = (int) sum;

}

for (t =0; t < height; t ) {

psrc[t * row_stride col] = dst[t];

}

}

}

// 水平方向

        if (horz >0.0) {

horz = Math.abs(horz) 1.0;

if (horz != vert) {

std_dev = Math.sqrt(-(horz * horz)

/ (2 * Math.log(1.0 /255.0)));

// 初试化常量

                findConstants(n_p, n_m, d_p, d_m, bd_p, bd_m, std_dev);

}

for (row =0; row < height; row ) {

for (k =0; k < max_len; k ) {

val_m[k] = val_p[k] =0;

}

for (t =0; t < width; t ) {

src[t] = psrc[row * row_stride t];

}

sp_p_idx =0;

sp_m_idx = width -1;

vp_idx =0;

vm_idx = width -1;

initial_p[0] = src[0];

initial_m[0] = src[width -1];

for (col =0; col < width; col ) {

terms = (col <4) ? col :4;

for (i =0; i <= terms; i ) {

val_p[vp_idx] = n_p[i] * src[sp_p_idx - i] - d_p[i]

* val_p[vp_idx - i];

val_m[vm_idx] = n_m[i] * src[sp_m_idx i] - d_m[i]

* val_m[vm_idx i];

}

for (j = i; j <=4; j ) {

val_p[vp_idx] = (n_p[j] - bd_p[j]) * initial_p[0];

val_m[vm_idx] = (n_m[j] - bd_m[j]) * initial_m[0];

}

sp_p_idx ;

sp_m_idx--;

vp_idx ;

vm_idx--;

}

int i1, j1, k1, b;

int bend =1 * width;

double sum;

i1 = j1 = k1 =0;

for (b =0; b < bend; b ) {

sum = val_p[i1 ] val_m[j1 ];

if (sum >255)

sum =255;

else if (sum <0)

sum =0;

dst[k1 ] = (int) sum;

}

for (t =0; t < width; t ) {

psrc[row * row_stride t] = dst[t];

}

}

}

return 0;

}

private void findConstants(double[] n_p,double[] n_m,double[] d_p,

double[] d_m,double[] bd_p,double[] bd_m,double std_dev) {

double div = Math.sqrt(2 *3.141593) * std_dev;

double x0 = -1.783 / std_dev;

double x1 = -1.723 / std_dev;

double x2 =0.6318 / std_dev;

double x3 =1.997 / std_dev;

double x4 =1.6803 / div;

double x5 =3.735 / div;

double x6 = -0.6803 / div;

double x7 = -0.2598 / div;

int i;

n_p[0] = x4 x6;

n_p[1] = (Math.exp(x1)

* (x7 * Math.sin(x3) - (x6 2 * x4) * Math.cos(x3)) Math

.exp(x0) * (x5 * Math.sin(x2) - (2 * x6 x4) * Math.cos(x2)));

n_p[2] = (2

                * Math.exp(x0 x1)

* ((x4 x6) * Math.cos(x3) * Math.cos(x2) - x5 * Math.cos(x3)

* Math.sin(x2) - x7 * Math.cos(x2) * Math.sin(x3)) x6

* Math.exp(2 * x0) x4 * Math.exp(2 * x1));

n_p[3] = (Math.exp(x1 2 * x0)

* (x7 * Math.sin(x3) - x6 * Math.cos(x3)) Math.exp(x0 2

                * x1)

* (x5 * Math.sin(x2) - x4 * Math.cos(x2)));

n_p[4] =0.0;

d_p[0] =0.0;

d_p[1] = -2 * Math.exp(x1) * Math.cos(x3) -2 * Math.exp(x0)

* Math.cos(x2);

d_p[2] =4 * Math.cos(x3) * Math.cos(x2) * Math.exp(x0 x1)

Math.exp(2 * x1) Math.exp(2 * x0);

d_p[3] = -2 * Math.cos(x2) * Math.exp(x0 2 * x1) -2 * Math.cos(x3)

* Math.exp(x1 2 * x0);

d_p[4] = Math.exp(2 * x0 2 * x1);

for (i =0; i <=4; i ) {

d_m[i] = d_p[i];

}

n_m[0] =0.0;

for (i =1; i <=4; i ) {

n_m[i] = n_p[i] - d_p[i] * n_p[0];

}

double sum_n_p, sum_n_m, sum_d;

double a, b;

sum_n_p =0.0;

sum_n_m =0.0;

sum_d =0.0;

for (i =0; i <=4; i ) {

sum_n_p = n_p[i];

sum_n_m = n_m[i];

sum_d = d_p[i];

}

a = sum_n_p / (1.0 sum_d);

b = sum_n_m / (1.0 sum_d);

for (i =0; i <=4; i ) {

bd_p[i] = d_p[i] * a;

bd_m[i] = d_m[i] * b;

}

}

/**

* 图片锐化(拉普Russ转换)

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap sharpenImageAmeliorate(Bitmap mBitmapSrc) {

// 拉普Russ矩阵

        int[] laplacian =new int[]{-1, -1, -1, -1,9, -1, -1, -1, -1};

int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

Bitmap bitmap = Bitmap.createBitmap(width, height,

Bitmap.Config.RGB_565);

int pixR =0;

int pixG =0;

int pixB =0;

int pixColor =0;

int newR =0;

int newG =0;

int newB =0;

int idx =0;

float alpha =0.3F;

int[] pixels =new int[width * height];

mBitmapSrc.getPixels(pixels,0, width,0,0, width, height);

for (int i =1, length = height -1; i < length; i ) {

for (int k =1, len = width -1; k < len; k ) {

idx =0;

for (int m = -1; m <=1; m ) {

for (int n = -1; n <=1; n ) {

pixColor = pixels[(i n) * width k m];

pixR = Color.red(pixColor);

pixG = Color.green(pixColor);

pixB = Color.blue(pixColor);

newR = newR (int) (pixR * laplacian[idx] * alpha);

newG = newG (int) (pixG * laplacian[idx] * alpha);

newB = newB (int) (pixB * laplacian[idx] * alpha);

idx ;

}

}

newR = Math.min(255, Math.max(0, newR));

newG = Math.min(255, Math.max(0, newG));

newB = Math.min(255, Math.max(0, newB));

pixels[i * width k] = Color.argb(255, newR, newG, newB);

newR =0;

newG =0;

newB =0;

}

}

bitmap.setPixels(pixels,0, width,0,0, width, height);

return bitmap;

}

/**

* 图片复古

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap oldRemeberImage(Bitmap mBitmapSrc) {

/*

* 怀旧处清理计算法即设置新的PRADOGB

* R=0.393r 0.769g 0.189b

* G=0.349r 0.686g 0.168b

* B=0.272r 0.534g 0.131b

*/

        int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

int pixColor =0;

int pixR =0;

int pixG =0;

int pixB =0;

int newR =0;

int newG =0;

int newB =0;

int[] pixels =new int[width * height];

mBitmapSrc.getPixels(pixels,0, width,0,0, width, height);

for (int i =0; i < height; i ) {

for (int k =0; k < width; k ) {

pixColor = pixels[width * i k];

pixR = Color.red(pixColor);

pixG = Color.green(pixColor);

pixB = Color.blue(pixColor);

newR = (int) (0.393 * pixR 0.769 * pixG 0.189 * pixB);

newG = (int) (0.349 * pixR 0.686 * pixG 0.168 * pixB);

newB = (int) (0.272 * pixR 0.534 * pixG 0.131 * pixB);

int newColor = Color.argb(255, newR >255 ?255 : newR, newG >255 ?255 : newG, newB >255 ?255 : newB);

pixels[width * i k] = newColor;

}

}

bitmap.setPixels(pixels,0, width,0,0, width, height);

return bitmap;

}

/**

* 图片浮雕

* 将日前像素点的TucsonGB值分别与255之差后的值作为当前点的凯雷德GB

* 灰度图像:平常使用的法子是gray=0.3*pixR 0.59*pixG 0.11*pixB

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap reliefImage(Bitmap mBitmapSrc) {

/*

* 算法原理:(前多个像素点牧马人GB-当前像素点中华VGB 127卡塔尔国作为当下像素点SportageGB值

* 在ABC中总计B点浮雕效果(兰德君越GB值在0~255)

* B.r = C.r - B.r 127

* B.g = C.g - B.g 127

* B.b = C.b - B.b 127

*/

        int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

int pixColor =0;

int pixR =0;

int pixG =0;

int pixB =0;

int newR =0;

int newG =0;

int newB =0;

int[] pixels =new int[width * height];

mBitmapSrc.getPixels(pixels,0, width,0,0, width, height);

for (int i =1; i < height -1; i ) {

for (int k =1; k < width -1; k ) {

//获取前贰个像素颜色

                pixColor = pixels[width * i k];

pixR = Color.red(pixColor);

pixG = Color.green(pixColor);

pixB = Color.blue(pixColor);

//获取当前像素

                pixColor = pixels[(width * i k) 1];

newR = Color.red(pixColor) - pixR 127;

newG = Color.green(pixColor) - pixG 127;

newB = Color.blue(pixColor) - pixB 127;

newR = Math.min(255, Math.max(0, newR));

newG = Math.min(255, Math.max(0, newG));

newB = Math.min(255, Math.max(0, newB));

pixels[width * i k] = Color.argb(255, newR, newG, newB);

}

}

bitmap.setPixels(pixels,0, width,0,0, width, height);

return bitmap;

}

/**

* 图片光照效果

*

    * @param mBitmapSrc  图片源

    * @param position 光照地方 私下认可居中

    * @param strength    光照强度 100-150

    * @return Bitmap

*/

    public Bitmap sunshineImage(Bitmap mBitmapSrc, Position position,float strength) {

/*

* 算法原理:(前八个像素点CRUISERGB-当前像素点卡宴GB 127卡塔尔(英语:State of Qatar)作为当前像素点LacrosseGB值

* 在ABC中总计B点浮雕效果(奥德赛GB值在0~255)

* B.r = C.r - B.r 127

* B.g = C.g - B.g 127

* B.b = C.b - B.b 127

* 光照中央取长度宽度十分的小值为半径,也能够自定义从左上角射过来

*/

        int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

int pixColor =0;

int pixR =0;

int pixG =0;

int pixB =0;

int newR =0;

int newG =0;

int newB =0;

//光照

        int centerX;

int centerY;

if (position == Position.LEFT_DOWN){centerX = width * (1/4); centerY = height * (3/4);}

else if (position == Position.LEFT_UP){centerX = width * (1/4); centerY = height * (1/4);}

else if (position == Position.RIGHT_DOWN){centerX = width * (3/4); centerY = height * (3/4);}

else if (position == Position.RIGHT_UP){centerX = width * (3/4); centerY = height * (1/4);}

else {centerX = width /2; centerY = height /2;}//暗中同意居中

        int radius = Math.min(centerX, centerY);

int[] pixels =new int[width * height];

mBitmapSrc.getPixels(pixels,0, width,0,0, width, height);

for (int i =1; i < height -1; i ) {

for (int k =1; k < width -1; k ) {

//获取前一个像素颜色

                pixColor = pixels[width * i k];

pixR = Color.red(pixColor);

pixG = Color.green(pixColor);

pixB = Color.blue(pixColor);

newR = pixR;

newG = pixG;

newB = pixB;

//计算当前点到光照中央的间距,平面坐标系中两点期间的离开

                int distance = (int) (Math.pow((centerY - i),2) Math.pow((centerX - k),2));

if (distance < radius * radius) {

//遵照间隔大小计算增加的光照值

                    int result = (int) (strength * (1.0 - Math.sqrt(distance) / radius));

newR = pixR result;

newG = newG result;

newB = pixB result;

}

newR = Math.min(255, Math.max(0, newR));

newG = Math.min(255, Math.max(0, newG));

newB = Math.min(255, Math.max(0, newB));

pixels[width * i k] = Color.argb(255, newR, newG, newB);

}

}

bitmap.setPixels(pixels,0, width,0,0, width, height);

return bitmap;

}

/**

* 图片冰冻效果

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap iceImage(Bitmap mBitmapSrc) {

int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

int pixColor =0;

int pixR =0;

int pixG =0;

int pixB =0;

int newColor =0;

int newR =0;

int newG =0;

int newB =0;

int[] pixels =new int[width * height];

mBitmapSrc.getPixels(pixels,0, width,0,0, width, height);

for (int i =0; i < height; i ) {

for (int k =0; k < width; k ) {

//获取前三个像素颜色

                pixColor = pixels[width * i k];

pixR = Color.red(pixColor);

pixG = Color.green(pixColor);

pixB = Color.blue(pixColor);

//红色

                newColor = pixR - pixG - pixB;

newColor = newColor *3 /2;

if (newColor <0) {

newColor = -newColor;

}

if (newColor >255) {

newColor =255;

}

newR = newColor;

//绿色

                newColor = pixG - pixB - pixR;

newColor = newColor *3 /2;

if (newColor <0) {

newColor = -newColor;

}

if (newColor >255) {

newColor =255;

}

newG = newColor;

//蓝色

                newColor = pixB - pixG - pixR;

newColor = newColor *3 /2;

if (newColor <0) {

newColor = -newColor;

}

if (newColor >255) {

newColor =255;

}

newB = newColor;

pixels[width * i k] = Color.argb(255, newR, newG, newB);

}

}

bitmap.setPixels(pixels,0, width,0,0, width, height);

return bitmap;

}

/**

* 放大减少图片

*

    * @param mBitmapSrc 图片源

    * @param w          压缩后的增加率 负数时为反向

    * @param h          压缩后的莫斯中国科学技术大学学 负数为反向

    * @return Bitmap

*/

    public Bitmap zoomBitmap(Bitmap mBitmapSrc,int w,int h) {

int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

Matrix matrix =new Matrix();

float scaleWidth = ((float) w / width);

float scaleHeight = ((float) h / height);

matrix.postScale(scaleWidth, scaleHeight);

return Bitmap.createBitmap(mBitmapSrc,0,0, width, height, matrix,true);

}

/**

* 按百分比放大收缩图片

*

    * @param mBitmapSrc  图片源

    * @param widthScale  宽缩放比

    * @param heightScale 高缩放比

    * @return Bitmap

*/

    public Bitmap zoomBitmap(Bitmap mBitmapSrc,float widthScale,float heightScale) {

Matrix matrix =new Matrix();

matrix.postScale(widthScale, heightScale);

return Bitmap.createBitmap(mBitmapSrc,0,0, mBitmapSrc.getWidth(), mBitmapSrc.getHeight(), matrix,true);

}

/**

* 将Drawable转化为Bitmap

*

    * @param mDrawableSrc 要中间转播的源drawable

    * @return Bitmap

*/

    public Bitmap drawableToBitmap(Drawable mDrawableSrc) {

int width = mDrawableSrc.getIntrinsicWidth();

int height = mDrawableSrc.getIntrinsicHeight();

Bitmap bitmap = Bitmap.createBitmap(width, height,

mDrawableSrc.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

                        : Bitmap.Config.RGB_565);

Canvas canvas =new Canvas(bitmap);

mDrawableSrc.setBounds(0,0, width, height);

mDrawableSrc.draw(canvas);

return bitmap;

}

/**

* 倒影图片

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap toReflectedImage(Bitmap mBitmapSrc) {

final int reflectionGap =4;

int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

Matrix matrix =new Matrix();

matrix.preScale(1, -1);

Bitmap reflectionImage = Bitmap.createBitmap(mBitmapSrc,0,

height /2, width, height /2, matrix,false);

Bitmap bitmap = Bitmap.createBitmap(width,

(height height /2), Bitmap.Config.ARGB_8888);

Canvas canvas =new Canvas(bitmap);

canvas.drawBitmap(mBitmapSrc,0,0,null);

Paint defaultPaint =new Paint();

canvas.drawRect(0, height, width, height reflectionGap, defaultPaint);

canvas.drawBitmap(reflectionImage,0, height reflectionGap,null);

Paint paint =new Paint();

LinearGradient shader =new LinearGradient(0,

mBitmapSrc.getHeight(),0, bitmap.getHeight()

reflectionGap,0x70FFFFFF,0x00FFFFFF,

Shader.TileMode.MIRROR);

paint.setShader(shader);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

canvas.drawRect(0, height, width, bitmap.getHeight()

reflectionGap, paint);

return bitmap;

}

/**

* 水印特效

*

    * @param mBitmapSrc  图片源

    * @param waterMarkSrc Bitmap

    * @param position position

    * @return Bitmap

*/

    public Bitmap createBitmapWithWatermark(Bitmap mBitmapSrc, Bitmap waterMarkSrc, Position position) {

if (mBitmapSrc ==null) {

return null;

}

int w = mBitmapSrc.getWidth();

int h = mBitmapSrc.getHeight();

int ww = waterMarkSrc.getWidth();

int wh = waterMarkSrc.getHeight();

Bitmap newBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888卡塔尔;// 创设一个新的和SRC长宽同样的位图

        Canvas cv =new Canvas(newBitmap);

cv.drawBitmap(mBitmapSrc,0,0,null卡塔尔;// 在 0,0坐标开端画入src

        if (position == Position.RIGHT_DOWN)

cv.drawBitmap(waterMarkSrc, w - ww 5, h - wh 5,null卡塔尔(قطر‎;// 在src的右下角画入水印

        else if (position == Position.RIGHT_UP)

cv.drawBitmap(waterMarkSrc, w - ww 5,5,null卡塔尔(英语:State of Qatar);// 在src的右上角画入水印

        else if (position == Position.LEFT_DOWN)

cv.drawBitmap(waterMarkSrc,5, h - wh 5,null卡塔尔;// 在src的左下角画入水印

        else if (position == Position.LEFT_UP)

cv.drawBitmap(waterMarkSrc,5,5,null卡塔尔国;// 在src的左上角画入水印

        else

            cv.drawBitmap(waterMarkSrc, w/2 - ww/2, h/2 - wh,null卡塔尔(英语:State of Qatar);// 在src的中级画入水印

        cv.save(Canvas.ALL_SAVE_FLAG);// 保存

        cv.restore();// 存储

        return newBitmap;

}

/**

* 获取缩略图

* 私下认可获取的宽高为 100

*

    * @param mBitmapSrc 图片源

    * @param width      int

    * @param height    int

    * @return Bitmap

*/

    public Bitmap getThumbBitmap(Bitmap mBitmapSrc,int width,int height) {

if (width ==0) width =100;

if (height ==0) height =100;

Bitmap thumbBitmap;

thumbBitmap = ThumbnailUtils.extractThumbnail(mBitmapSrc, width, height);

return thumbBitmap;

}

/**

* 黑白照片

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

public Bitmap toBlackAndWhite(Bitmap mBitmapSrc) {

int mBitmapWidth;

int mBitmapHeight;

mBitmapWidth = mBitmapSrc.getWidth();

mBitmapHeight = mBitmapSrc.getHeight();

Bitmap bitmap = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight,

Bitmap.Config.ARGB_8888);

int iPixel;

for (int i =0; i < mBitmapWidth; i ) {

for (int j =0; j < mBitmapHeight; j ) {

int curr_color = mBitmapSrc.getPixel(i, j);

int avg = (Color.red(curr_color) Color.green(curr_color) Color

.blue(curr_color)) /3;

if (avg >=100) {

iPixel =255;

}else {

iPixel =0;

}

int modify_color = Color.argb(255, iPixel, iPixel, iPixel);

bitmap.setPixel(i, j, modify_color);

}

}

return bitmap;

}

//二值

    public Bitmap convertBlackWhite(Bitmap bmp) {

int width = bmp.getWidth();

int height = bmp.getHeight();

int[] pixels =new int[width * height];

bmp.getPixels(pixels,0, width,0,0, width, height);

int alpha =0xFF <<24;

for (int i =0; i < height; i ) {

for (int j =0; j < width; j ) {

int grey = pixels[width * i j];

// 抽离三原色

                int red = ((grey &0x00FF0000) >>16);

int green = ((grey &0x0000FF00) >>8);

int blue = (grey &0x000000FF);

//                // 转形成灰度像素

//                grey = (int) (red * 0.3 green * 0.59 blue * 0.11);

//先求最大值

                int max = Math.max(Math.max(red, green), blue);

//                //有个别颜色值作为分界

                if (red == green && red == blue) {

grey = red;

}else if(max >200){

grey =255;

}else {

grey =0;

}

grey = alpha | (grey <<16) | (grey <<8) | grey;

pixels[width * i j] = grey;

}

}

// 新建图片

        Bitmap newbmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

newbmp.setPixels(pixels,0, width,0,0, width, height);

saveBitmap2File(newbmp,"bit", Environment.getExternalStorageDirectory().getPath() "/data", Format.PNG);

return ThumbnailUtils.extractThumbnail(newbmp, width, height);

}

/**

* 底片效果

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap negativeFilm(Bitmap mBitmapSrc) {

// GL450GBA的最大值

        final int MAX_VALUE =255;

int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

int pixR;

int pixG;

int pixB;

int pixColor;

int newR;

int newG;

int newB;

int[] pixels =new int[width * height];

mBitmapSrc.getPixels(pixels,0, width,0,0, width, height);

int pos =0;

for (int i =1, length = height -1; i < length; i ) {

for (int k =1, len = width -1; k < len; k ) {

pos = i * width k;

pixColor = pixels[pos];

pixR = Color.red(pixColor);

pixG = Color.green(pixColor);

pixB = Color.blue(pixColor);

newR = MAX_VALUE - pixR;

newG = MAX_VALUE - pixG;

newB = MAX_VALUE - pixB;

newR = Math.min(MAX_VALUE, Math.max(0, newR));

newG = Math.min(MAX_VALUE, Math.max(0, newG));

newB = Math.min(MAX_VALUE, Math.max(0, newB));

pixels[pos] = Color.argb(MAX_VALUE, newR, newG, newB);

}

}

bitmap.setPixels(pixels,0, width,0,0, width, height);

return bitmap;

}

/**

* 水墨画效果

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap oilPainting(Bitmap mBitmapSrc) {

Bitmap bmpReturn = Bitmap.createBitmap(mBitmapSrc.getWidth(),

mBitmapSrc.getHeight(), Bitmap.Config.RGB_565);

int color =0;

int Radio =0;

int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

Random rnd =new Random();

int iModel =10;

int i = width - iModel;

while (i >1) {

int j = height - iModel;

while (j >1) {

int iPos = rnd.nextInt(100000) % iModel;

color = mBitmapSrc.getPixel(i iPos, j iPos);

bmpReturn.setPixel(i, j, color);

j = j -1;

}

i = i -1;

}

return bmpReturn;

}

/**

* 图片合成

*

    * @param position  组合地方: -1 :左  1 :右  2 :上  -2 :下

    * @param mBitmapSrcs 图片源

    * @return Bitmap

*/

    public Bitmap photoMix(Position position, Bitmap... mBitmapSrcs) {

if (mBitmapSrcs.length <=0) {

return null;

}

if (mBitmapSrcs.length ==1) {

return mBitmapSrcs[0];

}

Bitmap newBitmap = mBitmapSrcs[0];

for (int i =1; i < mBitmapSrcs.length; i ) {

newBitmap = createBitmapForPhotoMix(newBitmap, mBitmapSrcs[i], position);

}

return newBitmap;

}

private Bitmap createBitmapForPhotoMix(Bitmap first, Bitmap second, Position position) {

if (first ==null) {

return null;

}

if (second ==null) {

return first;

}

int fw = first.getWidth();

int fh = first.getHeight();

int sw = second.getWidth();

int sh = second.getHeight();

Bitmap newBitmap =null;

if (position == Position.LEFT) {

newBitmap = Bitmap.createBitmap(fw sw, fh > sh ? fh : sh, Bitmap.Config.ARGB_8888);

Canvas canvas =new Canvas(newBitmap);

canvas.drawBitmap(first, sw,0,null);

canvas.drawBitmap(second,0,0,null);

}else if (position == Position.RIGHT) {

newBitmap = Bitmap.createBitmap(fw sw, fh > sh ? fh : sh, Bitmap.Config.ARGB_8888);

Canvas canvas =new Canvas(newBitmap);

canvas.drawBitmap(first,0,0,null);

canvas.drawBitmap(second, fw,0,null);

}else if (position == Position.TOP) {

newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh sh, Bitmap.Config.ARGB_8888);

Canvas canvas =new Canvas(newBitmap);

canvas.drawBitmap(first,0, sh,null);

canvas.drawBitmap(second,0,0,null);

}else if (position ==  Position.BOTTOM) {

newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh sh, Bitmap.Config.ARGB_8888);

Canvas canvas =new Canvas(newBitmap);

canvas.drawBitmap(first,0,0,null);

canvas.drawBitmap(second,0, fh,null);

}else if (position ==  Position.CENTRE) {

newBitmap = Bitmap.createBitmap(Math.max(fw, sw), Math.max(fw, sw), Bitmap.Config.ARGB_8888);

Canvas canvas =new Canvas(newBitmap);

canvas.drawBitmap(first,0,0,null);

canvas.drawBitmap(second, fw /2, fh /2,null);

}

return newBitmap;

}

/**

* bitmap 位图保存成文件

*

    * @param mBitmapSrc 图片源

    * @param fileName  文件名

    * @param filePath  保存的公文路线(默认为空时在内部存储器根目录)

    * @param format    保存的图片格式(私下认可 JPEG卡塔尔(قطر‎

*/

    public void saveBitmap2File(Bitmap mBitmapSrc, String fileName, String filePath, Format format) {

String suffix ="jpg";

if (TextUtils.isEmpty(filePath))

filePath = Environment.getExternalStorageDirectory().getAbsolutePath().toString();

Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;

if (format == Format.JPEG){

compressFormat = Bitmap.CompressFormat.JPEG;

suffix =".jpeg";

}

else if (format == Format.PNG){

compressFormat = Bitmap.CompressFormat.PNG;

suffix =".png";

}

else if (format == Format.WEBP){

compressFormat = Bitmap.CompressFormat.WEBP;

suffix =".webp";

}

File file =new File(filePath File.separator, fileName suffix);

try {

file.createNewFile();

OutputStream os =new FileOutputStream(file);

mBitmapSrc.compress(compressFormat,100, os);

os.flush();

}catch (IOException e) {

e.printStackTrace();

}

}

/**

* 图片平滑管理

* 3*3掩模管理(平均管理),裁减噪声

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap smoothImage(Bitmap mBitmapSrc) {

int w = mBitmapSrc.getWidth();

int h = mBitmapSrc.getHeight();

int[] data =new int[w * h];

mBitmapSrc.getPixels(data,0, w,0,0, w, h);

int[] resultData =new int[w * h];

try {

resultData = filter(data, w, h);

}catch (Exception e) {

e.printStackTrace();

}

Bitmap newBitmap = Bitmap.createBitmap(resultData, w, h, Bitmap.Config.ARGB_8888);

return newBitmap;

}

private int[] filter(int[] data,int width,int height)throws Exception {

int filterData[] =new int[data.length];

int min =10000;

int max = -10000;

if (data.length != width * height)return filterData;

try {

for (int i =0; i < height; i ) {

for (int j =0; j < width; j ) {

if (i ==0 || i ==1 || i == height -1 || i == height -2 || j ==0 || j ==1 || j == width -1 || j == width -2) {

filterData[i * width j] = data[i * width j];

}else {

double average;//核心的多少个像素点

                        average = (data[i * width j] data[i * width j -1] data[i * width j 1]

data[(i -1) * width j] data[(i -1) * width j -1] data[(i -1) * width j 1]

data[(i 1) * width j] data[(i 1) * width j -1] data[(i 1) * width j 1]) /9;

filterData[i * width j] = (int) (average);

}

if (filterData[i * width j] < min)

min = filterData[i * width j];

if (filterData[i * width j] > max)

max = filterData[i * width j];

}

}

for (int i =0; i < width * height; i ) {

filterData[i] = (filterData[i] - min) *255 / (max - min);

}

}catch (Exception e) {

e.printStackTrace();

throw new Exception(e);

}

return filterData;

}

/**

* 图片增亮

*

    * @param mBitmapSrc    图片源

    * @param brightenOffset 扩充的亮度值

    * @return Bitmap

*/

    public Bitmap brightenBitmap(Bitmap mBitmapSrc,int brightenOffset) {

int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

int[] pix =new int[width * height];

mBitmapSrc.getPixels(pix,0, width,0,0, width, height);

// Apply pixel-by-pixel change

        int index =0;

for (int y =0; y < height; y ) {

for (int x =0; x < width; x ) {

int r = (pix[index] >>16) &0xff;

int g = (pix[index] >>8) &0xff;

int b = pix[index] &0xff;

r = Math.max(0, Math.min(255, r brightenOffset));

g = Math.max(0, Math.min(255, g brightenOffset));

b = Math.max(0, Math.min(255, b brightenOffset));

pix[index] =0xff000000 | (r <<16) | (g <<8) | b;

index ;

}// x

        }// y

// Change bitmap to use new array

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

bitmap.setPixels(pix,0, width,0,0, width, height);

mBitmapSrc =null;

pix =null;

return bitmap;

}

/**

* 均值滤波

*

    * @param mBitmapSrc  图片源

    * @param filterWidth  滤波宽度值

    * @param filterHeight 滤波中度值

*/

    public Bitmap averageFilter(Bitmap mBitmapSrc,int filterWidth,int filterHeight) {

int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

int[] pixNew =new int[width * height];

int[] pixOld =new int[width * height];

mBitmapSrc.getPixels(pixNew,0, width,0,0, width, height);

mBitmapSrc.getPixels(pixOld,0, width,0,0, width, height);

// Apply pixel-by-pixel change

        int filterHalfWidth = filterWidth /2;

int filterHalfHeight = filterHeight /2;

int filterArea = filterWidth * filterHeight;

for (int y = filterHalfHeight; y < height - filterHalfHeight; y ) {

for (int x = filterHalfWidth; x < width - filterHalfWidth; x ) {

// Accumulate values in neighborhood

                int accumR =0, accumG =0, accumB =0;

for (int dy = -filterHalfHeight; dy <= filterHalfHeight; dy ) {

for (int dx = -filterHalfWidth; dx <= filterHalfWidth; dx ) {

int index = (y dy) * width (x dx);

accumR = (pixOld[index] >>16) &0xff;

accumG = (pixOld[index] >>8) &0xff;

accumB = pixOld[index] &0xff;

}// dx

                }// dy

// Normalize

                accumR /= filterArea;

accumG /= filterArea;

accumB /= filterArea;

int index = y * width x;

pixNew[index] =0xff000000 | (accumR <<16) | (accumG <<8) | accumB;

}// x

        }// y

// Change bitmap to use new array

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

bitmap.setPixels(pixNew,0, width,0,0, width, height);

mBitmapSrc =null;

pixOld =null;

pixNew =null;

return bitmap;

}

/**

* 中值滤波

*

    * @param mBitmapSrc  图片源

    * @param filterWidth  滤波宽度值

    * @param filterHeight 滤波中度值

*/

    public Bitmap medianFilter(Bitmap mBitmapSrc,int filterWidth,int filterHeight) {

int width = mBitmapSrc.getWidth();

int height = mBitmapSrc.getHeight();

int[] pixNew =new int[width * height];

int[] pixOld =new int[width * height];

mBitmapSrc.getPixels(pixNew,0, width,0,0, width, height);

mBitmapSrc.getPixels(pixOld,0, width,0,0, width, height);

// Apply pixel-by-pixel change

        int filterHalfWidth = filterWidth /2;

int filterHalfHeight = filterHeight /2;

int filterArea = filterWidth * filterHeight;

for (int y = filterHalfHeight; y < height - filterHalfHeight; y ) {

for (int x = filterHalfWidth; x < width - filterHalfWidth; x ) {

// Accumulate values in neighborhood

                int accumR =0, accumG =0, accumB =0;

for (int dy = -filterHalfHeight; dy <= filterHalfHeight; dy ) {

for (int dx = -filterHalfWidth; dx <= filterHalfWidth; dx ) {

int index = (y dy) * width (x dx);

accumR = (pixOld[index] >>16) &0xff;

accumG = (pixOld[index] >>8) &0xff;

accumB = pixOld[index] &0xff;

}// dx

                }// dy

// Normalize

                accumR /= filterArea;

accumG /= filterArea;

accumB /= filterArea;

int index = y * width x;

pixNew[index] =0xff000000 | (accumR <<16) | (accumG <<8) | accumB;

}// x

        }// y

// Change bitmap to use new array

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

bitmap.setPixels(pixNew,0, width,0,0, width, height);

mBitmapSrc =null;

pixOld =null;

pixNew =null;

return bitmap;

}

}

本文由澳门新萄京发布于澳门新萄京最大平台,转载请注明出处:澳门新萄京一些常用的图像处理方法,将HTML导出

上一篇:澳门新萄京:H5直播起航 下一篇:没有了
猜你喜欢
热门排行
精彩图文