Java 旋转位图像素
我正在尝试旋转一个位图,其中像素存储在数组Java 旋转位图像素,java,bitmap,rotation,transformation,Java,Bitmap,Rotation,Transformation,我正在尝试旋转一个位图,其中像素存储在数组int pixels[]中。我得到了以下方法: public void rotate(double angle) { double radians = Math.toRadians(angle); double cos, sin; cos = Math.cos(radians); sin = Math.sin(radians); int[] pixels2 = pixels; for (int x = 0;
int pixels[]
中。我得到了以下方法:
public void rotate(double angle) {
double radians = Math.toRadians(angle);
double cos, sin;
cos = Math.cos(radians);
sin = Math.sin(radians);
int[] pixels2 = pixels;
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++) {
int centerx = this.width / 2, centery = this.height / 2;
int m = x - centerx;
int n = y - centery;
int j = (int) (m * cos + n * sin);
int k = (int) (n * cos - m * sin);
j += centerx;
k += centery;
if (!((j < 0) || (j > this.width - 1) || (k < 0) || (k > this.height - 1)))
try {
pixels2[(x * this.width + y)] = pixels[(k * this.width + j)];
} catch (Exception e) {
e.printStackTrace();
}
}
pixels = pixels2;
}
public void旋转(双角度){
双弧度=数学上的环面(角度);
双因,罪;
cos=数学cos(弧度);
sin=数学sin(弧度);
int[]像素2=像素;
对于(int x=0;xthis.width-1)| |(k<0)| |(k>this.height-1)))
试一试{
像素s2[(x*this.width+y)]=像素[(k*this.width+j)];
}捕获(例外e){
e、 printStackTrace();
}
}
像素=像素2;
}
但它给了我疯狂的结果。有人知道错误在哪里吗?线路
int[] pixels2 = pixels;
应该复制数组,但您只是复制对它的引用。使用pixels.clone()
。实际上,您只需要一个新的空数组,所以newint[pixels.lenght]
就足够了。最后,您需要System.arraycopy
将新内容复制到旧数组中
代码中还存在其他问题——行和列混淆了。有些表达式的编写方式类似于图像是逐行存储的,而另一些表达式则类似于逐列存储的。如果是一行一行(我的假设),那么这没有意义:x*width+y
。它应该是y*width+x
——您正在向下跳过y
行,然后向右移动x
列。总而言之,我有一个代码可以正常工作:
import static java.lang.System.arraycopy;
public class Test
{
private final int width = 5, height = 5;
private int[] pixels = {0,0,1,0,0,
0,0,1,0,0,
0,0,1,0,0,
0,0,1,0,0,
0,0,1,0,0};
public Test rotate(double angle) {
final double radians = Math.toRadians(angle),
cos = Math.cos(radians), sin = Math.sin(radians);
final int[] pixels2 = new int[pixels.length];
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++) {
final int
centerx = this.width / 2, centery = this.height / 2,
m = x - centerx,
n = y - centery,
j = ((int) (m * cos + n * sin)) + centerx,
k = ((int) (n * cos - m * sin)) + centery;
if (j >= 0 && j < width && k >= 0 && k < this.height)
pixels2[(y * width + x)] = pixels[(k * width + j)];
}
arraycopy(pixels2, 0, pixels, 0, pixels.length);
return this;
}
public Test print() {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++)
System.out.print(pixels[width*y + x]);
System.out.println();
}
System.out.println();
return this;
}
public static void main(String[] args) {
new Test().print().rotate(-45).print();
}
}
导入静态java.lang.System.arraycopy;
公开课考试
{
专用最终整型宽度=5,高度=5;
私有int[]像素={0,0,1,0,0,
0,0,1,0,0,
0,0,1,0,0,
0,0,1,0,0,
0,0,1,0,0};
公共测试旋转(双角度){
最终双弧度=数学托拉度(角度),
cos=Math.cos(弧度),sin=Math.sin(弧度);
final int[]pixels2=新int[pixels.length];
对于(int x=0;x=0&&j=0&&k
公共void渲染(浮动nx、浮动ny、浮动nz、浮动大小、浮动旋转){
int wid=(int)((宽度-nz)*大小);
int hgt=(int)((高度-新西兰)*尺寸);
如果(wid<0 | | hgt<0){
wid=0;
hgt=0;
}
对于(int x=0;x=Main.width
||yp>=主高度){
打破
}
如果(xp<0
||yp<0
||像素[(宽度/宽度)*x+((高度/高度)*y)
*宽度]==0xFFFF00DC){
继续;
}
Main.pixels[xp+yp*Main.width]=像素[(宽度/宽度)*x
+((高度/高度)*y)*宽度];
}
}
}
这对我来说只是一个新的旋转,但这个过程是一个正常旋转的过程。它仍然需要大量的修复——效率低,速度慢。但是在一个小程序中,这段代码是有效的。我发布这篇文章是为了让你能接受它,让它变得更好。:) 请注意,
clone()
会创建一个浅拷贝,也就是说,它不会克隆数组的内容。对于int
来说,这并不重要,但是一般来说,最好像int[]pixels2=数组那样使用Arrays.copyOf
。copyOf(像素,像素.长度)
copyOf
也可以制作浅拷贝。事实上,没有标准的机制来深度复制对象。
public void render(float nx, float ny, float nz, float size, float rotate) {
int wid = (int) ((width - nz) * size);
int hgt = (int) ((height - nz) * size);
if (wid < 0 || hgt < 0) {
wid = 0;
hgt = 0;
}
for (int x = 0; x < wid; x++) {
for (int y = 0; y < hgt; y++) {
double simple = Math.PI;
int xp = (int) (nx +
Math.cos(rotate) * ((x / simple) - (wid / simple) / 2) + Math
.cos(rotate + Math.PI / 2)
* ((y / simple) - (hgt / simple) / 2));
int yp = (int) (ny + Math.sin(rotate)
* ((x / simple) - (wid / simple) / 2) + Math.sin(rotate
+ Math.PI / 2)
* ((y / simple) - (hgt / simple) / 2));
if (xp + width < 0 || yp + height < 0 || xp >= Main.width
|| yp >= Main.height) {
break;
}
if (xp < 0
|| yp < 0
|| pixels[(width / wid) * x + ((height / hgt) * y)
* width] == 0xFFFF00DC) {
continue;
}
Main.pixels[xp + yp * Main.width] = pixels[(width / wid) * x
+ ((height / hgt) * y) * width];
}
}
}