NodeJS实现图片文本分割

 更新时间:2021年9月6日 00:00  点击:2363

本文实例为大家分享了NodeJS实现图片文本分割的具体代码,供大家参考,具体内容如下

var fs = require('fs');
var jpeg = require('jpeg-js')
 
 
function getSumRGB(data, i)
{
 var cr = data.data[i+0]
 var cg = data.data[i+1]
 var cb = data.data[i+2]
 var srgb = (cr+cg+cb)
 return srgb
}
 
function getTopRGB(data, i)
{
 var topIndex = (data.width * 4 * -1)
 i= i + topIndex;
 var cr = data.data[i+0]
 var cg = data.data[i+1]
 var cb = data.data[i+2]
 
 return [cr,cg,cb]
}
 
function getHeightRGB(data, i, haveRGB)
{
 var width = data.width
 var height = data.height
 var len = width * height * 4
 var haveNum = 0
 
 for(i=i;i<len;i+=width*4)
 {
  if(getSumRGB(data, i) == haveRGB)
  {
   haveNum++
  }
 }
 
 return haveNum
}
 
function ClearBackGround(data)
{ 
 var width = data.width
 var height = data.height
 var len = width * height * 4
 var i, tmp
 for(i=0;i<len;i+=4) 
 {
  tmp = getSumRGB(data, i)
  if(tmp > 120*3) {
   data.data[i+0]=255
   data.data[i+1]=255
   data.data[i+2]=255
  }
 }
 
 for(i=0;i<len;i+=4)
 {
  tmp = getSumRGB(data, i)
  if(tmp <= 32*3) {
   tmp = getTopRGB(data, i)
   data.data[i+0]=tmp[0]
   data.data[i+1]=tmp[1]
   data.data[i+2]=tmp[2]
  }
 }
 
 for(i=0;i<len;i+=4)
 {
  tmp = getSumRGB(data, i)
  if(tmp !=255*3) {
   data.data[i+0]=0
   data.data[i+1]=0
   data.data[i+2]=0
  }
 }
 
 var maxwidth  = width * 4
 var arrlist = []
 arrlist[0]=[]
 arrlist[1]=[]
 arrlist[2]=[]
 arrlist[3]=[]
 arrnum = 0
 block = 0
 
 for(i=0;i<maxwidth;i+=4)
 {
  tmp= getHeightRGB(data, i, 0)
  if(tmp==0) {
   if(block != 0) {
    arrlist[arrnum] = [block/4,i/4]
    block=0
    arrnum++
   }
  } else if(tmp >0 ) {
   if(block == 0) {
    block = i
   }
  }
 }
 
 console.log(arrlist)
 
 return data
}
 
 
var picname = "tmp.jpg"
var newpicname= "000.jpg"
var jpegData = fs.readFileSync(picname)
var rawImageData = jpeg.decode(jpegData, {useTArray: true})
rawImageData = ClearBackGround(rawImageData)
var jpegImageData = jpeg.encode(rawImageData,100)
fs.writeFileSync(newpicname, jpegImageData.data)

再为大家分享JS实现图片切割的方法:

