澳门新萄京官方网站-www.8455.com-澳门新萄京赌场网址

一些常用的图像处理方法,前端实现

2019-07-13 作者:澳门新萄京赌场网址   |   浏览(153)

运用 canvas 完成数据压缩

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

初稿出处: EtherDream   

后边三个达成 SVG 转 PNG

2015/11/16 · JavaScript · PNG, SVG

原版的书文出处: 百度FEX - zhangbobell   

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

 

public class ImageProcessHelper {

IE这种上古神器居然还会有人在用??先天来享受二个很狂拽光彩夺目吊炸天的特效,其吹牛效果不亚于地点那张侵犯五角大楼导弹制导系统的概念图(手动滑稽),达成起来比较粗略,跟着入手一同来呢

前言

HTTP 援助 GZip 压缩,可节省无尽传输能源。但可惜的是,独有下载才有,上传并不协助。

假定上传也能压缩,那就宏观了。非常符合大量文本提交的地方,譬近日日头条,正是很好的事例。

固然正规不扶助「上传压缩」,但仍能友善来得以实现。

前言

svg 是一种矢量图形,在 web 上运用很常见,但是十分多时候由于使用的光景,平日需求将 svg 转为 png 格式,下载到当地等。随着浏览器对 HTML 5 的协助度越来越高,大家得以把 svg 转为 png 的干活交给浏览器来完结。

前言:

项目支付中遇见了亟待将HTML页面包车型地铁原委导出为二个word文书档案,所以有了此处小说。

自然,项目支出又时间有个别急迫,第有的时候间想到的是用插件,所以百度了下。下面就介绍五个导出word文档的章程。

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

正如标题所涉及的,大家运用到了canvas成分,你能够掌握为是一张画布,有了画布之后,我们将在在画布上海展览中心开绘图,而canvas成分自身是不富有绘图手艺的,所以大家要依赖JavaScript 来形成绘制工作

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()

来落到实处文件的导出。

插件源码:

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.Mso诺玛l, 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:"提姆es 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.cruity.com/uploads/allimg/190713/1P12I914-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

    private ImageProcessHelper() {

HTML 的结构大家只供给二个标签就够了,假设在低版本的浏览器中,大家照旧要升迁一下浏览器供给进步了

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 等工具,相对来讲相比较简单,这里我们任重(Ren Zhong)而道远查究第二种减轻方法。

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

注重是透过js模板设置相应的竹签,然后XDoc.to(baidu.template())导出word,而由此百度js模板引擎的好处是也能够导出PDF文件。

完整demo:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="UTF-8">
 5     <script type="text/javascript" src="http://www.cruity.com/uploads/allimg/190713/1P12HV5-6.jpg"></script>
 6     <script type="text/javascript" src="http://www.cruity.com/uploads/allimg/190713/1P12HX2-7.jpg"></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.cruity.com/uploads/allimg/190713/1P12I914-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文档,所以有了那边小说。 当然,项目开拓又时间有...

}

<canvas id="canvas"> 

IE这种上古神器居然还会有人在用?

</canvas>

其他

能或不能不要 JS,而是使用一些接口,间接达成缩小?

其实,在 HTML5 刚面世时,就专注到了二个职能:canvas 导出图片。能够扭转 jpg、png 等格式。

若是在探讨的话,相信您也想开了。没有错,即是 png —— 它是无损压缩的。

我们把一般数据当成像素点,画到 canvas 上,然后导出成 png,正是三个非常的缩减包了~


上边伊始探求。。。

svg 切分成多少个图片导出

思路:浏览器固然对 canvas 有尺寸和面积的限定,可是对于 image 成分并从未刚毅的范围,也正是首先步生成的 image 其实显示是例行的,我们要做的只是在其次步 dragImage 的时候分数14遍将 image 成分切分并贴到 canvas 上然后下载下来。 同偶尔间,应注意到 image 的载入是三个异步的进度。

首要代码

JavaScript

// 构造 svg Url,此处省略将 svg 经字符过滤后转为 url 的经过。 var svgUrl = DomUENVISIONL.createObjectURAV4L(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.toDataU瑞虎L('image/png'); var clickEvent = new Mouse伊夫nt('click', { 'view': window, 'bubbles': true, 'cancelable': false }); 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) { reject(err); }; }); }

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 {

安装全局 CSS 样式,很轻巧,代码如下:

数码转变

数据转像素,并不麻烦。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();

*{margin:0px;padding:0px;}

body{overflow:hidden;}

尺寸设定

最简便易行的,正是用 1px 的可观。举例有 一千 个像素,则填在 一千 x 1 的图形里。

但假诺有 一千0 像素,就不可行了。因为 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)); 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; } }

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 地址的人出示分歧的头像;
  • 能够每三二十二日调换头像的外观(而不用经过论坛管理员的稽核)。

总的来看此间,大致就知晓了百分之百难点的首尾了,当然还也可能有有些缘由可能是幸免图像递归。

}

赢得浏览器显示器并设置其宽高,设置二个暗含 256个空成分的数组,.join("1")用 1 来把数组里的因素拼接为字符串,.split("")过滤掉数组里的空元素

渲染难点

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

