PhoneGap中文网

 找回密码
 立即注册
查看: 22787|回复: 1
打印 上一主题 下一主题

nodejs egg.js图片截取模块jimp

[复制链接]

28

主题

44

帖子

204

积分

中级会员

Rank: 3Rank: 3

积分
204
跳转到指定楼层
楼主
发表于 2018-10-11 12:39:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
jimp是一个非常优秀的nodejs图片截取模块。它无需再电脑上面安装其他工具,直接安装jimp模块就可以使用。
jimp的优势:1.简单,2.支持回调方式和ES6(promise)语法也可以链式调用 3. 丰富的api  4.无需安装任何程序(gm和canvas都需要)


nodejs图片截取模块jimp快速入门:


  1. 1.安装jimp    通过cnpm install --save jimp安装

  2. 2、引入jimp
  3. var Jimp = require("jimp");

  4. 3、使用:

  5. Jimp.read(target, function (err, lenna) {
  6.               if (err) throw err;

  7.               lenna.resize(200, 200)            
  8.                   .quality(80)               
  9.                   // .greyscale()                 
  10.                   .write(target+'_200x200'+path.extname(target));
  11.           });

复制代码



1、nodejs egg.js图片截取模块jimp 回调模式使用:


  1. var Jimp = require("jimp");

  2. Jimp.read("lenna.png", function (err, lenna) {
  3.      if (err) throw err;
  4.      lenna.resize(256, 256)
  5.           .quality(60)
  6.           .greyscale()
  7.           .write("lena-small-bw.jpg");
  8. });
复制代码


2、nodejs egg.js图片截取模块jimp Promise模式使用:



  1. var Jimp = require("jimp");

  2. Jimp.read("lenna.png").then(function (lenna) {
  3.      lenna.resize(256, 256)
  4.           .quality(60)
  5.           .greyscale()
  6.           .write("lena-small-bw.jpg");
  7. }).catch(function (err) {
  8.      console.error(err);
  9. });
复制代码


因为代码是运行在node平台,个人强烈建议使用promise的写法。

3、更多的用法可以参考github:https://github.com/oliver-moran/jimp,我把一些api复制下来


  1. /* Resize */
  2. image.contain( w, h[, mode] );    // scale the image to the given width and height, some parts of the image may be letter boxed
  3. image.cover( w, h[, mode] );      // scale the image to the given width and height, some parts of the image may be clipped
  4. image.resize( w, h[, mode] );     // resize the image. Jimp.AUTO can be passed as one of the values.
  5. image.scale( f[, mode] );         // scale the image by the factor f
  6. image.scaleToFit( w, h[, mode] ); // scale the image to the largest size that fits inside the given width and height

  7. // An optional resize mode can be passed with all resize methods.

  8. /* Crop */
  9. image.autocrop();                 // automatically crop same-color borders from image (if any)
  10. image.crop( x, y, w, h );         // crop to the given region

  11. /* Composing */
  12. image.blit( src, x, y[, srcx, srcy, srcw, srch] );
  13.                                   // blit the image with another Jimp image at x, y, optionally cropped.
  14. image.composite( src, x, y );     // composites another Jimp image over this iamge at x, y
  15. image.mask( src, x, y );          // masks the image with another Jimp image at x, y using average pixel value image.rotate) and when writing formats that don't support alpha channels

  16. /* Flip and rotate */
  17. image.flip( horz, vert );         // flip the image horizontally or vertically
  18. image.mirror( horz, vert );       // an alias for flip
  19. image.rotate( deg[, mode] );      // rotate the image clockwise by a number of degrees. Optionally, a resize mode can be passed. If `false` is passed as the second parameter, the image width and height will not be resized.

  20. // JPEG images with EXIF orientation data will be automatically re-orientated as appropriate.

  21. /* Colour */
  22. image.brightness( val );          // adjust the brighness by a value -1 to +1
  23. image.contrast( val );            // adjust the contrast by a value -1 to +1
  24. image.dither565();                // ordered dithering of the image and reduce color space to 16-bits (RGB565)
  25. image.greyscale();                // remove colour from the image
  26. image.invert();                   // invert the image colours
  27. image.normalize();                // normalize the channels in an image

  28. /* Alpha channel */
  29. image.fade( f );                  // an alternative to opacity, fades the image by a factor 0 - 1. 0 will haven no effect. 1 will turn the image
  30. image.opacity( f );               // multiply the alpha channel by each pixel by the factor f, 0 - 1
  31. image.opaque();                   // set the alpha channel on every pixel to fully opaque
  32. image.background( hex );          // set the default new pixel colour (e.g. 0xFFFFFFFF or 0x00000000) for by some operations (e.g. image.contain and

  33. /* Blurs */
  34. image.gaussian( r );              // Gaussian blur the image by r pixels (VERY slow)
  35. image.blur( r );                  // fast blur the image by r pixels

  36. /* Effects */
  37. image.posterize( n );             // apply a posterization effect with n level
  38. image.sepia();                    // apply a sepia wash to the image
复制代码





1.  在图片上书写文字

  1. Jimp.loadFont( path ).then(function (font) {
  2.      image.print(font, x, y, str);
  3. });
复制代码


2. 读取图片字节流
  1. image.getBuffer( buffer , cb )
复制代码



3. 图片上某一点像素的操作

  1. image.getPixelColor(x, y)  //获取图片上某一点的像素值
  2. image.setPixelColor(hex, x, y);  //设置图片上的某一点像素
复制代码


4. 工具函数

  1. Jimp.rgbaToInt(r, g, b, a);   //将十进制转化为十六进制
  2. Jimp.intToRGBA(hex);    //将十六进制转化为十进制
复制代码


5. 判断图片是否为同一张图

  1. Jimp使用的了pHash算法对图片进行了计算,我们可以利用这个特点进行图片的比较,避免服务器保存了相同的图片
复制代码



海明威码判别

  1. Jimp.distance(image1, image2);  //返回值为相似程度,0表示两张图完全一样
复制代码


像素比较法

  1. var diff = Jimp.diff(image1, image2, threshold);
  2. diff.image;     //一张用来展示两张图片不一样的地方的图片
  3. diff.percent;   //像素不相同的比例        
复制代码


其中参数中threshold的取值为0-1,代表了比较的严格程度,0表示最严格,实际中可以结合使用两种方式来判断。







回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

ionic4视频教程

Archiver|手机版|小黑屋| PhoneGap中文网 ( 京ICP备13027796号-1 )  

GMT+8, 2024-4-27 04:04 , Processed in 0.048666 second(s), 29 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表