高分悬赏了,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 关于#python#的问题:求帮写python代码
- ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
- ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
- ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
- ¥15 perl MISA分析p3_in脚本出错
- ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
- ¥15 ubuntu虚拟机打包apk错误
- ¥199 rust编程架构设计的方案 有偿
- ¥15 回答4f系统的像差计算
- ¥15 java如何提取出pdf里的文字?