日期:2014-05-20  浏览次数:20764 次

求高手帮写个程序
The file should run as follows,
java ImageMagnifier image1.bmp image2.bmp
Here, a bmp image file called “image1.bmp” under the same directory will be read into memory. Your program should output the resized-image into another bmp file “image2.bmp”. The size of the new file is twice as large as the old file, e.g., if “image1.bmp” has 200*400 pixels, then

“image2.bmp” should have “400*800”.
You may consider to use “ImageIO” class for reading and outputting of bmp images.

就是写个放大2倍图片的程序 ImageIO

图片 java bmp

------解决方案--------------------

/**
 * 缩放图像
 * 
 * @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();
}
}