Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Images/JPEGFlip.java
41175 views
/*1*2* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/31package java2d.demos.Images;323334import static java.awt.Color.BLACK;35import static java.awt.Color.GREEN;36import static java.awt.Color.RED;37import static java.awt.Color.WHITE;38import java.awt.Font;39import java.awt.Graphics2D;40import java.awt.Image;41import java.awt.geom.GeneralPath;42import java.awt.geom.Path2D;43import java.awt.image.BufferedImage;44import java.io.ByteArrayInputStream;45import java.io.ByteArrayOutputStream;46import java.io.IOException;47import java.util.logging.Level;48import java.util.logging.Logger;49import java2d.Surface;50import javax.imageio.IIOImage;51import javax.imageio.ImageIO;52import javax.imageio.ImageWriteParam;53import javax.imageio.ImageWriter;54import javax.imageio.plugins.jpeg.JPEGImageWriteParam;55import javax.imageio.stream.ImageOutputStream;565758/**59* Render a filled star & duke into a BufferedImage, save the BufferedImage60* as a JPEG, display the BufferedImage, using the decoded JPEG BufferedImage61* display the JPEG flipped BufferedImage.62*/63@SuppressWarnings("serial")64public class JPEGFlip extends Surface {6566private static Image img;6768public JPEGFlip() {69setBackground(WHITE);70img = getImage("duke.png");71}7273@Override74public void render(int w, int h, Graphics2D g2) {7576int hh = h / 2;7778BufferedImage bi = new BufferedImage(w, hh, BufferedImage.TYPE_INT_RGB);79Graphics2D big = bi.createGraphics();8081// .. use rendering hints from J2DCanvas ..82big.setRenderingHints(g2.getRenderingHints());8384big.setBackground(getBackground());85big.clearRect(0, 0, w, hh);8687big.setColor(GREEN.darker());88GeneralPath p = new GeneralPath(Path2D.WIND_NON_ZERO);89p.moveTo(-w / 2.0f, -hh / 8.0f);90p.lineTo(+w / 2.0f, -hh / 8.0f);91p.lineTo(-w / 4.0f, +hh / 2.0f);92p.lineTo(+0.0f, -hh / 2.0f);93p.lineTo(+w / 4.0f, +hh / 2.0f);94p.closePath();95big.translate(w / 2, hh / 2);96big.fill(p);9798float scale = 0.09f;99int iw = (int) (scale * w);100int ih = (int) (img.getHeight(null) * iw / img.getWidth(null));101big.drawImage(img, -iw / 2, -ih / 2, iw, ih, this);102103g2.drawImage(bi, 0, 0, this);104g2.setFont(new Font("Dialog", Font.PLAIN, 10));105g2.setColor(BLACK);106g2.drawString("BufferedImage", 4, 12);107108109BufferedImage bi1 = null;110ImageOutputStream ios = null;111// To write the jpeg to a file uncomment the File* lines and112// comment out the ByteArray*Stream lines.113//FileOutputStream out = null;114ByteArrayOutputStream out = null;115//FileInputStream in = null;116ByteArrayInputStream in = null;117try {118//File file = new File("images", "test.jpg");119//out = new FileOutputStream(file);120out = new ByteArrayOutputStream();121ios = ImageIO.createImageOutputStream(out);122ImageWriter encoder =123ImageIO.getImageWritersByFormatName("JPEG").next();124JPEGImageWriteParam param = new JPEGImageWriteParam(null);125126param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);127param.setCompressionQuality(1.0f);128129encoder.setOutput(ios);130encoder.write(null, new IIOImage(bi, null, null), param);131132//in = new FileInputStream(file);133in = new ByteArrayInputStream(out.toByteArray());134bi1 = ImageIO.read(in);135} catch (Exception ex) {136g2.setColor(RED);137g2.drawString("Error encoding or decoding the image", 5, hh * 2 - 5);138return;139} finally {140if (ios != null) {141try {142ios.close();143} catch (IOException ex) {144Logger.getLogger(JPEGFlip.class.getName()).log(Level.SEVERE,145null, ex);146}147}148if (out != null) {149try {150out.close();151} catch (IOException ex) {152Logger.getLogger(JPEGFlip.class.getName()).log(Level.SEVERE,153null, ex);154}155}156if (in != null) {157try {158in.close();159} catch (IOException ex) {160Logger.getLogger(JPEGFlip.class.getName()).log(Level.SEVERE,161null, ex);162}163}164}165166if (bi1 == null) {167g2.setColor(RED);168g2.drawString("Error reading the image", 5, hh * 2 - 5);169return;170}171172g2.drawImage(bi1, w, hh * 2, -w, -hh, null);173174g2.drawString("JPEGImage Flipped", 4, hh * 2 - 4);175g2.drawLine(0, hh, w, hh);176}177178public static void main(String[] s) {179createDemoFrame(new JPEGFlip());180}181}182183184