java - 如何改变图像的透视
问题描述
实际上,我正在开发一个需要更换房间地板的应用程序。应用程序的后端在 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.
解决方案
推荐阅读
- kotlin - 在 Kotlin 中实例化函数类型的最佳实践
- angular - 如何使用 ngFor 显示多维数组?
- shell - 如何使用 sed/awk 在两个模式之间提取文本
- amazon-web-services - 在 AWS 上处理 Outlook .msg 消息
- android - 在android pie上重新启动后无法启动服务
- java - 在特定端口监听 UDP 消息
- javascript - 如何替换本地化字符串的占位符
- c++ - 一个数的sqrt不能通过在线判断
- javascript - 如何在 Angular 应用程序中获取所有控制台错误消息
- java - JavaFX TabPane 标题 OverrunStyle