首页 > 解决方案 > 如何改变图像的透视

问题描述

实际上,我正在开发一个需要更换房间地板的应用程序。应用程序的后端在 java 中。假设这是房间。

需要更换楼层的房间

拆除地板后的房间

需要铺设的地毯

执行代码后的结果

我已经使用 Photoshop 之类的软件移除了房间的地板。现在我需要为该图像添加新的地毯地板。地毯地板必须是动态的,用户选择任何地毯并在渲染后替换图像。

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class FloorChange {

    public static void main(String[] args) {
        int width = 1920; // width of the image
        int height = 1080; // height of the image

        // For storing image in RAM
        BufferedImage image = null;
        BufferedImage background = null;
        BufferedImage finalImage = null;

        // READ IMAGE
        try {
            File input_file = new File("room.png"); // image file path
            File floor_tile = new File("milkyway.jpg"); // image file path

            FloorChange floorChange = new FloorChange();
            image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            background = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);


            System.out.println("Reading complete.");
        } catch (IOException e) {
            System.out.println("Error: " + e);
        }

        try {
            Graphics2D g = finalImage.createGraphics();

            g.drawImage(background, 0, 0, null);
            g.drawImage(image, 0, 0, null);
            File output_file = new File("floor1.png");

            ImageIO.write(finalImage, "png", output_file);
            System.out.println("Writing complete.");
        } catch (IOException e) {
            System.out.println("Error: " + e);
        }
    }

    private BufferedImage resizeImage(BufferedImage originalImage, int type) {
        int IMG_WIDTH = 1920;
        int IMG_CLAHEIGHT = 1080;
        BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_CLAHEIGHT, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_CLAHEIGHT, null);
        g.dispose();
        return resizedImage;
    }

I have replaced the floor but the image was stretched and if I repeat the image it gives lines between each carpet. And also the perspective of the imag e is not proper.

标签: javaimage-processing

解决方案


推荐阅读