К теме вращения картинок.
Выкладываю готовый класс, в котором реализованы методы вращения и изменения размеров изображений.
Надеюсь, кому-то это сэкономит время.
| Код | /** * This class contains methods, what may rotate and scale images. * Cos and sin got from Midp 2.0 * */
public class Translator {
/** * Rotate source image and return rotated. * * @param argb_source source image * @param argb_rotated rotated image * @param alpha angle for rotate * @param imageWidth - width of image * @param imageHeight - height of image */ public static void rotateImage(int[] argb_source, int[] argb_rotated, int alpha, int imageWidth, int imageHeight) {
int x0, y0, x1, y1; long sn =(long)( Math.sin( Math.toRadians( alpha ))* 10000); long cs = (long)( Math.cos( Math.toRadians( alpha ))* 10000); int xc = imageWidth / 2; int yc = imageHeight / 2;
for (y1 = 0; y1 < imageHeight; y1++) {
for (x1 = 0; x1 < imageWidth; x1++) {
// На всякий случай заполняем картинку цветом фона int bgcol = 0; argb_rotated [ y1 * imageWidth + x1 ] = bgcol; x0 = ( int ) ( ( cs * ( x1 - xc ) + sn * ( y1 - yc ) ) / 10000 + xc ); y0 = ( int ) ( - ( sn * ( x1 - xc ) - cs * ( y1 - yc ) ) / 10000 + yc ); // Проверяем, не выходит ли точка за пределы области if ( x0 > -1 ) if ( x0 < imageWidth) if ( y0 > -1 ) if ( y0 < imageHeight ) { // Закрашиваем точку argb_rotated[ y1 * imageWidth + x1 ] = argb_source[ y0 * imageWidth + x0 ]; }
}
} } /** * Scale source image. * * Scaled image has same width and height as source, but contents is scaled. * If you want grows image, set lambda > 1, else set lambda < 1 * * @param argb_source - given image for scale * @param argb_scaled - result scaled image * @param lambda - coefficient of scale * @param imageWidth - width of image * @param imageHeight - height of image */ public static void scaleImage(int[] argb_source, int[] argb_scaled, double lambda, int imageWidth, int imageHeight) {
int x0, y0, x1, y1;
for (y1 = 0; y1 < imageHeight; y1++) {
for (x1 = 0; x1 < imageWidth; x1++) {
int bgcol = 0; argb_scaled [ y1 * imageWidth + x1 ] = bgcol; x0 = ( int ) (x1 / lambda); y0 = ( int ) (y1 / lambda); if ( x0 > -1 ) if ( x0 < imageWidth ) if ( y0 > -1 ) if ( y0 < imageHeight ) { argb_scaled[ y1 * imageWidth + x1 ] = argb_source[ y0 * imageWidth + x0 ]; } }
} }
}
|
Автор: http://vingrad.ru/@GhostMasterNet Источник: http://forum.vingrad.ru/index.php?show_type=forum&showtopic=167315 |