View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.examples;
18  
19  import java.awt.Dimension;
20  import java.awt.color.ICC_Profile;
21  import java.awt.image.BufferedImage;
22  import java.io.File;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  
26  import org.apache.commons.imaging.FormatCompliance;
27  import org.apache.commons.imaging.ImageFormat;
28  import org.apache.commons.imaging.ImageFormats;
29  import org.apache.commons.imaging.ImageInfo;
30  import org.apache.commons.imaging.Imaging;
31  import org.apache.commons.imaging.common.ImageMetadata;
32  
33  public class SampleUsage {
34  
35      @SuppressWarnings("unused")
36      public SampleUsage() {
37  
38          try {
39              // <b>Code won't work unless these variables are properly
40              // initialized.
41              // Imaging works equally well with File, byte array or InputStream
42              // inputs.</b>
43              final BufferedImage someImage = null;
44              final byte[] someBytes = null;
45              final File someFile = null;
46              final InputStream someInputStream = null;
47              final OutputStream someOutputStream = null;
48  
49              // <b>The Imaging class provides a simple interface to the library.
50              // </b>
51  
52              // <b>how to read an image: </b>
53              final byte[] imageBytes = someBytes;
54              final BufferedImage image1 = Imaging.getBufferedImage(imageBytes);
55  
56              // <b>methods of Imaging usually accept files, byte arrays, or
57              // inputstreams as arguments. </b>
58              final BufferedImage image2 = Imaging.getBufferedImage(imageBytes);
59              final File file = someFile;
60              final BufferedImage image3 = Imaging.getBufferedImage(file);
61              final InputStream is = someInputStream;
62              final BufferedImage image4 = Imaging.getBufferedImage(is);
63  
64              // <b>Write an image. </b>
65              final BufferedImage image = someImage;
66              final File dst = someFile;
67              final ImageFormat format = ImageFormats.PNG;
68              Imaging.writeImage(image, dst, format);
69  
70              final OutputStream os = someOutputStream;
71              Imaging.writeImage(image, os, format);
72  
73              // <b>get the image's embedded ICC Profile, if it has one. </b>
74              final byte[] iccProfileBytes = Imaging.getIccProfileBytes(imageBytes);
75  
76              final ICC_Profile iccProfile = Imaging.getIccProfile(imageBytes);
77  
78              // <b>get the image's width and height. </b>
79              final Dimension d = Imaging.getImageSize(imageBytes);
80  
81              // <b>get all of the image's info (ie. bits per pixel, size,
82              // transparency, etc.) </b>
83              final ImageInfo imageInfo = Imaging.getImageInfo(imageBytes);
84  
85              if (imageInfo.getColorType() == ImageInfo.ColorType.GRAYSCALE) {
86                  System.out.println("Grayscale image.");
87              }
88              if (imageInfo.getHeight() > 1000) {
89                  System.out.println("Large image.");
90              }
91  
92              // <b>try to guess the image's format. </b>
93              final ImageFormat imageFormat = Imaging.guessFormat(imageBytes);
94              imageFormat.equals(ImageFormats.PNG);
95  
96              // <b>get all metadata stored in EXIF format (ie. from JPEG or
97              // TIFF). </b>
98              final ImageMetadata metadata = Imaging.getMetadata(imageBytes);
99  
100             // <b>print a dump of information about an image to stdout. </b>
101             Imaging.dumpImageFile(imageBytes);
102 
103             // <b>get a summary of format errors. </b>
104             final FormatCompliance formatCompliance = Imaging.getFormatCompliance(imageBytes);
105 
106         } catch (final Exception e) {
107 
108         }
109     }
110 }