Jump to content
Sign in to follow this  

Rotação de imagem

Recommended Posts


Boa noite, eu ando a tentar desenvolver um programa em java que permite roda-la um determinado angulo, eu baseie-me em algoritmos que encontrei pela net e ate agora tenho isto:

import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.util.Scanner;

public class Gira {

    public static double Vcos;
    public static double Vsin;
    public static float angle;
    public static double hw;
    public static double hh;

     * @param args the command line arguments
    public static void rotacao(float angle, BufferedImage image) {
        int w = image.getWidth(null);
        int h = image.getHeight(null);
        int nb = image.getSampleModel().getNumBands();
        Raster inputRaster = image.getData();
        int[] pixel = new int[nb * w * h];
        inputRaster.getPixels(0, 0, w, h, pixel);
         BufferedImage i = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

        Vcos = Math.cos(Math.toRadians(angle));
        Vsin = Math.sin(Math.toRadians(angle));
        double x0 = 0.5 * (w - 1); 
        double y0 = 0.5 * (h - 1);
        //hw = w / 2;
        //hh = h / 2;
       // float hw = w/2;
        //float hh = h/2;
        for (int x = 0; x < w; x++) 
            for (int y = 0; y < h; y++) {
              int rgb = image.getRGB(x, y);
                int r = (int) ((rgb & 0x00FF00) >>> 16);
                int g = (int) ((rgb & 0x00FF00) >>> 8);
                int b = (int) (rgb & 0x000000FF);
                double a = x - x0;
                double c = y - y0;
                int xx = (int) (+a * Vcos - c * Vsin + x0);
                int yy = (int) (+a * Vsin + c * Vcos + y0);
                if (xx >= 0 && xx < w && yy >= 0 && yy < h) {
                     int[] pixels = new int[nb * xx * yy];
                    WritableRaster outputRaster = i.getRaster();
                     //Put the raster back on the new image
                    outputRaster.setPixels(0, 0, xx, yy, pixels);
        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ImageIcon(i));
        frame.getContentPane().add(label, BorderLayout.CENTER);

    public static void main(String[] args) throws IOException{

        BufferedImage image = null;
        File file = new File("funny-pictures-evil-sesame-street-1tt.jpg");
        image = ImageIO.read(file);
        System.out.println("Insira o angulo de rotação");
        Scanner in = new Scanner(System.in);
        angle = in.nextFloat();
        //Apresentar imagem Original    
        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ImageIcon(image));
        frame.getContentPane().add(label, BorderLayout.CENTER);
        //Apresentar imagem rotacionada
        rotacao(angle, image);


Agora o problema e que não fazer com que a nova imagem tenha as coordenadas dos pixeis da imagem 1 alteradas para ficar rotacionada.


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.