求助 Java实现将本地一个图片重画成倒影并从上到下越来越透明,并保存倒影图片,不需要在程序里显示倒影,只生成就好

解决方案 »

  1.   

    http://www.javaeye.com/topic/714669
    这里有实现代码
      

  2.   

    无聊写了下:
    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);
        }
    }
      

  3.   

    你帮我看一下,谢谢
    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];
            }
            
        }
    }
      

  4.   

    为什么保存下来的图和预览的不一样呢,那么黑
    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);
        }
    }
      

  5.   

    因为半透明图片使用了alpha通道,保存为png图像就可以了,jpg不支持前透明效果。
      

  6.   

     该了好像没有用public void savePic(Image img,Image iamge ){ 
            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(); 
            } 
        }