画像における空白を分析するために、私が知っている唯一の方法は、canvas
にそのイメージをロードすることである。
var img = new Image(),
$canvas = $("<canvas>"), // create an offscreen canvas
canvas = $canvas[0],
context = canvas.getContext("2d");
img.onload = function() {
context.drawImage(this, 0, 0); // put the image in the canvas
$("body").append($canvas);
removeBlanks(this.width, this.height);
};
// test image
img.src = 'http://images.productserve.com/preview/1302/218680281.jpg';
次に、getImageData()方法を使用します。このメソッドは、各ピクセルデータ(カラー)を検査するために使用できるImageDataオブジェクトを返します。
var removeBlanks = function (imgWidth, imgHeight) {
var imageData = context.getImageData(0, 0, canvas.width, canvas.height),
data = imageData.data,
getRBG = function(x, y) {
return {
red: data[(imgWidth*y + x) * 4],
green: data[(imgWidth*y + x) * 4 + 1],
blue: data[(imgWidth*y + x) * 4 + 2]
};
},
isWhite = function (rgb) {
return rgb.red == 255 && rgb.green == 255 && rgb.blue == 255;
},
scanY = function (fromTop) {
var offset = fromTop ? 1 : -1;
// loop through each row
for(var y = fromTop ? 0 : imgHeight - 1; fromTop ? (y < imgHeight) : (y > -1); y += offset) {
// loop through each column
for(var x = 0; x < imgWidth; x++) {
if (!isWhite(getRBG(x, y))) {
return y;
}
}
}
return null; // all image is white
},
scanX = function (fromLeft) {
var offset = fromLeft? 1 : -1;
// loop through each column
for(var x = fromLeft ? 0 : imgWidth - 1; fromLeft ? (x < imgWidth) : (x > -1); x += offset) {
// loop through each row
for(var y = 0; y < imgHeight; y++) {
if (!isWhite(getRBG(x, y))) {
return x;
}
}
}
return null; // all image is white
};
var cropTop = scanY(true),
cropBottom = scanY(false),
cropLeft = scanX(true),
cropRight = scanX(false);
// cropTop is the last topmost white row. Above this row all is white
// cropBottom is the last bottommost white row. Below this row all is white
// cropLeft is the last leftmost white column.
// cropRight is the last rightmost white column.
};
は、率直に言って、私は正当な理由のために、このコードをテストすることができませんでした:私は、悪名高い出くわしたセキュリティ例外「キャンバスクロスオリジン・データによって汚染されていますので、キャンバスから画像データを取得することができません。」 。
これはバグではなく、意図した機能です。 specsから:
toDataURL()、toDataURLHD()、toBlob()、getImageData()、及び getImageDataHD()メソッドフラグをチェックし、例外SecurityError 例外をスローするのではなく、クロスオリジンデータを漏洩します。
さらにデータ操作を防止する際drawImage()
ロードファイルキャンバスの起源クリーンフラグがfalseに設定されるようになり、外部ドメイン、からこれが起こります。
私はあなたが同じ問題に遭遇するだろうかと思いますが、これはクライアント側で動作する場合であっても、とにかく、here is the code.
、私は性能面になりますどのように悲惨な想像することができます。だから、Janさんが言ったように、あなたが画像をダウンロードしてサーバー側で前処理することができれば、それは良いでしょう。
編集:私は自分のコードが実際に画像をトリミングするかどう見て興味があった、と確かにそれはありません。
あなたは前に述べたようにそれだけで、あなたのドメインからのイメージのために働くhere
それをチェックアウトすることができます。あなたがいないjsFiddleから、自分のドメインからコードを実行する必要があります、明らかに
// define here an image from your domain
img.src = 'http://localhost/strawberry2.jpg';
:あなたは、白い背景を使用して独自の画像を選択して、最後の行を変更することができます。
EDIT2:あなたはトリミングと同じアスペクト比を維持するためにスケールアップしたい場合は、この
var $croppedCanvas = $("<canvas>").attr({ width: cropWidth, height: cropHeight });
// finally crop the guy
$croppedCanvas[0].getContext("2d").drawImage(canvas,
cropLeft, cropTop, cropWidth, cropHeight,
0, 0, cropWidth, cropHeight);
へ
var $croppedCanvas = $("<canvas>").attr({ width: imgWidth, height: imgHeight });
// finally crop the guy
$croppedCanvas[0].getContext("2d").drawImage(canvas,
cropLeft, cropTop, cropWidth, cropHeight,
0, 0, imgWidth, imgHeight);
EDIT3を変更します。ブラウズで画像をトリミングする1つの方法このexcellent articleが説明するように、Webワーカーを使用して作業負荷を並列化することです。
実際に問題が発生した画像の1つを質問に追加することをお勧めします。あなたのサイトを効果的に宣伝しているので投票が終わることはありません。 –
イメージをキャンバスに描画し、空白の列と行全体を削除し、イメージを同じサイズ(縦横比を保持)に再スケーリングすることができます。 – Prusse
「画像を前処理するのではなく、JavaScriptを使用したい」 - なぜですか?なぜあなたは、サーバー側で一度だけ実行するのではなく、白いスペースを削除する必要がありますか? – h2ooooooo