ImageRotator.java
package img;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageRotator {
private BufferedImage image; // the rasterized image
// on-screen view
private String filename; // name of file
private boolean isOriginUpperLeft = true; // location of origin
private int width, height;
public ImageRotator(String filepath){
filename=filepath;
try {
// try to read from file in working directory
File file = new File(filename);
if (file.isFile()) {
image = ImageIO.read(file);
}
width = image.getWidth(null);
height = image.getHeight(null);
}
catch (IOException e) {
// e.printStackTrace();
throw new RuntimeException("Could not open file: " + filename);
}
}
public ImageRotator(int w,int h){
width = w;
height = h;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// set to TYPE_INT_ARGB to support transparency
filename = width + "-by-" + height;
}
public int height() {
return height;
}
public int width() {
return width;
}
public Color get(int i, int j) {
if (isOriginUpperLeft) return new Color(image.getRGB(i, j));
else return new Color(image.getRGB(i, height - j - 1));
}
/**
* Set the color of pixel (i, j) to c.
*/
public void set(int i, int j, Color c) {
if (c == null) { throw new RuntimeException("can't set Color to null"); }
if (isOriginUpperLeft) image.setRGB(i, j, c.getRGB());
else image.setRGB(i, height - j - 1, c.getRGB());
}
public void save(String name) {
save(new File(name));
}
/**
* Save the picture to a file in a standard image format.
*/
public void save(File file) {
this.filename = file.getName();
String suffix = filename.substring(filename.lastIndexOf('.') + 1);
suffix = suffix.toLowerCase();
if (suffix.equals("jpg") || suffix.equals("png")) {
try { ImageIO.write(image, suffix, file); }
catch (IOException e) { e.printStackTrace(); }
}
else {
System.out.println("Error: filename must end in .jpg or .png");
}
}
}
Rotation.java
package img;
// import java.awt.Color;
public class Rotation {
public static void rotate(ImageRotator pic1,String rangle,String target){
int width = pic1.width();
int height = pic1.height();
double angle = Math.toRadians(Double.parseDouble(rangle));
double sin = Math.sin(angle);
double cos = Math.cos(angle);
double x0 = 0.5 * (width - 1); // point to rotate about
double y0 = 0.5 * (height - 1); // center of image
ImageRotator pic2=new ImageRotator(width,height);
// rotation
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
double a = x - x0;
double b = y - y0;
int xx = (int) (+a * cos - b * sin + x0);
int yy = (int) (+a * sin + b * cos + y0);
// plot pixel (x, y) the same color as (xx, yy) if it's in bounds
if (xx >= 0 && xx < width && yy >= 0 && yy < height) {
pic2.set(x, y, pic1.get(xx, yy));
}
}
}
pic2.save(target);
}
public static void main(String[] args) {
args=new String[]{"f.jpg"};
ImageRotator pic1=new ImageRotator(args[0]);
for(int i=10;i<360;i+=10){
rotate(pic1, i+"", i+".jpg");
}
System.out.println("Done");
}
}
This comment has been removed by the author.
ReplyDelete