Java 移動模糊

來源:互聯網
上載者:User

標籤:throws   cal   ati   extend   getwidth   ...   flow   post   blog   

Java 移動模糊代碼

想用Java 寫個移動模糊的效果,無奈本人水平有限,國內也沒找到資源,於是Google到了一個文檔,特地分享出來!

本代碼源自 http://www.jhlabs.com/ip/blurring.html

Java移動模糊演算法:

import java.awt.*;import java.awt.geom.*;import java.awt.image.*;public class MotionBlurOp extends AbstractBufferedImageOp {    private float centreX = 0.5f, centreY = 0.5f;    private float distance=20.0f;                //這裡設定運動距離    private float angle;    private float rotation;    private float zoom;    public MotionBlurOp() {    }    public MotionBlurOp( float distance, float angle, float rotation, float zoom ) {        this.distance = distance;        this.angle = angle;        this.rotation = rotation;        this.zoom = zoom;    }    public void setAngle( float angle ) {        this.angle = angle;    }    public float getAngle() {        return angle;    }    public void setDistance( float distance ) {        this.distance = distance;    }    public float getDistance() {        return distance;    }    public void setRotation( float rotation ) {        this.rotation = rotation;    }    public float getRotation() {        return rotation;    }    public void setZoom( float zoom ) {        this.zoom = zoom;    }    public float getZoom() {        return zoom;    }    public void setCentreX( float centreX ) {        this.centreX = centreX;    }    public float getCentreX() {        return centreX;    }    public void setCentreY( float centreY ) {        this.centreY = centreY;    }    public float getCentreY() {        return centreY;    }    public void setCentre( Point2D centre ) {        this.centreX = (float)centre.getX();        this.centreY = (float)centre.getY();    }    public Point2D getCentre() {        return new Point2D.Float( centreX, centreY );    }    private int log2( int n ) {        int m = 1;        int log2n = 0;        while (m < n) {            m *= 2;            log2n++;        }        return log2n;    }    public BufferedImage filter( BufferedImage src, BufferedImage dst ) {        if ( dst == null )            dst = createCompatibleDestImage( src, null );        BufferedImage tsrc = src;        float cx = (float)src.getWidth() * centreX;        float cy = (float)src.getHeight() * centreY;        float imageRadius = (float)Math.sqrt( cx*cx + cy*cy );        float translateX = (float)(distance * Math.cos( angle ));        float translateY = (float)(distance * -Math.sin( angle ));        float scale = zoom;        float rotate = rotation;        float maxDistance = distance + Math.abs(rotation*imageRadius) + zoom*imageRadius;        int steps = log2((int)maxDistance);        translateX /= maxDistance;        translateY /= maxDistance;        scale /= maxDistance;        rotate /= maxDistance;        if ( steps == 0 ) {            Graphics2D g = dst.createGraphics();            g.drawRenderedImage( src, null );            g.dispose();            return dst;        }        BufferedImage tmp = createCompatibleDestImage( src, null );        for ( int i = 0; i < steps; i++ ) {            Graphics2D g = tmp.createGraphics();            g.drawImage( tsrc, null, null );            g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );            g.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR );            g.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0.5f ) );            g.translate( cx+translateX, cy+translateY );            g.scale( 1.0001+scale, 1.0001+scale );  // The .0001 works round a bug on Windows where drawImage throws an ArrayIndexOutofBoundException            if ( rotation != 0 )                g.rotate( rotate );            g.translate( -cx, -cy );            g.drawImage( dst, null, null );            g.dispose();            BufferedImage ti = dst;            dst = tmp;            tmp = ti;            tsrc = dst;            translateX *= 2;            translateY *= 2;            scale *= 2;            rotate *= 2;        }        return dst;    }    public String toString() {        return "Blur/Motion Blur...";    }}

測試代碼:

import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;/** * Created by zdmein on 2018/1/10. */public class MotionBlurOpTest {    public static void main(String [] args) throws IOException {        BufferedImage sourceImage = ImageIO.read(new File("flower.jpg"));        MotionBlurOp filter=new MotionBlurOp();        BufferedImage destImage=filter.filter(sourceImage,null);        ImageIO.write(destImage, "PNG", new File("MotionBlurOpflower.jpg"));    }}


Java 移動模糊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.