[英]Manually rotate image in java?
I already save my image information in bitmap: 我已经将图像信息保存在位图中:
for(int j=0; j<tinggi; j++) {
for(int i=0; i<lebar; i++) {
warna = pixels[j*lebar+i];
alpha = (warna >>24) &0xff;
red = (warna >>16) & 0xff;
bmpR[i][j] = red;
green = (warna >>8) &0xff;
bmpG[i][j] = green;
blue = blue = (warna ) &0xff;
bmpB[i][j] = blue;
}
}
I try to rotate the image with this code: 我尝试使用以下代码旋转图像:
for(int j=0;j<tinggi;j++) {
for(int i=0;i<lebar;i++) {
double xr = (i*Math.cos(r))-(j*Math.sin(r));
double yr = (i*Math.sin(r))+(j*Math.cos(r));
int xro = (int) Math.round(xr);
int yro = (int) Math.round(yr);
rotationR [i+ xro][j+ yro] = (bmpR[i][j]);
rotationG [i+ xro][j+ yro] = (bmpG[i][j]);
rotationB [i+ xro][j+ yro] = (bmpB[i][j]);
}
}
for(int j=0;j<tinggi;j++) {
for(int i=0;i<lebar;i++) {
g.setColor(new Color(rotationR[i][j], rotationG[i][j], rotationB[i][j]));
g.drawLine(i+lebar+100, j+450, i+lebar+100, j+450);
}
}
But it doesn't output anything (While the translation and scaling is working). 但是它什么也没输出(虽然转换和缩放有效)。
What's wrong with my rotation code? 我的旋转代码有什么问题?
there is a flaw in your logic: 您的逻辑存在缺陷:
you have to store the new location in a array instead of changing the array indice 您必须将新位置存储在数组中,而不是更改数组索引
Point[][] rotatedLocations = ...
for(int j=0;j<tinggi;j++) {
for(int i=0;i<lebar;i++) {
double xr = (i*Math.cos(r))-(j*Math.sin(r));
double yr = (i*Math.sin(r))+(j*Math.cos(r));
int xro = (int) Math.round(xr);
int yro = (int) Math.round(yr);
rotatedLocations[i][j] = new Point(xr, yr);
}
}
when you draw the pixel, draw them just on the associated location 绘制像素时,仅在关联位置上绘制它们
for(int j=0;j<tinggi;j++) {
for(int i=0;i<lebar;i++) {
//new (rotated) location for original x/y
Point rotatedLocation = rotatedLocations[i][j];
//color from the original
g.setColor(new Color(original[i][j], original[i][j], original[i][j]));
//but drawn on the new (rotated) location
g.drawLine(rotatedLocation.x, rotatedLocation.y, rotatedLocation.x, rotatedLocation.y);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.