但是现实中,总某个意外的坑。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); 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>

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 = DomUENCOREL.createObjectU福睿斯L(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.toDataUHighlanderL("image/png"); var click伊芙nt = new MouseEvent("click", { "view": window, "bubbles": true, "cancelable": false }); a.dispatch伊芙nt(clickEvent); }); }, 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 函数的时候会报错。

/**

const canvas = document.getElementById("canvas"),

ctx    = canvas.getContext("2d"),

s      = window.screen,

w      = canvas.width = s.width,

h      = canvas.height = s.height;

let  words  = Array(256).join("1").split("");

数据编码

末尾,便是将图像实行导出。

如果 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 仍是 叁十三个人的。

还要,也力不能支设置压缩等第,使得这种压缩格局,效用并不高。

相对来说 Flash 压缩,差别就大约了:

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

再正是 Flash 生成的是通用格式,后端解码时,使用规范库就可以。

而 PNG 还得位图解码、像素处理等手续,很费劲。

之所以,现实中照旧优先选取 Flash,本文只是开脑洞而已。

* Get ImageProcessHelper instance by single

setInterval( () => {

ctx.fillStyle = "rgba(0, 0, 0, 0.05)";

ctx.fillRect(0, 0, w, h);

ctx.fillStyle = "#20af0e";

//数组元素的酷炫

words.map( (y,n) => {

//生成A-Z a-z之间的值

text = String.fromCharCode(Math.ceil(65 Math.random() * 57))

x = n * 10;

ctx.fillText(text, x, y);

words[n] = (y > 758 Math.random() * 484 )? 0 : y 10;

});

},50);

实在用途

只是这种艺术,实际照旧有效到过。用在三个一点都不小日志上传的场子(而且无法用 Flash)。

因为后端并不深入分析,仅仅累积而已。所以,可以将日志对应的 PNG 下回地面,在总指挥本人计算机上分析。

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

这么,既裁减了宽带,也节约存储空间。

3 赞 4 收藏 1 评论

澳门新萄京官方网站 13

*

澳门新萄京官方网站 14

    * @return ImageProcessHelper

运营结果

*/

运作结果

    public static ImageProcessHelper getInstance() {

这里我们还足以将填充绘图的颜色修改成自由颜色,而颜色值是十六进制数,其范围是 000000 - FFFFFF,调换为十进制是 0 - 16777215,所以我们透过任性数生成在那么些范围内的色值,当然最后照旧要转成十六进制,不要忘记在色值前边加#号 ,一共有三种方法,代码如下所示:

return HelperTemp.helper;

// 方法一

function color1(){

let color = "";

const colors = [0,1,2,3,4,5,6,7,8,9,"a","b","c","d","e","f"];

for(let i = 0; i < 6; i ){

const n = Math.ceil(Math.random() * 15);

color = "" colors[n];

if(i == 5){

return "#" color;

}

}

}

// 方法二

function color2(){

let color = Math.ceil(Math.random() * 16777215).toString(16);

while(color.length < 6) {

color = "0" color;

}

return "#" color;

}

}

// 方法三

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

function color3(){

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

return "#" ( color => {

/**

return new Array(7 - color.length).join("0") color;

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

})((Math.random() * 0x1000000 << 0).toString(16))

* */

}

    public enum Position {

澳门新萄京官方网站 15

LEFT,

运转结果

RIGHT,

本篇的内容到那边就总体得了了,源码小编一度发到了 GitHubHacker meteor shower上了,有亟待的同桌可机关下载

TOP,

End of File

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);

// 得到画布

        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();//获取位图的高

        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);

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

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

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

// 获得当前像素的值

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

// 获得阿尔法通道的值

                int alpha = col &0xFF000000;

// 获得图像的像素奇骏GB的值

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

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

int blue = (col &0x000000FF);

// 用公式X = 0.3×奥迪Q3 0.59×G 0.11×B总计出X代替本来的QashqaiGB

                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[]{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) {

/*

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

* 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;

}

/**

* 图片浮雕

* 将眼下像素点的奥迪Q3GB值分别与255之差后的值作为当前点的CR-VGB

* 灰度图像:日常使用的艺术是gray=0.3*pixR 0.59*pixG 0.11*pixB

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap reliefImage(Bitmap mBitmapSrc) {

/*

* 算法原理:(前叁个像素点本田CR-VGB-当前像素点CRUISERGB 127)作为当前像素点智跑GB值

* 在ABC中总结B点浮雕效果(奥迪Q3GB值在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) {

/*

* 算法原理:(前八个像素点大切诺基GB-当前像素点昂科威GB 127)作为当下像素点奥迪Q5GB值

* 在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(water马克Src, w - ww 5, h - wh 5,null);// 在src的右下角画入水印

        else if (position == Position.RIGHT_UP)

cv.drawBitmap(water马克Src, w - ww 5,5,null);// 在src的右上角画入水印

        else if (position == Position.LEFT_DOWN)

cv.drawBitmap(water马克Src,5, h - wh 5,null);// 在src的左下角画入水印

        else if (position == Position.LEFT_UP)

cv.drawBitmap(water马克Src,5,5,null);// 在src的左上角画入水印

        else

            cv.drawBitmap(water马克Src, w/2 - ww/2, h/2 - wh,null);// 在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) {

// RAV4GBA的最大值

        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;

}

}

本文由澳门新萄京官方网站发布于澳门新萄京赌场网址,转载请注明出处:一些常用的图像处理方法,前端实现

关键词: