Source Code Cross Referenced for ImageUtil.java in  » Portal » stringbeans-3.5 » com » nabhinc » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » stringbeans 3.5 » com.nabhinc.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * (C) Copyright 2004 Nabh Information Systems, Inc.
003:         *
004:         * All copyright notices regarding Nabh's products MUST remain
005:         * intact in the scripts and in the outputted HTML.
006:         * This program is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public License
008:         * as published by the Free Software Foundation; either version 2.1 
009:         * of the License, or (at your option) any later version.
010:         * 
011:         * This program is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of 
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
014:         * GNU Lesser General Public License for more details.
015:         * 
016:         * You should have received a copy of the GNU Lesser General Public License
017:         * along with this program; if not, write to the Free Software 
018:         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
019:         *
020:         */
021:
022:        package com.nabhinc.util;
023:
024:        import java.awt.Graphics2D;
025:        import java.awt.Image;
026:        import java.awt.geom.AffineTransform;
027:        import java.awt.image.BufferedImage;
028:        import java.io.FileOutputStream;
029:        import java.io.IOException;
030:        import java.io.OutputStream;
031:
032:        import javax.swing.ImageIcon;
033:
034:        import com.sun.image.codec.jpeg.JPEGCodec;
035:        import com.sun.image.codec.jpeg.JPEGImageEncoder;
036:
037:        /**
038:         *
039:         * @author Padmanabh Dabke
040:         * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
041:         */
042:        public class ImageUtil {
043:
044:            /**
045:             * Create a jpeg thumbnail image with the specified maximum size. If the image size is larger 
046:             * than the maximum allowed, the size (width and height) will be scaled to the actual 
047:             * ratio (width/height). The output image file name must have .jpg or jpeg extension.
048:             *
049:             * @param inputStream     The original image input stream.
050:             * @param imageFileName   The output image file. The name must be ended with .jpg or jpeg
051:             * @param maxWidth        Image's maximum width
052:             * @param maxHeight       Image's maximum height
053:             * @throws IOException	  Thrown if the output filename's extension is not jpg.
054:             */
055:            public static void createJPEGThumbnail(byte[] imgByte,
056:                    String imageFileName, int maxWidth, int maxHeight)
057:                    throws IOException {
058:
059:                if (!imageFileName.toLowerCase().endsWith(".jpg")
060:                        && !imageFileName.toLowerCase().endsWith(".jpeg"))
061:                    throw new IOException(
062:                            "Unsupported image type. Only support image with extension '.jpg' or '.jpeg'.");
063:
064:                OutputStream outputStream = null;
065:                try {
066:                    ImageIcon imgIcon = new ImageIcon(imgByte);
067:                    Image srcImg = imgIcon.getImage();
068:                    int imgWidth = srcImg.getWidth(null);
069:                    int imgHeight = srcImg.getHeight(null);
070:                    if ((imgWidth <= 0) || (imgHeight <= 0)) {
071:                        throw new IOException("Invalid image size.");
072:                    }
073:
074:                    AffineTransform aTransform = new AffineTransform();
075:                    if ((imgWidth > maxWidth) || (imgHeight > maxHeight)) {
076:                        double scaleX = (double) maxWidth / imgWidth;
077:                        double scaleY = (double) maxHeight / imgHeight;
078:                        double scaleRatio = (scaleX < scaleY) ? scaleX : scaleY;
079:                        imgWidth = (int) (imgWidth * scaleRatio);
080:                        imgHeight = (int) (imgHeight * scaleRatio);
081:                        aTransform.scale(scaleRatio, scaleRatio);
082:                    }
083:
084:                    BufferedImage bufferedImg = new BufferedImage(imgWidth,
085:                            imgHeight, BufferedImage.TYPE_INT_RGB);
086:
087:                    Graphics2D g = bufferedImg.createGraphics();
088:                    g.drawImage(srcImg, aTransform, null);
089:                    g.dispose();
090:
091:                    outputStream = new FileOutputStream(imageFileName);
092:                    JPEGImageEncoder encoder = JPEGCodec
093:                            .createJPEGEncoder(outputStream);
094:                    encoder.encode(bufferedImg);
095:
096:                } catch (IOException ie) {
097:                    throw ie;
098:
099:                } finally {
100:                    if (outputStream != null)
101:                        outputStream.close();
102:                }
103:            }
104:
105:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.