怎样分割图片?? 譬如我要一幅图片的 从(100,100)开始  ,长为100,宽为100的范围,生成一幅小图片!???

解决方案 »

  1.   

    一个拼图游戏,里面有你要用的方法,你自己找吧。用法:java RectJigsaw <图片文件名,包括路径>
    比如:java RectJigsaw "C:\WINNT\Web\Wallpaper\Windows 2000.jpg"图片文件只能是gif或jpg// RectJigsaw.java
    // author: unagainimport sun.awt.image.ToolkitImage;import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import java.awt.event.*;import java.util.*;import javax.swing.*;
    import javax.swing.border.*;public class RectJigsaw 
    extends JPanel{    
        final static int rowCount = 7;
        final static int colCount = 8;
        final static int HCTRL = 40;
        final static int HMARGIN = 50;
        final static int VMARGIN = 50;
        
        private BufferedImage bi;    static int imgWidth = 0, imgHeight = 0;
        static int cellWidth = 0, cellHeight = 0;
        
        static Vector<BufferedImage> images = new Vector<BufferedImage>();
        static Vector<Integer> imgOrder = new Vector<Integer>();    public RectJigsaw(String fn) {
            super();
            setBackground(Color.BLACK);
            setBorder(new BevelBorder(BevelBorder.LOWERED));        initCells(fn);
        }    private void initCells(String fn) {
            final Image img = Toolkit.getDefaultToolkit().getImage(fn);
            try {
                MediaTracker tracker = new MediaTracker(this);
                tracker.addImage(img, 0);
                tracker.waitForID(0);
            } catch (Exception e) {}        bi = ((ToolkitImage) img).getBufferedImage();                bi = roundImgBounds(bi);        Graphics2D g2 = (Graphics2D) bi.getGraphics();
            
            int x = 0;
            int y = 0;        cellWidth = bi.getWidth() / colCount;
            cellHeight = bi.getHeight() / rowCount;        g2.setColor(Color.WHITE);
            int k=0;        for (int row=0; row<rowCount; row++) {
                for (int col=0; col<colCount; col++) {
                    g2.draw3DRect(
                        x + cellWidth * col, 
                        y + cellHeight * row, 
                        cellWidth - 2, cellHeight - 2, true);                imgOrder.add(k++);                images.add(
                        bi.getSubimage(
                            x + cellWidth * col, 
                            y + cellHeight * row, 
                            cellWidth, cellHeight));
                }                    
            }        
            g2.setColor(Color.BLACK);
            for (int col=0; col < colCount; col++) {
                g2.drawLine(
                    x + cellWidth*(col+1) - 1, 
                    y, 
                    x + cellWidth *(col+1) - 1, 
                    y + bi.getHeight() - 1);
            }        for (int row=0; row < rowCount; row++) {
                g2.drawLine(
                    x, 
                    y + cellHeight*(row+1) - 1, 
                    x + bi.getWidth() - 1, 
                    y + cellHeight*(row+1) - 1);
            }    }
        public void paintComponent(Graphics g){
            super.paintComponent(g);        Graphics2D g2 = (Graphics2D) g;
            int orgX = (getWidth() - imgWidth) / 2;
            int orgY = (getHeight() - imgHeight) / 2;        int i = -1;        for (Integer index : imgOrder) {
                i ++;            int row = i / colCount;
                int col = i % colCount;            BufferedImage img = images.get(index.intValue());            g2.drawImage(
                    img,
                    orgX + col * cellWidth, 
                    orgY + row * cellHeight, 
                    this);
            }
        }    private BufferedImage roundImgBounds(BufferedImage bi) {        int height = bi.getHeight();
            int width = bi.getWidth();        float cellHeight = Math.round(height / 1.0 / rowCount);
            float cellWidth = Math.round(width / 1.0 / colCount);        AffineTransform at = new AffineTransform();        imgWidth = (int)cellWidth * colCount;
            imgHeight = (int)cellHeight * rowCount;        at.scale(imgWidth / width, 
                imgHeight / height);        AffineTransformOp biop = 
                new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);        BufferedImage newbi = biop.filter(bi, null);
            
            return newbi;
        }    public static void main(String[] args) {
            try {
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            } catch (Exception e) {
            }        JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        final JPanel pane = new RectJigsaw(args[0]);
            JPanel ctrlPane = new JPanel();        ctrlPane.setBorder(new LineBorder(Color.DARK_GRAY));
            ctrlPane.add(
                new JButton(
                    new AbstractAction("Restore"){
                        public void actionPerformed(ActionEvent e) {                        
                            Collections.sort(imgOrder);
                            pane.repaint();
                        }
            }));
            ctrlPane.add(
                new JButton(
                    new AbstractAction("Shuffle") {
                        public void actionPerformed(ActionEvent e) {                        Collections.shuffle(imgOrder);
                            SwingUtilities.invokeLater(
                                new Runnable() {
                                    public void run(){
                                        pane.repaint();
                                    }
                            });                                            }
            }));        frame.getContentPane().add(pane, BorderLayout.CENTER);
            frame.getContentPane().add(ctrlPane, BorderLayout.PAGE_END);        ctrlPane.setPreferredSize(
                new Dimension(imgWidth + HMARGIN * 2, HCTRL));
            frame.setPreferredSize(
                new Dimension(imgWidth + HMARGIN * 2, imgHeight + VMARGIN * 2 + HCTRL));        frame.pack();
            frame.setVisible(true);
        }
    }
      

  2.   

    import javax.swing.*;
    import java.io.*;
    import java.awt.image.BufferedImage;
    import javax.imageio.*;import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;import java.awt.*;public class CutImage{
        //5个参数分别是原图,其实位置的x坐标,y坐标,结束位置的x坐标,y坐标,楼主可以根据需要改下
        public static BufferedImage cut(BufferedImage image,int leftTopX,int leftTopY,int rightBottomX,int rightBottomY){
             int length = rightBottomX-leftTopX;
    int width = rightBottomY-leftTopX;
    BufferedImage ima = new BufferedImage(length,width,BufferedImage.TYPE_INT_RGB);
    Graphics g = ima.getGraphics();
    g.drawImage(image,-leftTopX,-leftTopY,null);
    return ima;
    }
    public static void main(String args[]) throws IOException{
    File file = new File(".\\33.jpg");
    BufferedImage image = ImageIO.read(file);
    BufferedImage ima;
    ima = CutImage.cut(image,40,30,70,80);
    ImageIO.write(ima,"jpg",new File(".\\s.jpg"));
    }
    }
      

  3.   

    楼上的.为什么只可以转化为jpg,gif或其他文件类型怎么不可以啊?
      

  4.   

    java2D包好像只支持gif和jpg。
    你需要打开什么类型的文件?你可以找一下sun的JAI软件包,可以处理各种图片。
      

  5.   

    嗯,刚看了API Spec,是可以处理png
      

  6.   

    sun8408() :
    g.drawImage(image,-leftTopX,-leftTopY,null);
    中的坐标用负数表示 怎样理解??  为什么用正数就不可以呢?