Index: ssts-tousse/src/main/java/com/forgon/disinfectsystem/tousse/imagefilemanager/service/ImageFileManagerImpl.java =================================================================== diff -u -r12331 -r14968 --- ssts-tousse/src/main/java/com/forgon/disinfectsystem/tousse/imagefilemanager/service/ImageFileManagerImpl.java (.../ImageFileManagerImpl.java) (revision 12331) +++ ssts-tousse/src/main/java/com/forgon/disinfectsystem/tousse/imagefilemanager/service/ImageFileManagerImpl.java (.../ImageFileManagerImpl.java) (revision 14968) @@ -9,6 +9,7 @@ import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -25,9 +26,12 @@ import org.hibernate.Session; import com.forgon.disinfectsystem.basedatamanager.supplyroomconfig.service.SupplyRoomConfigManager; +import com.forgon.disinfectsystem.common.geom.SizeAndPosition; import com.forgon.disinfectsystem.entity.basedatamanager.imagefilemanager.ImageFile; import com.forgon.disinfectsystem.entity.basedatamanager.supplyroomconfig.SupplyRoomConfig; import com.forgon.tools.hibernate.ObjectDao; +import com.forgon.util.FileUtils; +import com.sun.image.codec.jpeg.ImageFormatException; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; @@ -289,37 +293,23 @@ File imageFile = new File(file, UUIDImageName); byte[] imageData = imageFileObj.getImage(); FileOutputStream fos = null; - FileOutputStream out = null; try { //保存原图 fos = new FileOutputStream(imageFile); fos.write(imageData); //生成缩略图 - BufferedImage bi = abbreviatedZoom(imageData); - out = new FileOutputStream(new File(file2,UUIDthumbnailImageName)); - JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); - encoder.encode(bi); - out.close(); + createThumbnailImage(imageFile, imageData, new File(file2,UUIDthumbnailImageName)); +// BufferedImage bi = abbreviatedZoom(imageData); +// out = new FileOutputStream(new File(file2,UUIDthumbnailImageName)); +// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); +// encoder.encode(bi); +// out.close(); } catch (Exception e) { e.printStackTrace(); - }finally{ - if(fos!=null){ - try { - fos.close(); - } catch (IOException e) { - e.printStackTrace(); - }finally{ - if(out!=null){ - try { - out.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } + } finally { + FileUtils.closeFileOutputStream(fos); } imageFileObj.setImagePath(originalImagePath); imageFileObj.setThumbnailImagePath(thumbnailImagePath); @@ -344,8 +334,7 @@ } } - @Override - public BufferedImage abbreviatedZoom(byte[] imageData) { + public BufferedImage abbreviatedZoom_old(byte[] imageData) { ImageIcon imgIcon = new ImageIcon(imageData); Image img = imgIcon.getImage(); int height = 600; @@ -359,7 +348,112 @@ g.dispose(); return buffImg; } + + private void createThumbnailImage(File originalFile, + byte[] originalImageData, File thumbnailFile) { + ImageIcon originalImageIcon = new ImageIcon(originalImageData); + Image originalImage = originalImageIcon.getImage(); + int boundaryWidth = 800; + int boundaryHeight = 600; + int originalWidth = originalImageIcon.getIconWidth(); + int originalHeight = originalImageIcon.getIconHeight(); + + FileOutputStream thumbnailFOS = null; + if (whetherNeedToZoomByTotalPixles(originalWidth, originalHeight, + boundaryWidth * boundaryHeight)) { + SizeAndPosition sizeAndPosition = new SizeAndPosition(); + calculateZoomSize(originalWidth, originalHeight, boundaryWidth, + boundaryHeight, sizeAndPosition); + + int scaledWidth = sizeAndPosition.scaledWidth; + int scaledHeight = sizeAndPosition.scaledHeight; + int x = sizeAndPosition.x; + int y = sizeAndPosition.y; + // 生成缩略图 + BufferedImage thumbnailBufferedImage = new BufferedImage( + scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB); + Graphics2D g = thumbnailBufferedImage.createGraphics(); + g.setColor(Color.WHITE); + g.fillRect(0, 0, scaledWidth, scaledHeight); + g.drawImage(originalImage, 0, 0, scaledWidth, + scaledHeight, null); + g.dispose(); + try { + thumbnailFOS = new FileOutputStream(thumbnailFile); + JPEGImageEncoder encoder = JPEGCodec + .createJPEGEncoder(thumbnailFOS); + encoder.encode(thumbnailBufferedImage); + } catch (FileNotFoundException e) { + e.printStackTrace(); + throw new RuntimeException("创建缩略图失败,文件未找到!"); + } catch (ImageFormatException e) { + e.printStackTrace(); + throw new RuntimeException("创建缩略图失败,图像格式错误!"); + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException("创建缩略图失败!"); + } + } else { + // 不需要缩放,那么缩略图直接复制原图像 + try { + thumbnailFOS = new FileOutputStream(thumbnailFile); + thumbnailFOS.write(originalImageData); + } catch (FileNotFoundException e) { + e.printStackTrace(); + throw new RuntimeException("创建缩略图失败,文件未找到!"); + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException("创建缩略图失败!"); + } finally { + FileUtils.closeFileOutputStream(thumbnailFOS); + } + } + } + + // 根据总的像素数判断是否需要缩放 + private boolean whetherNeedToZoomByTotalPixles(int originalWidth,int originalHeight,int totalPixles){ + if(originalWidth*originalHeight <= totalPixles){ + return false; + } + return true; + } + // 根据是否超出边界判断是否需要缩放 + private boolean whetherNeedToZoomByBoundary(int originalWidth,int originalHeight,int boundaryWidth,int boundaryHeight){ + if(originalWidth <= boundaryWidth && originalHeight<=boundaryHeight){ + // 原始图片完全小于或者等于边框,那么不需要缩放 + return false; + } + return true; + } + private void calculateZoomSize(int originalWidth,int originalHeight,int boundaryWidth,int boundaryHeight,SizeAndPosition sizeAndPosition){ + + sizeAndPosition.scaledWidth = originalWidth; + sizeAndPosition.scaledHeight = originalHeight; + sizeAndPosition.x = 0; + sizeAndPosition.y = 0; + double desScaleYX = boundaryHeight*1.0/boundaryWidth; // 屏幕的高宽比 + double srcScaleYX = originalHeight*1.0/originalWidth; // 图像的高宽比 + + if(originalWidth > boundaryWidth || originalHeight > boundaryHeight) + { + // 任何一边大于屏幕时,等比例缩放 + if(srcScaleYX>desScaleYX) + { + sizeAndPosition.scaledHeight = boundaryHeight; +// scaledWidth = ((int)(boundaryHeight*1.0/originalHeight*originalWidth))&~1;// 保证为2的倍数 + sizeAndPosition.scaledWidth = ((int)(boundaryHeight*1.0/originalHeight*originalWidth));// 保证为2的倍数 + }else + { + sizeAndPosition.scaledWidth = boundaryWidth; + sizeAndPosition.scaledHeight = ((int)(boundaryWidth*1.0/originalWidth*originalHeight)); + } + sizeAndPosition.x = (boundaryWidth - sizeAndPosition.scaledWidth)/2; + sizeAndPosition.y = (boundaryHeight - sizeAndPosition.scaledHeight)/2; + } + } + + @SuppressWarnings("unchecked") public byte[] getImageByIdAndType(Long id,String type, String page) { Index: ssts-tousse/src/main/java/com/forgon/disinfectsystem/tousse/imagefilemanager/service/ImageFileManager.java =================================================================== diff -u -r12331 -r14968 --- ssts-tousse/src/main/java/com/forgon/disinfectsystem/tousse/imagefilemanager/service/ImageFileManager.java (.../ImageFileManager.java) (revision 12331) +++ ssts-tousse/src/main/java/com/forgon/disinfectsystem/tousse/imagefilemanager/service/ImageFileManager.java (.../ImageFileManager.java) (revision 14968) @@ -35,8 +35,6 @@ public void saveImageToLocal(ImageFile imageFileObj,String saveImageDirectory); public void saveImage(ImageFile imageFile); - - public BufferedImage abbreviatedZoom(byte[] imageData); public byte[] getImageByIdAndType(Long id,String type,String page); Index: ssts-basedata/src/main/java/com/forgon/disinfectsystem/common/geom/SizeAndPosition.java =================================================================== diff -u --- ssts-basedata/src/main/java/com/forgon/disinfectsystem/common/geom/SizeAndPosition.java (revision 0) +++ ssts-basedata/src/main/java/com/forgon/disinfectsystem/common/geom/SizeAndPosition.java (revision 14968) @@ -0,0 +1,15 @@ +/** + * + */ +package com.forgon.disinfectsystem.common.geom; + +/** + * @author jeffli 2016年9月13日 下午4:26:58 + * + */ +public class SizeAndPosition { + public int scaledWidth = 0; + public int scaledHeight = 0; + public int x = 0; + public int y = 0; +} Index: ssts-web/src/main/webapp/disinfectsystem/packing/packingView.js =================================================================== diff -u -r14965 -r14968 --- ssts-web/src/main/webapp/disinfectsystem/packing/packingView.js (.../packingView.js) (revision 14965) +++ ssts-web/src/main/webapp/disinfectsystem/packing/packingView.js (.../packingView.js) (revision 14968) @@ -872,13 +872,19 @@ height:((sstsConfig.showInspectorField || sstsConfig.showWrapperField || sstsConfig.showSterilizer) ? 230 : 297), border : true, layout:'fit', - items : [{ - xtype : 'image', - id : 'browseImage', - fieldLabel : "预览图片", - title:'双击预览图片', - src : Ext4.BLANK_IMAGE_URL - }], + items: [{ + xtype : 'panel', + layout:'absolute', + id: 'imageContentPanel', + items: [{ + xtype : 'image', + id : 'browseImage', + fieldLabel : "预览图片", + title:'双击预览图片', + src : Ext4.BLANK_IMAGE_URL + }] + } + ], bbar: [ { id:'imageName'}, {xtype:'hidden',id:'imageType'}, @@ -1689,15 +1695,69 @@ function loadGoodsImage(tousseName,type,materialId,currentPage,vedioName,td_id){ var url = WWWROOT + "/disinfectSystem/recyclingApplicationAction!getImage.do?imageType=" + type +'&materialId='+materialId+'&tousseId='+td_id+'&page='+currentPage + '&name='+encodeURIComponent(tousseName); - var image = Ext4.getCmp('browseImage'); - image.setSrc(''); - image.setSrc(url);// 覆盖原来的图片 +// var image = Ext4.getCmp('browseImage'); +// image.setSrc(''); +// image.setSrc(url);// 覆盖原来的图片 Ext4.getCmp('imageName').setText(tousseName); Ext4.getCmp('imageType').setValue(type); Ext4.getCmp('currentPage').setValue(currentPage); Ext4.getCmp('materialId').setValue(materialId); Ext4.getCmp('vedioName').setValue(vedioName); Ext4.getCmp('td_id').setValue(td_id); + + var panel = Ext4.getCmp('imageContentPanel'); + var image = Ext4.getCmp('browseImage'); + panel.remove(image); + + var newImage = Ext4.create('Ext4.Img', { + id : 'browseImage', + fieldLabel : "预览图片", + title:'双击预览图片', + listeners: { + render: function() { + var imgCmp = this; + var img = this.imgEl + img.on('load',function(){ + var img = this; + var panel = Ext4.getCmp('imageContentPanel'); + var panelSize = panel.getSize(true); + + var boundaryWidth = panelSize.width; + var boundaryHeight = panelSize.height; + var originalWidth = img.getWidth(); + var originalHeight = img.getHeight(); + var scaledWidth = originalWidth; + var scaledHeight = originalHeight; + var x=0,y=0; + // 计算出合适的大小 + var desScaleYX = boundaryHeight*1.0/boundaryWidth; // 屏幕的高宽比 + var srcScaleYX = originalHeight*1.0/originalWidth; // 图像的高宽比 + if(originalWidth > boundaryWidth || originalHeight > boundaryHeight) + { + // 任何一边大于屏幕时,等比例缩放 + if(srcScaleYX>desScaleYX) + { + scaledHeight = boundaryHeight; + scaledWidth = parseInt(boundaryHeight*1.0/originalHeight*originalWidth);// 保证为2的倍数 + }else + { + scaledWidth = boundaryWidth; + scaledHeight = parseInt(boundaryWidth*1.0/originalWidth*originalHeight); + } + x = (boundaryWidth - scaledWidth)/2; + y = (boundaryHeight - scaledHeight)/2; + } + imgCmp.setSize(scaledWidth,scaledHeight); + imgCmp.setPosition(x,y); +// Ext.Msg.alert('x='+x+',y='+y+',w='+scaledWidth+',h='+scaledHeight+',bw='+boundaryWidth+',bh='+boundaryHeight); + + }); + } + }, + src : url + }); + + panel.add(newImage); } function loadTousseInfo(tousseName,type,vedioName,td_id){ Index: forgon-core/src/main/java/com/forgon/util/FileUtils.java =================================================================== diff -u -r12331 -r14968 --- forgon-core/src/main/java/com/forgon/util/FileUtils.java (.../FileUtils.java) (revision 12331) +++ forgon-core/src/main/java/com/forgon/util/FileUtils.java (.../FileUtils.java) (revision 14968) @@ -94,4 +94,14 @@ } return saved; } + + public static void closeFileOutputStream(FileOutputStream fos) { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } }