高分悬赏了,Java语言怎么将图片的分辨率提高一些,怎么把小图片还原成大图片,大图片找不到了
2条回答 默认 最新
- Z_墨脱 2020-04-16 10:16关注
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;import javax.imageio.ImageIO;
public class Temp01{
private Temp01(){ } private void imageOp(InputStream inFile, String outFilePath, int width, int height){ Image image = null; try { image = ImageIO.read(inFile); } catch (IOException e) { System.out.println("file path error..."); } int originalImageWidth = image.getWidth(null); int originalImageHeight = image.getHeight(null); BufferedImage originalImage = new BufferedImage( originalImageWidth, originalImageHeight, BufferedImage.TYPE_3BYTE_BGR); Graphics2D g2d = originalImage.createGraphics(); g2d.drawImage(image, 0, 0, null); BufferedImage changedImage = new BufferedImage( width, height, BufferedImage.TYPE_3BYTE_BGR); double widthBo = (double)width/originalImageWidth; double heightBo = (double)width/originalImageHeight; AffineTransform transform = new AffineTransform(); transform.setToScale(widthBo, heightBo); AffineTransformOp ato = new AffineTransformOp(transform, null); ato.filter(originalImage, changedImage); File fo = new File(outFilePath); //将要转换出的小图文件 try { ImageIO.write(changedImage, "jpeg", fo); } catch (Exception e) { e.printStackTrace(); } } private void imageOp(String inFilePath, String outFilePath, int width, int height){ File tempFile = new File(inFilePath); Image image = null; try { image = ImageIO.read(tempFile); } catch (IOException e) { System.out.println("file path error..."); } int originalImageWidth = image.getWidth(null); int originalImageHeight = image.getHeight(null); BufferedImage originalImage = new BufferedImage( originalImageWidth, originalImageHeight, BufferedImage.TYPE_3BYTE_BGR); Graphics2D g2d = originalImage.createGraphics(); g2d.drawImage(image, 0, 0, null); BufferedImage changedImage = new BufferedImage( width, height, BufferedImage.TYPE_3BYTE_BGR); double widthBo = (double)width/originalImageWidth; double heightBo = (double)width/originalImageHeight; AffineTransform transform = new AffineTransform(); transform.setToScale(widthBo, heightBo); AffineTransformOp ato = new AffineTransformOp(transform, null); ato.filter(originalImage, changedImage); File fo = new File(outFilePath); //将要转换出的小图文件 try { ImageIO.write(changedImage, "jpeg", fo); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws FileNotFoundException { Temp01 t1 = new Temp01(); t1.imageOp("C:/p02.jpg", "C:/p03.jpg", 400, 300); InputStream in = new FileInputStream(new File("C:/p02.jpg")); t1.imageOp(in, "C:/p04.jpg", 400, 300); }
}
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 逻辑谓词和消解原理的运用
- ¥15 请求分析基于spring boot+vue的前后端分离的项目
- ¥15 三菱伺服电机按启动按钮有使能但不动作
- ¥15 js,页面2返回页面1时定位进入的设备
- ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
- ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
- ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
- ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
- ¥20 腾讯企业邮箱邮件可以恢复么
- ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?