首页 技术 正文
技术 2022年11月21日
0 收藏 926 点赞 3,046 浏览 3744 个字

需求:

有一张位置大小的图片,现在需要根据这张原图得到指定尺寸的图片,且得到的图片要符合原先图片的比例,就是在原图的基础上等比例缩放得到图片后,在进行剪裁,这样保证得到的图片是原图的一部分,而不是将原图拉伸或着是压缩到指定的尺寸,这样的图片就会严重的失真,且不协调。

例如:

一张原图为600×400的图片,现在需求如下:

  1. 一张500×300的图片
  2. 一张700×400的图片
  3. 一张400×500的图片

注意:得到的图片不能是原图中的人物、景象有拉伸或压缩的感觉。

思路:

500×300的图片:可以看出宽度和高度都在原图的尺寸之内,但是为了多的得到原图的信息,可先将原图按照一定的比率压缩,压缩的比率min(500/600,300/400),为什么要选择这样的压缩比率呢?因为假如按照宽度比进行压缩,虽然得到的图片的宽度和要求的一致,但是那高度呢?有可能高度压缩之前确实是符合的,也就是大于目标图片的高度,但是枷锁之后,可能出现高度比需求的高度小,导致无法安装、要求截取图片,所以需要比较之后进行压缩,这样不会超出范围。

同理,不管要求的图片大小是否超出原图的大小,或是在原图的大小范围之内,都要先比较,然后再压缩,这样就可以保证得到的图片是放大或缩小到最合适并且包含最多的原图信息,不会变形。

计算压缩比例的核心算法

  1. /*
  2. * 核心算法,计算图片的压缩比
  3. */
  4. int w= buffer.getWidth();
  5. int h=buffer.getHeight();
  6. double ratiox = 1.0d;
  7. double ratioy = 1.0d;
  8. ratiox= w * ratiox / width;
  9. ratioy= h * ratioy / height;
  10. if( ratiox >= 1){
  11. if(ratioy < 1){
  12. ratiox = height * 1.0 / h;
  13. }else{
  14. if(ratiox > ratioy){
  15. ratiox = height * 1.0 / h;
  16. }else{
  17. ratiox = width * 1.0 / w;
  18. }
  19. }
  20. }else{
  21. if(ratioy < 1){
  22. if(ratiox > ratioy){
  23. ratiox = height * 1.0 / h;
  24. }else{
  25. ratiox = width * 1.0 / w;
  26. }
  27. }else{
  28. ratiox = width * 1.0 / w;
  29. }
  30. }
  31. /*
  32. * 对于图片的放大或缩小倍数计算完成,ratiox大于1,则表示放大,否则表示缩小
  33. */

这样,计算完的ratiox就是要压缩的比率。w、h是原图的width和height,而程序中的width和height是要得到图片的width和height。

在生成图片和其他的地方的程序是参考别人的,具体地址给忘了,再次谢过作者,以下是源代码:

  1. import java.awt.geom.AffineTransform;
  2. import java.awt.image.AffineTransformOp;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import javax.imageio.ImageIO;
  6. public class UploadImg {
  7. String fromFileStr;
  8. String saveToFileStr;
  9. String sysimgfile;
  10. int width;
  11. int height;
  12. String suffix;
  13. /**
  14. * @param fromFileStr
  15. *            原始图片完整路径
  16. * @param saveToFileStr
  17. *            缩略图片保存路径
  18. * @param sysimgfilenNow
  19. *            处理后的图片文件名前缀
  20. *
  21. */
  22. public UploadImg(String fromFileStr, String saveToFileStr, String sysimgfile,String suffix,int width,int height) {
  23. this.fromFileStr = fromFileStr;
  24. this.saveToFileStr = saveToFileStr;
  25. this.sysimgfile = sysimgfile;
  26. this.width=width;
  27. this.height=height;
  28. this.suffix=suffix;
  29. }
  30. public boolean createThumbnail() throws Exception {
  31. // fileExtNmae是图片的格式 gif JPG 或png
  32. // String fileExtNmae=””;
  33. File F = new File(fromFileStr);
  34. if (!F.isFile())
  35. throw new Exception(F
  36. + ” is not image file error in CreateThumbnail!”);
  37. File ThF = new File(saveToFileStr, sysimgfile +”.”+suffix);
  38. BufferedImage buffer = ImageIO.read(F);
  39. /*
  40. * 核心算法,计算图片的压缩比
  41. */
  42. int w= buffer.getWidth();
  43. int h=buffer.getHeight();
  44. double ratiox = 1.0d;
  45. double ratioy = 1.0d;
  46. ratiox= w * ratiox / width;
  47. ratioy= h * ratioy / height;
  48. if( ratiox >= 1){
  49. if(ratioy < 1){
  50. ratiox = height * 1.0 / h;
  51. }else{
  52. if(ratiox > ratioy){
  53. ratiox = height * 1.0 / h;
  54. }else{
  55. ratiox = width * 1.0 / w;
  56. }
  57. }
  58. }else{
  59. if(ratioy < 1){
  60. if(ratiox > ratioy){
  61. ratiox = height * 1.0 / h;
  62. }else{
  63. ratiox = width * 1.0 / w;
  64. }
  65. }else{
  66. ratiox = width * 1.0 / w;
  67. }
  68. }
  69. /*
  70. * 对于图片的放大或缩小倍数计算完成,ratiox大于1,则表示放大,否则表示缩小
  71. */
  72. AffineTransformOp op = new AffineTransformOp(AffineTransform
  73. .getScaleInstance(ratiox, ratiox), null);
  74. buffer = op.filter(buffer, null);
  75. //从放大的图像中心截图
  76. buffer = buffer.getSubimage((buffer.getWidth()-width)/2, (buffer.getHeight() – height) / 2, width, height);
  77. try {
  78. ImageIO.write(buffer, suffix, ThF);
  79. } catch (Exception ex) {
  80. throw new Exception(” ImageIo.write error in CreatThum.: “
  81. + ex.getMessage());
  82. }
  83. return (true);
  84. }
  85. public static void main(String[] args) {
  86. UploadImg UI;
  87. boolean ss = false;
  88. try {
  89. UI = new UploadImg(“C:\\Users\\Administrator\\Pictures\\111.jpg”, “C:\\Users\\Administrator\\Pictures\\”, “ps_low2″,”png”,280,280);
  90. ss = UI.createThumbnail();
  91. if (ss) {
  92. System.out.println(“Success”);
  93. } else {
  94. System.out.println(“Error”);
  95. }
  96. } catch (Exception e) {
  97. System.out.print(e.toString());
  98. }
  99. }
  100. }

接下来测试几个例子:

原图1024*520:

Java实现图片的裁剪(包括透明背景)

要求得到尺寸:1000*500

  1. UI = new UploadImg(“F:\\2.jpg”, “F:\\”, “ps”,”jpg”,1000,500);

Java实现图片的裁剪(包括透明背景)

目标尺寸1000*700:

  1. UI = new UploadImg(“F:\\2.jpg”, “F:\\”, “ps”,”jpg”,1000,700);

Java实现图片的裁剪(包括透明背景)

目标尺寸:1100*600:

  1. UI = new UploadImg(“F:\\2.jpg”, “F:\\”, “ps”,”jpg”,1100,600);

Java实现图片的裁剪(包括透明背景)

目标尺寸600*500:

  1. UI = new UploadImg(“F:\\2.jpg”, “F:\\”, “ps”,”jpg”,600,500);

Java实现图片的裁剪(包括透明背景)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918