求助 Java实现将本地一个图片重画成倒影并从上到下越来越透明,并保存倒影图片,不需要在程序里显示倒影,只生成就好
解决方案 »
- Spring2.5,Jpa,Flex(使用LCDS通信,部署在Tomcat6.0),出现离奇错误,求高人指点
- 每十秒读一次数据库
- servlet中使用 ehcache缓存
- JavaScript改变背景图片特效如何制作?
- WebRowSet批量读取数据和批量保存数据 高手帮看看
- Struts的base标签有何作用,我怎么感觉用与不用没什么分别呢?
- sturts中<htmL:text>的value的问题
- flex能不能调用windows api
- weblogic服务器上怎样把一个ear包中的war包发布到根目录上?
- 每半年产生几亿条数据,你会如何设计架构?
- severlet问题
- 请教hibernate一对多的问题.
这里有实现代码
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;public class Test extends JPanel {
private Image img;
private Image reflectImg;
private int w;
private int h;
public Test(String imageName) {
img = new ImageIcon(imageName).getImage();
// 从图片中提取像素数据
w = img.getWidth(null);
h = img.getHeight(null);
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (Exception ex) {
}
for (int y = 0; y < h; ++y) {
int a = (int) (y * 100.0 / h * 2.55); // 每一行的透明度0~255
for (int x = 0; x < w; ++x) {
int rgb = pixels[y * w + x] & 0xFFFFFF;
pixels[y * w + x] = (a << 24) | rgb;
}
}
// 翻转图像
for (int y = 0; y < h / 2; ++y) {
for (int x = 0; x < w; ++x) {
int temp = pixels[y * w + x];
pixels[y * w + x] = pixels[(h - 1 - y) * w + x];
pixels[(h - 1 - y) * w + x] = temp;
}
}
// 使用像素数据创建镜面图像
MemoryImageSource source = new MemoryImageSource(w, h, pixels, 0, w);
reflectImg = createImage(source);
pixels = null;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(img, 0, 0, this);
g2d.drawImage(reflectImg, 0, h, this);
}
public static void main(String[] args) {
String imgName = "a.jpg"; // 图片名字,从命令行参数传入
if (args.length == 1) {
imgName = args[0];
}
JPanel panel = new Test(imgName);
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 800);
frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.swing.*;public class ReflectionApplet {
private Image img;
private Image reflectImg;
private int w;
private int h;
Graphics waveGraphics; // 倒影的Graphics对象
public ReflectionApplet(String imageName) {
img = new ImageIcon(imageName).getImage();
// 从图片中提取像素数据
w = img.getWidth(null);
h = img.getHeight(null);
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (Exception ex) {
}
for (int y = 0; y < h; ++y) {
int a = (int) (y * 100.0 / h * 2.55); // 每一行的透明度0~255
for (int x = 0; x < w; ++x) {
int rgb = pixels[y * w + x] & 0xFFFFFF;
pixels[y * w + x] = (a << 24) | rgb;
}
}
// 翻转图像
for (int y = 0; y < h / 2; ++y) {
for (int x = 0; x < w; ++x) {
int temp = pixels[y * w + x];
pixels[y * w + x] = pixels[(h - 1 - y) * w + x];
pixels[(h - 1 - y) * w + x] = temp;
}
}
// 使用像素数据创建镜面图像
MemoryImageSource source = new MemoryImageSource(w, h, pixels, 0, w);
// reflectImg = MemoryImageSource.createImage(source);
waveGraphics = reflectImg.getGraphics(); // 得到波浪效果的Graphics实例
paintComponent(waveGraphics);
pixels = null;
}
protected void paintComponent(Graphics g) {
// super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(img, 0, 0, null);
g2d.drawImage(reflectImg, 0, h, null);
File file = new File("file:///e:/223.jpg"); Font font = new Font("Serif", Font.BOLD, 10);
BufferedImage bi = new BufferedImage(w,h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)bi.getGraphics();
g2.drawImage(reflectImg,0, 0, null);
try {
ImageIO.write(bi, "jpg", file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
String imgName = "file:///e:/2.jpg"; // 图片名字,从命令行参数传入
if (args.length == 1) {
imgName = args[0];
}
}
}
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.swing.*;public class Test extends JPanel {
private Image img;
private Image reflectImg;
private int w;
private int h;
String imageName;
public Test(String imageName) {
img = new ImageIcon(imageName).getImage();
this.imageName = imageName;
// 从图片中提取像素数据
w = img.getWidth(null);
h = img.getHeight(null);
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (Exception ex) {
}
for (int y = 0; y < h; ++y) {
int a = (int) (y * 100.0 / h * 1.55); // 每一行的透明度0~255
for (int x = 0; x < w; ++x) {
int rgb = pixels[y * w + x] & 0xFFFFFF;
pixels[y * w + x] = (a << 24) | rgb;
}
}
// 翻转图像
for (int y = 0; y < h / 2; ++y) {
for (int x = 0; x < w; ++x) {
int temp = pixels[y * w + x];
pixels[y * w + x] = pixels[(h - 1 - y) * w + x];
pixels[(h - 1 - y) * w + x] = temp;
}
}
// 使用像素数据创建镜面图像
MemoryImageSource source = new MemoryImageSource(w, h, pixels, 0, w);
reflectImg = createImage(source);
savePic(reflectImg);
pixels = null;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(reflectImg, 0, h, this);
g2d.drawImage(img, 0, 0, this);
}
public void savePic(Image iamge ){
int w = iamge.getWidth(this);
int h = iamge.getHeight(this);//首先创建一个BufferedImage变量,因为ImageIO写图片用到了BufferedImage变量。
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);//再创建一个Graphics变量,用来画出来要保持的图片,及上面传递过来的Image变量
Graphics g = bi.getGraphics();
try {
g.drawImage(iamge, 0, 0, null);//将BufferedImage变量写入文件中。
ImageIO.write(bi,"png",new File("d:/car_1.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
String imgName = "e:\\car2.jpg"; // 图片名字,从命令行参数传入
if (args.length == 1) {
imgName = args[0];
}
JPanel panel = new Test(imgName);
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setVisible(true);
}
}
int w = iamge.getWidth(this);
int h = iamge.getHeight(this);
BufferedImage bi = new BufferedImage(w, h*2, BufferedImage.TYPE_3BYTE_BGR);
Graphics g = bi.getGraphics();
try {
g.drawImage(img, 0, 0, null);
g.drawImage(iamge, 0, h, null); ImageIO.write(bi,"png",new File("d:/car6_1.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}