Image Processing 台形変形への道 - へたれなので。
2008/06/02
java
画像処理
Javaです。
画像を変形させて台形の形にしたくて試行錯誤してます。
そんなのがりがり書けよといわれそうですけど...
前回、JAI( Java Advanced Imaging)を使ってチャレンジしてたのですが、なんかいまいちでした。ぎざぎざが目立ったので。なんかいい方法がないのかしら。
プログラマメモ2: PerspectiveTransformを使ってみる - 台形変形に向けて
灯台もとくらしで、以前使った、Filters.jarに含まれていたのでそれを使ってみました。
ぎざぎざがあまりでない感じがしますね。
以下コード。参考にならないけど...
package pers;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.jhlabs.image.PerspectiveFilter;
public class Test5 {
static class Panel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
p(g2d);
}
void p(Graphics2D g2d) {
BufferedImage image;
try {
image = c();
g2d.drawImage(image, 30, 30, null);
g2d.drawImage(a(image), image.getWidth() + 30, 30, null);
} catch (IOException e) {
e.printStackTrace();
}
}
}
static BufferedImage c() throws IOException {
BufferedImage org = ImageIO.read(Test5.class
.getResourceAsStream("zzz.png"));
BufferedImage image = new BufferedImage(org.getWidth(),
org.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g2d = (Graphics2D) image.getGraphics();
g2d.drawImage(org, 0, 0, null);
g2d.dispose();
return image;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Panel());
frame.setSize(1000, 600);
frame.setVisible(true);
}
public static BufferedImage a(BufferedImage image) {
int w = image.getWidth();
int h = image.getHeight();
PerspectiveFilter perspectiveFilter = new PerspectiveFilter();
perspectiveFilter.setCorners(0, 0, w, 100, w, h - 100, 0, h);
BufferedImage image2 = perspectiveFilter.filter(image, null);
return image2;
}
}
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.jhlabs.image.PerspectiveFilter;
public class Test5 {
static class Panel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
p(g2d);
}
void p(Graphics2D g2d) {
BufferedImage image;
try {
image = c();
g2d.drawImage(image, 30, 30, null);
g2d.drawImage(a(image), image.getWidth() + 30, 30, null);
} catch (IOException e) {
e.printStackTrace();
}
}
}
static BufferedImage c() throws IOException {
BufferedImage org = ImageIO.read(Test5.class
.getResourceAsStream("zzz.png"));
BufferedImage image = new BufferedImage(org.getWidth(),
org.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g2d = (Graphics2D) image.getGraphics();
g2d.drawImage(org, 0, 0, null);
g2d.dispose();
return image;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Panel());
frame.setSize(1000, 600);
frame.setVisible(true);
}
public static BufferedImage a(BufferedImage image) {
int w = image.getWidth();
int h = image.getHeight();
PerspectiveFilter perspectiveFilter = new PerspectiveFilter();
perspectiveFilter.setCorners(0, 0, w, 100, w, h - 100, 0, h);
BufferedImage image2 = perspectiveFilter.filter(image, null);
return image2;
}
}
: