放大2倍图片的程序 求高手帮忙修改下 可以运行 在线等
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class OperateImage{
public void enlargementImageByRatio(String srcImagePath,String toImagePath,int widthRatio,int heightRatio) throws IOException{
try{
File file = new File(srcImagePath);
BufferedImage src = javax.imageio.ImageIO.read(file);
int width = src.getWidth();
int height = src.getHeight();
BufferedImage tag = new BufferedImage(width * 2, height * 2, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, width * 2, height * 2, null);
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args)throws Exception{
OperateImage imageObj = new OperateImage();
ImageIO.write(imageObj, "bmp", new File());
}}
java bmp string
------解决方案--------------------
/**
* 缩放图像
*
* @param srcImageFile
* 源图像文件地址
* @param result
* 缩放后的图像地址
* @param scale
* 缩放比例
* @param flag
* 缩放选择:true 放大; false 缩小;
*/
public static void scale(String srcImageFile, String result, int scale,
boolean flag) {
try {
BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
int width = src.getWidth(); // 得到源图宽
int height = src.getHeight(); // 得到源图长
if (flag) {
// 放大
width = width * scale;
height = height * scale;
} else {
// 缩小
width = width / scale;
height = height / scale;
}
Image image = src.getScaledInstance(width, height,
Image.SCALE_DEFAULT);
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
ImageIO.write(tag, "JPEG", new File(result));// 输出到文件流
} catch (IOException e) {
e.printStackTrace();
}
}