Java中的Skew或Distort Image对象

是否可能在Java中扭曲或扭曲Image对象? 我’拉出’图像的一面,使它看起来更接近我。 (LIke 3D)。

有什么建议么?

是。 很多方法,但我会从Advanced Imaging API开始 。 它提供了大量先进的成像function。

但只是为了做你正在谈论的变换类型,你可能只需要一个仿射变换 。 此处显示上一个链接的结果。

您也可以使用JavaFX执行此操作。

以下示例在BufferedImage上使用PerspectiveTransform和一些旋转。

它会变成这个图像

Stack Overflow徽标

进入这个

Stack Overflow徽标扭曲

 import javafx.application.Platform; import javafx.embed.swing.JFXPanel; import javafx.embed.swing.SwingFXUtils; import javafx.scene.SnapshotParameters; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.effect.PerspectiveTransform; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.paint.Color; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.concurrent.CountDownLatch; /** * Distorts images using transformations. * 

* Created by Matthias Braun on 2018-09-05. */ public class Distortion { public static void main(String... args) throws IOException { URL imgUrl = new URL("http://sofzh.miximages.com/java/so-logo.png"); BufferedImage img = ImageIO.read(imgUrl); BufferedImage distorted = distortImg(img); File newImgFile = new File(System.getenv("HOME") + "/distorted.png"); System.out.println("Saving to: " + newImgFile); ImageIO.write(distorted, "png", newImgFile); // Since we started a JavaFX thread in distortImg we have to shut it down. Otherwise the JVM won't exit Platform.exit(); } /** * Applies perspective transformations to a copy of this {@code image} and rotates it. *

* Since this method starts a JavaFX thread, it's important to call {@link Platform#exit()} at the end of * your application. Otherwise the thread will prevent the JVM from shutting down. * * @param image the image we want to distort * @return the distorted image */ private static BufferedImage distortImg(BufferedImage image) { // Necessary to initialize the JavaFX platform and to avoid "IllegalStateException: Toolkit not initialized" new JFXPanel(); // This array allows us to get the distorted image out of the runLater closure below final BufferedImage[] imageContainer = new BufferedImage[1]; // We use this latch to await the end of the JavaFX thread. Otherwise this method would finish before // the thread creates the distorted image final CountDownLatch latch = new CountDownLatch(1); // To avoid "IllegalStateException: Not on FX application thread" we start a JavaFX thread Platform.runLater(() -> { int width = image.getWidth(); int height = image.getHeight(); Canvas canvas = new Canvas(width, height); GraphicsContext graphicsContext = canvas.getGraphicsContext2D(); ImageView imageView = new ImageView(SwingFXUtils.toFXImage(image, null)); PerspectiveTransform trans = new PerspectiveTransform(); trans.setUlx(0); trans.setUly(height / 4); trans.setUrx(width); trans.setUry(0); trans.setLrx(width); trans.setLry(height); trans.setLlx(0); trans.setLly(height - height / 2); imageView.setEffect(trans); imageView.setRotate(2); SnapshotParameters params = new SnapshotParameters(); params.setFill(Color.TRANSPARENT); Image newImage = imageView.snapshot(params, null); graphicsContext.drawImage(newImage, 0, 0); imageContainer[0] = SwingFXUtils.fromFXImage(newImage, image); // Work is done, we decrement the latch which we used for awaiting the end of this thread latch.countDown(); }); try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } return imageContainer[0]; } }