将HTML导出生成word文档,一些常用的图像处理情势

应用 canvas 完毕数据压缩

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

原文出处:
EtherDream   

前端已毕 SVG 转 PNG

2015/11/16 · JavaScript
· PNG,
SVG

初稿出处: 百度FEX –
zhangbobell   

将HTML导出生成word文档,

 

public class ImageProcessHelper {

前言

HTTP 协理 GZip
压缩,可节省举不胜举传输资源。但遗憾的是,唯有下载才有,上传并不帮忙。

万一上传也能减少,这就完善了。尤其符合大批量文本提交的场面,比如和讯,就是很好的事例。

尽管正规不辅助「上传压缩」,但仍是可以够友善来落到实处。

前言

svg 是一种矢量图形,在 web
上应用很普遍,不过众多时候由于使用的情景,平常须要将 svg 转为 png
格式,下载到本地等。随着浏览器对 HTML 5 的支撑度越来越高,大家得以把 svg
转为 png 的行事交给浏览器来形成。

前言:

系列支付中碰着了须求将HTML页面的情节导出为一个word文档,所以有了此处小说。

自然,项目支出又时间有些热切,第一时间想到的是用插件,所以百度了下。上边就介绍七个导出word文档的点子。

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

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*;.*charset\s*=\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.0\nContent-Base: ” +
location.href + “\nContent-Type: Multipart/related;
boundary=\”NEXT.ITEM-BOUNDARY\”;type=\”text/html\”\n\n–NEXT.ITEM-BOUNDARY\nContent-Type:
text/html; charset=\”utf-8\”\nContent-Location: ” + location.href +
“\n\n<!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-BOUNDARY\n”; 57 mhtmlBottom +=
“Content-Location: ” + images[i].location + “\n”; 58 mhtmlBottom +=
“Content-Type: ” + images[i].type + “\n”; 59 mhtmlBottom +=
“Content-Transfer-Encoding: ” + images[i].encoding + “\n\n”; 60
mhtmlBottom += images[i].data + “\n\n”; 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.Mso诺玛l,
li.Mso诺玛l,
div.Mso诺玛l{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 Roman”;mso-hansi-font-family:”提姆es New
Roman”;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="https://cdn.bootcss.com/jquery/2.2.4/jquery.js"></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() {

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())导出word,而经过百度js模板引擎的好处是也足以导出PDF文件。

完整demo:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="UTF-8">
 5     <script type="text/javascript" src="http://www.xdocin.com/xdoc.js"></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="https://cdn.bootcss.com/jquery/2.2.4/jquery.js"></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文档,所以有了此地散文。
当然,项目开支又时间有…

}

其他

是还是不是不要 JS,而是采用某些接口,间接完毕减少?

实在,在 HTML5 刚面世时,就留心到了一个效益:canvas 导出图片。可以变更
jpg、png 等格式。

如果在动脑筋的话,相信你也想开了。没错,就是 png —— 它是无损压缩的。

咱俩把普通数据当成像素点,画到 canvas 上,然后导出成
png,就是一个与众不一样的减弱包了~


下边初始探究。。。

svg 切分成几个图片导出

思路:浏览器即便对 canvas 有尺寸和面积的界定,然则对于 image
元素并没有显明的限制,也就是率先步生成的 image
其实显示是健康的,大家要做的只是在其次步 dragImage 的时候分多次将
image 元素切分并贴到 canvas 上然后下载下来。 同时,应小心到 image
的载入是一个异步的历程。

重视代码

JavaScript

// 构造 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 click伊夫nt = 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 {

多少转换

数量转像素,并不麻烦。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)); 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">(如若那位黑客拥有对于
evilhacker.com 的控制权),那么那位黑客就完全能不负众望下边的业务:

  • 假定有人查看她的材料,evilhacker.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);
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 =
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 click伊芙nt = new Mouse伊夫nt(“click”,
{ “view”: window, “bubbles”: true, “cancelable”: false });
a.dispatch伊夫nt(click伊芙nt); }); }, 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
    库打造的,那么可以直接使用库里面用于固定的函数,比一向从最底层运算尤其便民和标准。
  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);

// 得到画布

        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;

// 设置新图像的RGB值

                bitmap.setPixel(i, j, newColor);

}

}

return bitmap;

}

/**

* 图像二值化处理

*

    * @param mBitmapSrc 图片源

将HTML导出生成word文档,一些常用的图像处理情势。    * @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);

// 得到alpha通道的值

                int alpha = col &0xFF000000;

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

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

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

int blue = (col &0x000000FF);

// 用公式X = 0.3×R+0.59×G+0.11×B统计出X代替本来的RGB

                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;

}

}

/**

* 图片锐化(拉普拉斯变换)

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap sharpenImageAmeliorate(Bitmap mBitmapSrc) {

// 拉普拉斯矩阵

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

/*

* 怀旧处理算法即设置新的RGB

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

}

/**

* 图片浮雕

* 将最近像素点的RGB值分别与255之差后的值作为当下点的RGB

* 灰度图像:平常拔取的办法是gray=0.3*pixR+0.59*pixG+0.11*pixB

*

    * @param mBitmapSrc 图片源

    * @return Bitmap

*/

    public Bitmap reliefImage(Bitmap mBitmapSrc) {

/*

* 算法原理:(前一个像素点RGB-当前像素点RGB+127)作为当下像素点RGB值

* 在ABC中计算B点浮雕效果(RGB值在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) {

/*

* 算法原理:(前一个像素点RGB-当前像素点RGB+127)作为当前像素点RGB值

* 在ABC中计算B点浮雕效果(RGB值在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) {

// RGBA的最大值

        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;

}

}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图