这个是在参考网上一些代码基础上进行编写的,主要是首先实现图片的等比缩放,然后在进行截取,比如一张1024*768的图要截成263*150的,首先是等比缩放后变成263*197的,然后在对这个图片的高度进行截取,变成163*150的
java 代码
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Rectangle;
- import java.awt.RenderingHints;
- import java.awt.geom.AffineTransform;
- import java.awt.image.BufferedImage;
- import java.awt.image.ColorModel;
- import java.awt.image.WritableRaster;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import javax.imageio.ImageIO;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
-
-
-
-
-
-
-
- public class ImageHepler {
-
-
-
-
-
-
-
- private static BufferedImage resize(BufferedImage source, int targetW,
- int targetH) {
-
- int type = source.getType();
- BufferedImage target = null;
- double sx = (double) targetW / source.getWidth();
- double sy = (double) targetH / source.getHeight();
-
-
- if (sx < sy) {
- sx = sy;
- targetW = (int) (sx * source.getWidth());
- } else {
- sy = sx;
- targetH = (int) (sy * source.getHeight());
- }
- if (type == BufferedImage.TYPE_CUSTOM) {
- ColorModel cm = source.getColorModel();
- WritableRaster raster = cm.createCompatibleWritableRaster(targetW,
- targetH);
- boolean alphaPremultiplied = cm.isAlphaPremultiplied();
- target = new BufferedImage(cm, raster, alphaPremultiplied, null);
- } else
- target = new BufferedImage(targetW, targetH, type);
- Graphics2D g = target.createGraphics();
-
- g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
- RenderingHints.VALUE_INTERPOLATION_BICUBIC);
- g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
- g.dispose();
- return target;
- }
-
-
-
-
-
-
-
-
-
-
-
- public static void saveImageAsJpg(String inFilePath, String outFilePath,
- int width, int hight, boolean proportion)throws Exception {
- File file = new File(inFilePath);
- InputStream in = new FileInputStream(file);
- File saveFile = new File(outFilePath);
-
- BufferedImage srcImage = ImageIO.read(in);
- if (width > 0 || hight > 0) {
-
- int sw = srcImage.getWidth();
- int sh = srcImage.getHeight();
-
- if (sw > width && sh > hight) {
- srcImage = resize(srcImage, width, hight);
- } else {
- String fileName = saveFile.getName();
- String formatName = fileName.substring(fileName
- .lastIndexOf('.') + 1);
- ImageIO.write(srcImage, formatName, saveFile);
- return;
- }
- }
-
- int w = srcImage.getWidth();
- int h = srcImage.getHeight();
-
- if (w == width) {
-
- int x = 0;
- int y = h / 2 - hight / 2;
- saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
- }
-
- else if (h == hight) {
-
- int x = w / 2 - width / 2;
- int y = 0;
- saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
- }
- in.close();
- }
-
-
-
-
-
-
-
- private static void saveSubImage(BufferedImage image,
- Rectangle subImageBounds, File subImageFile) throws IOException {
- if (subImageBounds.x < 0 || subImageBounds.y < 0
- || subImageBounds.width - subImageBounds.x > image.getWidth()
- || subImageBounds.height - subImageBounds.y > image.getHeight()) {
- System.out.println("Bad subimage bounds");
- return;
- }
- BufferedImage subImage = image.getSubimage(subImageBounds.x,subImageBounds.y, subImageBounds.width, subImageBounds.height);
- String fileName = subImageFile.getName();
- String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
- ImageIO.write(subImage, formatName, subImageFile);
- }
-
-
- }
分享到:
- 2007-08-02 10:28
- 浏览 4732
- 评论(9)
- 论坛回复 / 浏览 (3 / 14011)
- 查看更多
相关推荐
PC、移动端自适应等比缩放布局方案
这是个工具类,可以实现图片的等比缩放,坑根据图片百分比,或者固定尺寸等进行缩放,还可以给图片增加水印,很好用
JQuery实现等比缩放图片插件,有需要的可以参考使用。
图片压缩、图片等比缩放软件图片压缩、图片等比缩放软件
本例使用php+h5+canvas+jcrop 实现网页上传图片并对原图等比缩放后进行裁剪。实现过程:在HTML中对图片进行上传,在图片文件还未上传至服务器仍停留在form表单时,对该文件进行处理,在此过程中先获取原图的64位base...
但是没有边框,窗体就不能移动,并且不能修改大小,经过修改,窗体可以移动,并且能移动大小,但是发现窗体拉到以后分辨率会降低,经过努力,终于把无边框窗体改成可以拖动,改变大小并且可以等比缩放窗体,...
图片自动等比缩放JS(实用) 直接加载调用,不受IE、TT、火狐影响
产品图与缩略图共用一个图片时,缩略图等比缩放,不变形。
用JAVA获取视频文件中的帧图片并等比缩放,java 获取视频中图片 缩放比例 javacv
图片等比缩放展示,可以在窗口重置的情况下,等比缩放,翻页滚动。主流浏览器都兼容。
图片处理继续 实现等比缩放 大小控制 使用java实现 希望对大家有帮助
Qt点击等比缩放源码
图片显示等比缩放
有以下三个API:将矩形图片截取成正方形,然后缩放成你要的比例 将矩形图缩放成你要的比例
自己改进的简单php等比缩放生成缩略图函数
CSS3的background-size属性,实现响应式式图片等比例缩放。告别了考虑各种兼容性物理像素的烦恼。
里面提供的是php处理图片的两个方法,一个是对图片进行等比缩放,另一个是对图片添加水印。也可以两者结合应用。在相应的web目录下运行代码,就可以在image目录下看到经过等比缩放处理的有“s_”前缀的图片和经过...
js等比缩放图片。 火狐,ie,谷歌都可以用。
jquery图片根据自定义横宽像素,对任何图片进行等比缩放,更换img的src图片测试即可。
上传图片的同时可以生成两个小的缩略图,一个为100X100,一个为300X300。