//图片切割
var ImgCropper = Class.create();
ImgCropper.prototype = {
  //容器对象,控制层,图片地址
  initialize: function(container, handle, url, options) {
    this._Container = $(container);//容器对象
    this._layHandle = $(handle);//控制层
    this.Url = url;//图片地址
    
    this._layBase = this._Container.appendChild(document.createElement("img"));//底层
    this._layCropper = this._Container.appendChild(document.createElement("img"));//切割层
    this._layCropper.onload = Bind(this, this.SetPos);
    //用来设置大小
    this._tempImg = document.createElement("img");
    this._tempImg.onload = Bind(this, this.SetSize);
    
    this.SetOptions(options);
    
    this.Opacity = Math.round(this.options.Opacity);
    this.Color = this.options.Color;
    this.Scale = !!this.options.Scale;
    this.Ratio = Math.max(this.options.Ratio, 0);
    this.Width = Math.round(this.options.Width);
    this.Height = Math.round(this.options.Height);
    
    //设置预览对象
    var oPreview = $(this.options.Preview);//预览对象
    if(oPreview){
        oPreview.style.position = "relative";
        oPreview.style.overflow = "hidden";
        this.viewWidth = Math.round(this.options.viewWidth);
        this.viewHeight = Math.round(this.options.viewHeight);
        //预览图片对象
        this._view = oPreview.appendChild(document.createElement("img"));
        this._view.style.position = "absolute";
        this._view.onload = Bind(this, this.SetPreview);
    }
    //设置拖放
    this._drag = new Drag(this._layHandle, { Limit: true, onMove: Bind(this, this.SetPos), Transparent: true });
    //设置缩放
    this.Resize = !!this.options.Resize;
    if(this.Resize){
        var op = this.options, _resize = new Resize(this._layHandle, { Max: true, onResize: Bind(this, this.SetPos) });
        //设置缩放触发对象
        op.RightDown && (_resize.Set(op.RightDown, "right-down"));
        op.LeftDown && (_resize.Set(op.LeftDown, "left-down"));
        op.RightUp && (_resize.Set(op.RightUp, "right-up"));
        op.LeftUp && (_resize.Set(op.LeftUp, "left-up"));
        op.Right && (_resize.Set(op.Right, "right"));
        op.Left && (_resize.Set(op.Left, "left"));
        op.Down && (_resize.Set(op.Down, "down"));
        op.Up && (_resize.Set(op.Up, "up"));
        //最小范围限制
        this.Min = !!this.options.Min;
        this.minWidth = Math.round(this.options.minWidth);
        this.minHeight = Math.round(this.options.minHeight);
        //设置缩放对象
        this._resize = _resize;
    }
    //设置样式
    this._Container.style.position = "relative";
    this._Container.style.overflow = "hidden";
    this._layHandle.style.zIndex = 200;
    this._layCropper.style.zIndex = 100;
    this._layBase.style.position = this._layCropper.style.position = "absolute";
    this._layBase.style.top = this._layBase.style.left = this._layCropper.style.top = this._layCropper.style.left = 0;//对齐
    //初始化设置
    this.Init();
  },
  //设置默认属性
  SetOptions: function(options) {
    this.options = {//默认值
        Opacity:    50,//透明度(0到100)
        Color:        "",//背景色
        Width:        0,//图片高度
        Height:        0,//图片高度
        //缩放触发对象
        Resize:        false,//是否设置缩放
        Right:        "",//右边缩放对象
        Left:        "",//左边缩放对象
        Up:            "",//上边缩放对象
        Down:        "",//下边缩放对象
        RightDown:    "",//右下缩放对象
        LeftDown:    "",//左下缩放对象
        RightUp:    "",//右上缩放对象
        LeftUp:        "",//左上缩放对象
        Min:        false,//是否最小宽高限制(为true时下面min参数有用)
        minWidth:    50,//最小宽度
        minHeight:    50,//最小高度
        Scale:        false,//是否按比例缩放
        Ratio:        0,//缩放比例(宽/高)
        //预览对象设置
        Preview:    "",//预览对象
        viewWidth:    0,//预览宽度
        viewHeight:    0//预览高度
    };
    Extend(this.options, options || {});
  },
  //初始化对象
  Init: function() {
    //设置背景色
    this.Color && (this._Container.style.backgroundColor = this.Color);
    //设置图片
    this._tempImg.src = this._layBase.src = this._layCropper.src = this.Url;
    //设置透明
    if(isIE){
        this._layBase.style.filter = "alpha(opacity:" + this.Opacity + ")";
    } else {
        this._layBase.style.opacity = this.Opacity / 100;
    }
    //设置预览对象
    this._view && (this._view.src = this.Url);
    //设置缩放
    if(this.Resize){
        with(this._resize){
            Scale = this.Scale; Ratio = this.Ratio; Min = this.Min; minWidth = this.minWidth; minHeight = this.minHeight;
        }
    }
  },
  //设置切割样式
  SetPos: function() {
    //ie6渲染bug
    if(isIE6){ with(this._layHandle.style){ zoom = .9; zoom = 1; }; };
    //获取位置参数
    var p = this.GetPos();
    //按拖放对象的参数进行切割
    this._layCropper.style.clip = "rect(" + p.Top + "px " + (p.Left + p.Width) + "px " + (p.Top + p.Height) + "px " + p.Left + "px)";
    //设置预览
    this.SetPreview();
  },
  //设置预览效果
  SetPreview: function() {
    if(this._view){
        //预览显示的宽和高
        var p = this.GetPos(), s = this.GetSize(p.Width, p.Height, this.viewWidth, this.viewHeight), scale = s.Height / p.Height;
        //按比例设置参数
        var pHeight = this._layBase.height * scale, pWidth = this._layBase.width * scale, pTop = p.Top * scale, pLeft = p.Left * scale;
        //设置预览对象
        with(this._view.style){
            //设置样式
            width = pWidth + "px"; height = pHeight + "px"; top = - pTop + "px "; left = - pLeft + "px";
            //切割预览图
            clip = "rect(" + pTop + "px " + (pLeft + s.Width) + "px " + (pTop + s.Height) + "px " + pLeft + "px)";
        }
    }
  },
  //设置图片大小
  SetSize: function() {
    var s = this.GetSize(this._tempImg.width, this._tempImg.height, this.Width, this.Height);
    //设置底图和切割图
    this._layBase.style.width = this._layCropper.style.width = s.Width + "px";
    this._layBase.style.height = this._layCropper.style.height = s.Height + "px";
    //设置拖放范围
    this._drag.mxRight = s.Width; this._drag.mxBottom = s.Height;
    //设置缩放范围
    if(this.Resize){ this._resize.mxRight = s.Width; this._resize.mxBottom = s.Height; }
  },
  //获取当前样式
  GetPos: function() {
    with(this._layHandle){
        return { Top: offsetTop, Left: offsetLeft, Width: offsetWidth, Height: offsetHeight }
    }
  },
  //获取尺寸
  GetSize: function(nowWidth, nowHeight, fixWidth, fixHeight) {
    var iWidth = nowWidth, iHeight = nowHeight, scale = iWidth / iHeight;
    //按比例设置
    if(fixHeight){ iWidth = (iHeight = fixHeight) * scale; }
    if(fixWidth && (!fixHeight || iWidth > fixWidth)){ iHeight = (iWidth = fixWidth) / scale; }
    //返回尺寸对象
    return { Width: iWidth, Height: iHeight }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • NodeJS实现阿里大鱼短信通知发送

    本文给大家介绍的是nodejs实现使用阿里大鱼短信API发送消息的方法和代码,有需要的小伙伴可以参考下。...2016-01-20
  • Nodejs回调加超时限制两种实现方法

    这篇文章主要介绍了Nodejs回调加超时限制两种实现方法的相关资料,需要的朋友可以参考下...2017-06-15
  • Nodejs中使用captchapng模块生成图片验证码

    本篇文章主要介绍了Nodejs中使用captchapng模块实现图片验证码,非常具有实用价值,需要的朋友可以参考下 ...2017-05-22
  • Nodejs学习item【入门手上】

    这篇文章主要介绍了Nodejs学习item【入门手上】,需要的朋友可以参考下...2016-05-09
  • nodejs 终端打印进度条实例代码

    本篇文章主要介绍了nodejs 终端打印进度条实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...2017-04-27
  • Node.js检测端口(port)是否被占用的简单示例

    大家有没有遇到过在开启本地服务时,有这么一种情况:当前端口已经被另一个项目使用了,导致服务开启失败。那么接下来,我们通过简简单单的示例代码来检测端口是否已经被占用。有需要的朋友们可以参考借鉴。...2016-10-02
  • Nodejs express框架一个工程中同时使用ejs模版和jade模版

    这篇文章主要介绍了Nodejs express框架一个工程中同时使用ejs模版和jade模版 的相关资料,需要的朋友可以参考下...2015-12-29
  • Nodejs下DNS缓存问题浅析

    本文给大家一起探讨nodejs下dns的缓存问题,本文给大家介绍的非常详细,感兴趣的朋友一起看看吧...2016-11-22
  • 详解Windows下安装Nodejs步骤

    本篇文章主要介绍了详解Windows下安装Nodejs步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...2017-05-22
  • Nodejs 数组的队列以及forEach的应用详解

    这篇文章主要介绍了Nodejs 数组的队列以及forEach的应用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-25
  • node.js如何根据URL返回指定的图片详解

    这篇文章主要介绍了NODE.JS如何根据URL返回指定的图片详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-21
  • Node.js中使用jQuery的做法

    在Node.js中使用jQuery的做法,需要先安装jquery,npm install jquery ,安装后的版本是 3.1.0,本文介绍的非常详细,具有参考借鉴价值,感兴趣的朋友一起看下吧...2016-08-24
  • 详解Nodejs基于mongoose模块的增删改查的操作

    本篇文章主要介绍了Nodejs基于mongoose模块的增删改查的操作,Mongoose是MongoDB的一个对象模型工具,封装了MongoDB对文档的的一些增删改查等常用方法,让NodeJS操作Mongodb数据库变得更加灵活简单。 ...2016-12-31
  • NodeJS 实现手机短信验证模块阿里大于功能

    这篇文章主要介绍了NodeJS 实现手机短信验证模块阿里大于功能,需要的朋友可以参考下...2017-06-24
  • NodeJS通过魔术封包唤醒局域网计算机实例

    这篇文章主要为大家介绍了NodeJS通过魔术封包唤醒局域网计算机代码实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...2022-06-11
  • nodejs基础知识

    本文主要介绍了nodejs基础知识。具有很好的参考价值,下面跟着小编一起来看下吧...2017-02-08
  • 深入浅析NodeJs并发异步的回调处理

    这篇文章主要介绍了NodeJs并发异步的回调处理的相关资料,需要的朋友可以参考下...2015-12-24
  • 详解Node.Js如何处理post数据

    这篇文章给大家介绍了如何利用Node.Js处理post数据,文中通过实例和图文介绍的很详细,有需要的小伙伴们可以参考借鉴,下面来一起看看吧。...2016-10-03
  • Node.js+Express配置入门教程

    Node.js是一个Javascript运行环境(runtime)。本文给大家介绍Node.js+Express配置入门教程,值得推荐给大家,感兴趣的朋友一起学习吧...2016-05-20
  • NodeJS实现图片文本分割

    这篇文章主要为大家详细介绍了NodeJS实现图片文本分割,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-06