在用Java做一个基本GUI的项目时,有个需求是将一幅小图加到另一幅大图的中心,我一点头绪都没有,哪们高人能给点线索或者代码之类的,急盼

解决方案 »

  1.   

    1. PixelGrabber(Image img, int x, int y, int w, int h, int[] pix, int off, int scansize) 
              创建一个 PixelGrabber 对象,以从指定图像将像素矩形部分 (x, y, w, h) 抓取到给定的数组中。
    使用PixelGrabber把图像的像素数据抓取保存取数组pix中.2. 使用抓取到的像素数据数组, 把小图的像素数据放到大图的像素数据里, 如直接替换掉, 或者用一些图像算法等. 你的这个要求是需要计算出小图在大图的像素数组中的中心位置, 相信这不难.3. 通过2得到合成后的像素数组, 使用MemoryImageSource和刚才生成的像素数据, 生成一个新的图像.
    Image img = createImage(new MemoryImageSource(w, h, pix, 0, w));下面是jdk自带的一个例子:
    此类是 ImageProducer 接口的一个实现,该接口使用一个数组为 Image 生成像素值。下面的示例计算了一幅 100x100 的图像,表示沿 X 轴从黑色渐变到蓝色,沿 Y 轴从黑色渐变到红色: 
            int w = 100;
            int h = 100;
            int pix[] = new int[w * h];
            int index = 0;
            for (int y = 0; y < h; y++) {
                int red = (y * 255) / (h - 1);
                for (int x = 0; x < w; x++) {
                    int blue = (x * 255) / (w - 1);
                    pix[index++] = (255 << 24) | (red << 16) | blue;
                }
            }
            Image img = createImage(new MemoryImageSource(w, h, pix, 0, w));
      

  2.   


    import java.io.*; 
    import java.awt.*; 
    import java.awt.image.*; 
    import javax.swing.*; public class ImagePrint extends JComponent 

        public ImagePrint() 
        { 
            init(); 
        }     public void init() 
        { 
            try 
            { 
                ImageIcon imageIconlogo = new javax.swing.ImageIcon(Toolkit. 
                        getDefaultToolkit().createImage(ImagePrint. 
                        class.getResource("/images/logo.png"))); 
                BufferedImage blogo = new java.awt.image.BufferedImage( 
                        imageIconlogo.getIconWidth(), imageIconlogo.getIconHeight(), 
                        java.awt.image.BufferedImage.TYPE_INT_ARGB); 
                Graphics g1 = blogo.getGraphics(); 
                g1.drawImage(imageIconlogo.getImage(), 0, 0, this); 
                
                ImageIcon imageIconphoto = new javax.swing.ImageIcon(Toolkit. 
                        getDefaultToolkit().createImage(ImagePrint. 
                        class.getResource("/images/bgpic.jpg"))); 
                BufferedImage bphoto = new java.awt.image.BufferedImage( 
                        imageIconphoto.getIconWidth(), 
                        imageIconphoto.getIconHeight(), 
                        java.awt.image.BufferedImage.TYPE_3BYTE_BGR); 
                Graphics g2 = bphoto.getGraphics(); 
                g2.drawImage(imageIconphoto.getImage(), 0, 0, this); 
                
                //重合并保存图片 logo右下角和photo右下角的距离2,2 
                printAndAave(blogo, bphoto, 2, 2); 
            } 
            catch (Exception e) 
            { 
                e.printStackTrace(); 
            } 
        }     /** 
        * 进行图片透明印章处理并保存 
        * @param bprint BufferedImage logo图 
        * @param bibackground BufferedImage photo图 
        * @param x int logo右下角和photo右下角x轴方向距离 
        * @param y int logo右下角和photo右下角y轴方向距离 
        */ 
        public void printAndAave(BufferedImage blogo, BufferedImage bphoto, 
                                int x, int y) 
        { 
            float alpha = 0.5f; // 透明度 
            Graphics2D g2d = bphoto.createGraphics(); // 设置透明度 
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha)); // 开始 // 
            g2d.drawImage(blogo, bphoto.getWidth() - blogo.getWidth() - x, 
                          bphoto.getHeight() - blogo.getHeight() - y, null); 
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); // 结束 
            FileOutputStream out = null; 
            try 
            { 
                out = new FileOutputStream("image//logonphoto.jpg"); 
                com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image. 
                        codec.jpeg.JPEGCodec.createJPEGEncoder(out); 
                encoder.encode(bphoto); 
                out.close(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
        }     public static void main(String args[]) 
        { 
            ImagePrint a = new ImagePrint(); 
        } 
    }