java---头像剪切功能

实现思路:

在原图(image)上虚构一个长度可以伸缩的正方框,将框出来的图像切割出来作为子图(tempImage1),子图1:1放大或缩小到要显示的大小,对子图重缩放(tempImage2),子图上再切割出图像作为要保存的图像(saveImage);最后显示的图像是tempImage2和覆盖在上面的saveImage。

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package Body;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.Toolkit;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionAdapter;

import java.awt.event.MouseWheelEvent;

import java.awt.event.MouseWheelListener;

import java.awt.image.BufferedImage;

import java.awt.image.RescaleOp;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

/**

*

* @author 心里的潇洒情

*/

public class CutHeadImage extends JFrame{

    private BufferedImage image;//原图

    private BufferedImage tempImage1;//子图

    private BufferedImage tempImage2;//重缩放子图

    private BufferedImage saveImage;

    private final int lenght=700;//显示大小700*700,包括2*padding

    private final int padding=100;

    private final int margin=100;//制作原图留的外边距

    private int nowSize;//虚构方框大小

    private final int minSize,maxSize;//虚构方框大小边界,规范nowSize

    private JPanel toolPane;

    private int orgx,orgy,endx,endy;

    private int lux=0,luy=0;//虚构方框左上角

    public CutHeadImage(Image i){

        setLayout(null);

        setUndecorated(true);

        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);

        setBackground(new java.awt.Color(255, 102, 51));

        setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

        setResizable(false);

        setTitle("调整图片");

        toolPane=new JPanel();

        toolPane.setBackground(Color.gray);

        toolPane.setLayout(new FlowLayout());

        JButton yesButton=new JButton("确定");

        yesButton.addMouseListener(new java.awt.event.MouseAdapter() {

        });

        JButton cancleButton=new JButton("取消");

        cancleButton.addMouseListener(new java.awt.event.MouseAdapter(){

            @Override

            public void mouseClicked(MouseEvent evt){

                dispose();

            }

        });

        toolPane.add(yesButton);

        toolPane.add(cancleButton);

        this.add(toolPane);

        toolPane.setBounds(0,lenght,lenght, 100);

        setSize(lenght, lenght+100);

        int x = (Toolkit.getDefaultToolkit().getScreenSize().width - this.getSize().width)/2;

        int y = (Toolkit.getDefaultToolkit().getScreenSize().height - this.getSize().height)/2;

        setLocation(x, y);

        this.setVisible(true);


        int smaller=Math.min(i.getWidth(null), i.getHeight(null));

        double N=(lenght-2.0*padding)/smaller;

        int width=(int)(i.getWidth(null)*N);

        int height=(int)(i.getHeight(null)*N);

        BufferedImage temp=new BufferedImage(2*margin+width, 2*margin+height,  BufferedImage.TYPE_INT_BGR);

        Graphics2D bGr = temp.createGraphics();

        bGr.setColor(Color.white);

        bGr.fillRect(0, 0, 2*margin+width,2*margin+width);

        bGr.drawImage(i, margin, margin, width, height, null);

        bGr.dispose();

        image=temp;

        nowSize=Math.min(image.getWidth(), image.getHeight());

        maxSize=nowSize;

        minSize=maxSize-200;

        addMouseWheelListener(new MouseWheelListener() {

            public void mouseWheelMoved(MouseWheelEvent evt) {

                int n=evt.getWheelRotation();

                if(nowSize==minSize&&n<0)

                    return ;

                else if(nowSize==maxSize&&n>0)

                    return ;

                nowSize+=n*10;

                lux+=n*5;

                luy+=n*5;

                if(nowSize>=maxSize)

                    nowSize=maxSize;

                else if(nowSize<=minSize)

                    nowSize=minSize;

                if(lux<=0)

                    lux=0;

                else if(lux+nowSize>=image.getWidth())

                    lux=image.getWidth()-nowSize;

                if(luy<=0)

                    luy=0;

                else if(luy+nowSize>=image.getHeight())

                    luy=image.getHeight()-nowSize;


                make();

            }

        });

        addMouseListener(new MouseAdapter(){

            @Override

            public void mousePressed(MouseEvent e) {

                orgx=e.getX();

                orgy=e.getY();

            }

        });

        addMouseMotionListener(new MouseMotionAdapter(){

            @Override

            public void mouseDragged(MouseEvent e){

                endx=e.getX();

                endy=e.getY();

                lux=lux-(endx-orgx);

                luy=luy-(endy-orgy);

                if(lux<=0)

                    lux=0;

                else if(lux+nowSize>=image.getWidth())

                    lux=image.getWidth()-nowSize;

                if(luy<=0)

                    luy=0;

                else if(luy+nowSize>=image.getHeight())

                    luy=image.getHeight()-nowSize;


                make();

            }

        });

    }

    protected void make(){

        BufferedImage temp=new BufferedImage(lenght, lenght ,BufferedImage.TYPE_INT_BGR);

        temp.createGraphics().drawImage(image.getSubimage(lux, luy, nowSize, nowSize), 0, 0, lenght,lenght,null);//切割后放大或缩小

        tempImage1=temp;

        RescaleOp ro = new RescaleOp(0.8f, 0, null); 

        tempImage2 = ro.filter(tempImage1, null);

        Image tempImage3=createImage(lenght,lenght);//margin+lenght+margin,margin为空

        Graphics g=tempImage3.getGraphics();

        g.drawImage(tempImage2, 0, 0, lenght,lenght,null);

        g.setColor(Color.green);

        g.drawRect(padding-1, padding-1, lenght-2*padding+1, lenght-2*padding+1);

        saveImage=tempImage1.getSubimage(padding, padding, lenght-2*padding, lenght-2*padding);

        g.drawImage(saveImage, padding, padding, null);

        CutHeadImage.this.getGraphics().drawImage( tempImage3, 0, 0, null);

    }

    @Override

    public void paint(Graphics g){

        super.paint(g);

        BufferedImage temp=new BufferedImage(lenght, lenght ,BufferedImage.TYPE_INT_BGR);

        temp.createGraphics().drawImage(image.getSubimage(lux, luy, nowSize, nowSize), 0, 0, lenght,lenght,null);  ;

        tempImage1=temp;

        RescaleOp ro = new RescaleOp(0.8f, 0, null); 

        tempImage2 = ro.filter(tempImage1, null); 

        g.drawImage(tempImage2, 0, 0, lenght, lenght, null);

        g.setColor(Color.green);

        g.drawRect(padding-1, padding-1, lenght-2*padding+1, lenght-2*padding+1);//99,99,501,501

        saveImage=tempImage1.getSubimage(padding, padding, lenght-2*padding, lenght-2*padding);

        g.drawImage(saveImage, padding, padding, null);

    }

    public static void main(String args[]){

        new CutHeadImage(new ImageIcon("C:\\Users\\心里的潇洒情\\Desktop\\新建文件夹\\0c3c28bfd9fbe87b952ce334fd430ae.jpg").getImage());

    }

}

你可能感兴趣的:(java---头像剪切功能)