Java motion Blur

Source: Internet
Author: User

Java motion Blur Code

Want to use Java write a motion blur effect, helpless I level limited, domestic also did not find resources, so Google to a document, specially share out!

This code originates from http://www.jhlabs.com/ip/blurring.html

Java motion blur algorithm:

Import java.awt.*;import java.awt.geom.*;import java.awt.image.*;p ublic class Motionblurop extends    Abstractbufferedimageop {private float CentreX = 0.5f, Centrey = 0.5f;                private float distance=20.0f;    Here set the movement distance private float angle;    private float rotation;    private float zoom; Public Motionblurop () {} public Motionblurop (float distance, float angle, float rotation, float zoom) {th        Is.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 = Crea        Tecompatibledestimage (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;    The public String toString () {return "blur/motion Blur ..."; }}

Test code:

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 motion Blur

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.