NodeJS實現(xiàn)圖片文本分割
本文實例為大家分享了NodeJS實現(xiàn)圖片文本分割的具體代碼,供大家參考,具體內(nèi)容如下

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實現(xiàn)圖片切割的方法:
//圖片切割
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) });
//設置縮放觸發(fā)對象
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,//圖片高度
//縮放觸發(fā)對象
Resize: false,//是否設置縮放
Right: "",//右邊縮放對象
Left: "",//左邊縮放對象
Up: "",//上邊縮放對象
Down: "",//下邊縮放對象
RightDown: "",//右下縮放對象
LeftDown: "",//左下縮放對象
RightUp: "",//右上縮放對象
LeftUp: "",//左上縮放對象
Min: false,//是否最小寬高限制(為true時下面min參數(shù)有用)
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; }; };
//獲取位置參數(shù)
var p = this.GetPos();
//按拖放對象的參數(shù)進行切割
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;
//按比例設置參數(shù)
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 }
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
node.js連接mongoDB數(shù)據(jù)庫 快速搭建自己的web服務
這篇文章主要為大家詳細介紹了node.js連接mongoDB數(shù)據(jù)庫,如何快速搭建自己的web服務,感興趣的小伙伴們可以參考一下2016-04-04
node+koa實現(xiàn)數(shù)據(jù)mock接口的方法
本篇文章主要介紹了node+koa實現(xiàn)數(shù)據(jù)mock接口的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
淺析nodejs實現(xiàn)Websocket的數(shù)據(jù)接收與發(fā)送
WebSocket是HTML5開始提供的一種瀏覽器與服務器間進行全雙工通訊的網(wǎng)絡技術,本文給大家介紹nodejs實現(xiàn)websocket的數(shù)據(jù)庫接收與發(fā)送,小伙伴們一起學習吧2015-11-11
node終端里如何連接mysql數(shù)據(jù)庫并進行sql查詢
這篇文章主要為大家介紹了node終端里如何連接mysql數(shù)據(jù)庫并進行sql查詢,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
使用?Node-RED對?MQTT?數(shù)據(jù)流處理
本文將介紹使用 Node-RED 連接到 MQTT 服務器,并對 MQTT 數(shù)據(jù)進行過濾和處理后再將其發(fā)送至 MQTT 服務器的完整操作流程。讀者可以快速了解如何使用 Node-RED 對 MQTT 數(shù)據(jù)進行簡單的流處理2022-05-05
Node+Express+MongoDB實現(xiàn)登錄注冊功能實例
這篇文章主要介紹了Node+Express+MongoDB實現(xiàn)登錄注冊功能,需要的朋友可以參考下2017-04-04
nodejs文件操作模塊FS(File System)常用函數(shù)簡明總結(jié)
這篇文章主要介紹了nodejs文件操作模塊FS(File System)常用函數(shù)簡明總結(jié),對FS模塊的大部份異步函數(shù)做了介紹,而且用中文注釋,這下用起來方便了,需要的朋友可以參考下2014-06-06

