- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我被要求编写一个程序来获取图像,读取位图数据,然后随机化图像。
我尝试了简单的方法,在每个“”上分割“字符串”,然后对其进行洗牌,但是图像损坏了,知道如何做到这一点吗?
我会在 cocoa、actionscript 3、js 或 php 上执行此操作。
基本上只要我能在 Mac 上使用它,我的客户就会很高兴:)
最佳答案
javascript中逐 block 打乱图像的解决方案:http://fiddle.jshell.net/upgradellc/53wKG/show/编辑:http://jsfiddle.net/upgradellc/53wKG/
/* Script copyright Max @ www.upgradeyour.com - if it is used or modified, this message must remain intact! */
$( document ).ready( function() {
var imgData1 = 'img_url_or_base64';
new ImageShuffler( imgData1, 3 );
new ImageShuffler( imgData1, 5 );
new ImageShuffler( imgData1, 10 );
} );
function ImageShuffler( imgUrl, numberOfSquares, elementToAddTo ) {
var that = this;
this.url = imgUrl;
this.numberOfSquares = numberOfSquares;
this.elementToAddTo = elementToAddTo || $( 'body' );
this.holder = $( '<div></div>' ).appendTo( $( this.elementToAddTo ) )[0];
this.c1 = $( '<canvas></canvas>' ).appendTo( $( this.holder ) )[0];
this.c2 = $( '<canvas></canvas>' ).appendTo( $( this.holder ) )[0];
this.img = $( '<img src="' + this.url + '">' ).prependTo( $( this.holder ) )[0];
this.img.onload = function() {
that.doShuffleImage()
};
}
ImageShuffler.prototype.doShuffleImage = function() {
this.widthOfSquares = Math.ceil( this.img.width / this.numberOfSquares );
this.heightOfSquares = Math.ceil( this.img.height / this.numberOfSquares );
var extrax = (this.img.width - this.widthOfSquares * this.numberOfSquares);
var extray = (this.img.height - this.heightOfSquares * this.numberOfSquares);
var width = this.removeExtraPixels( this.img.width, extrax, this.widthOfSquares );
var height = this.removeExtraPixels( this.img.height, extray, this.heightOfSquares );
this.c1.width = this.c2.width = width;
this.c1.height = this.c2.height = height;
this.c1c = this.c1.getContext( '2d' );
this.c2c = this.c2.getContext( '2d' );
this.c1c.drawImage( this.img, 0, 0, this.img.width, this.img.height );
var tlc = this.c1c.getImageData( this.img.width - 1, this.img.height - 1, 1, 1 ).data;
this.c1c.fillStyle = "rgb(" + tlc[0] + "," + tlc[1] + "," + tlc[2] + ");";
this.c1c.fillRect( 0, 0, this.c1.width, this.c1.height );
this.c1c.drawImage( this.img, 0, 0, this.img.width, this.img.height );
this.shuffleAll();
};
ImageShuffler.prototype.shuffleAll = function() {
this.c2c.putImageData( this.c1c.getImageData( 0, 0, this.c1.width, this.c1.height ), 0, 0 );
//this.c1c.getImageData( 0, 0, this.img.width, this.img.height );
var timesToShuffle = Math.pow( this.numberOfSquares, 2 );
for( var count = 0; count < timesToShuffle; count++ ) {
p1 = {x: rand( this.c2.width, this.widthOfSquares ), y: rand( this.c2.height, this.heightOfSquares )};
p2 = {x: rand( this.c2.width, this.widthOfSquares ), y: rand( this.c2.height, this.heightOfSquares )};
if( p1.x + this.widthOfSquares < this.c2.width ) {
this.swapTile( p1.x, p1.y, p2.x, p2.y );
}
}
};
ImageShuffler.prototype.swapTile = function( x1, y1, x2, y2 ) {
tile1 = this.c2c.getImageData( x1, y1, this.widthOfSquares, this.heightOfSquares );
tile2 = this.c2c.getImageData( x2, y2, this.widthOfSquares, this.heightOfSquares );
this.c2c.putImageData( tile1, x2, y2 );
this.c2c.putImageData( tile2, x1, y1 );
};
ImageShuffler.prototype.removeExtraPixels = function( currentLength, extraPixels, sizeOfSquare ) {
if( extraPixels < 0 ) {
return currentLength + (-1 * extraPixels);
}
if( extraPixels > 0 ) {
return currentLength + sizeOfSquare - extraPixels;
}
return currentLength;
};
//returns a random number below max which is a multiple of increment
function rand( max, increment ) {
return Math.floor( Math.random() * Math.ceil( max / increment ) ) * increment;
}
javascript中用于打乱图像中所有像素的解决方案: http://jsfiddle.net/upgradellc/2LJwH/1/
/* Script copyright Max @ www.upgradeyour.com - if it is used or modified, this message must remain intact! */
$( document ).ready( function() {
var imgData1 = '/img/logo.png';
new ImageShuffler( imgData1 );
} );
function ImageShuffler( imgUrl, elementToAddTo ) {
var that = this;
this.url = imgUrl;
this.elementToAddTo = elementToAddTo || $( 'body' );
this.holder = $( '<div></div>' ).appendTo( $( this.elementToAddTo ) )[0];
console.log( this.holder );
this.c1 = $( '<canvas></canvas>' ).appendTo( $( this.holder ) )[0];
this.c2 = $( '<canvas></canvas>' ).appendTo( $( this.holder ) )[0];
this.img = $( '<img src="' + this.url + '">' ).prependTo( $( this.holder ) )[0];
this.img.onload = function() {
that.doShuffleImage()
};
}
ImageShuffler.prototype.doShuffleImage = function() {
this.c1.width = this.c2.width = this.img.width;
this.c1.height = this.c2.height = this.img.height;
this.c1c = this.c1.getContext( '2d' );
this.c2c = this.c2.getContext( '2d' );
this.c1c.drawImage( this.img, 0, 0, this.img.width, this.img.height );
this.c2c.putImageData( shuffleArray( this.c1c.getImageData( 0, 0, this.img.width, this.img.height ) ), 0, 0 );
};
//shuffles the data array
function shuffleArray( arr ) {
var length = arr.data.length;
for( x = 0; x < length; x++ ) {
var p1x = rand( length, 4 ), p2x = rand( length, 4 );
var p1r = arr.data[p1x];
var p1g = arr.data[p1x + 1];
var p1b = arr.data[p1x + 2];
var p1a = arr.data[p1x + 3];
for( i = 0; i < 3; i++ ) {
arr.data[p2x + i] = arr.data[p1x + i];
}
arr.data[p2x] = p1r;
arr.data[p2x + 1] = p1g;
arr.data[p2x + 2] = p1b;
arr.data[p2x + 3] = p1a;
}
return arr;
}
//returns a random number below max which is a multiple of increment
function rand( max, increment ) {
return Math.floor( Math.random() * Math.ceil( max / increment ) ) * increment;
}
关于actionscript-3 - 如何打乱位图数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17225017/
我正在使用下面的随机播放脚本来随机化网站上某些 div 的顺序。 http://james.padolsey.com/javascript/shuffling-the-dom/ 该脚本有效,但仅随机化
我想打乱 HashMap 中的值。我有以下类型的 HashMap Map > trainDataSet = new HashMap>(); 我想打乱 map 中的值。我该怎么做呢? 以下是我的尝试:
这个问题已经有答案了: Shuffle a list of integers with Java 8 Streams API (8 个回答) 已关闭 3 年前。 我有一个单词列表:List words
我必须使用 SQL SELECT 语句返回一些结果。我需要返回两条关于员工的信息,Employee# 和 EmployeeName。 我试过了 SELECT Employee#, EmployeeNa
我有一个网页,我正在尝试打印它。我正在使用引导导航栏。问题是,当我使用 bootstarp 导航栏时,打印预览没有任何意义。我在页面中看到了代码行而不是实际内容。只有当我将引用添加到 bootstra
我用 16 位值加载两个 SSE 128 位寄存器。这些值按以下顺序排列: src[0] = [E_3, O_3, E_2, O_2, E_1, O_1, E_0, O_0] src[1] = [E_
我需要做的是:原始状态: 洗牌后: 第一个 div 中的 Divs 留在那里但会被打乱,同样的情况也会发生在具有相同类的第二个 div 中。要在特定的 div
我有一个列表: milk butter eggs orange juice bananas 如何使用 javascript 随机重新排序列表项? 最佳答案
我有一个大小为 n 的 NSMutableArray urlArray,我想从数组中的元素总数中随机选择其中的 4 个 URL。 但是我不想直接打乱 urlArray,我更喜欢制作一个“indexAr
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
首先,我的母语不是英语,所以请原谅我的一些错误。 我想对 ArrayList 进行洗牌(没问题),但是洗牌后列表必须满足某些条件。我的第一个方法是创建 if 语句,并在每次为 true 时进行洗牌。但
我想打乱 __m256i vector 的元素。并且有一个内在的 _mm256_shuffle_epi8 做类似的事情,但它不执行跨车道洗牌。 如何使用 AVX2 指令来实现? 最佳答案 有一种方法可
有谁知道改变现有 NSString 或 NSMutableString 字符顺序的现有方法吗?无论如何,我有一个解决方法,但如果有一个现有的方法,那就太好了。 例如,给定字符串@"HORSE",一个方
我是 Objective-C 新手,通过反复试验来学习!如果这个问题有点幼稚,请原谅我。 我创建了一组图像,需要对它们进行随机播放。我已经使用了这里给出的建议: What's the Best Way
我们有一个网络应用程序,希望向潜在客户演示,但我们最好的方法是使用现有数据,以获得完整的体验。当然,我们不想使用应用程序中可见的实际客户名称或地址等来执行此操作。 SQL Server 中有没有一种简
有一个简单的方法来缩小和/或混淆 JS,它存在于我的 django 模板中? 我还在js中使用模板变量和模板标签,因此,标准的uglifyjs或类似的不符合我最初的要求。 有什么事情要做吗? 最佳答案
我是新用户 ArrayList 。我不明白。 如果我使用int[]作为ArrayList项目有错误: The method put(int) is undefined for the type Arr
是否可以随机打乱 Excel 工作表中行或列中的值。有没有相关的函数/宏?我该怎么做? -广告。 最佳答案 嗯...就像简单的解决方案一样,无需编程。例如,您有一列数据 (A): 23 78 12 7
我想打乱我的数组项。像这样: [1,2,3,4].scramble => [2,1,3,4] [1,2,3,4].scramble => [3,1,2,4] [1,2,3,4].scramble =>
我目前正在为 Android 开发内容创建应用程序。它的主要目的是让用户生成一个包含文本和图像条目的列表(最后由 EditText 和 ImageView 表示)。 我通过使用 ViewHolder
我是一名优秀的程序员,十分优秀!