⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imageeffects.java

📁 j2me源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 *
 * Copyright (c) 2007, Sun Microsystems, Inc.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of Sun Microsystems nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Vector;

import javax.microedition.amms.GlobalManager;
import javax.microedition.amms.MediaProcessor;
import javax.microedition.amms.MediaProcessorListener;
import javax.microedition.amms.control.imageeffect.ImageTransformControl;
import javax.microedition.amms.control.imageeffect.ImageEffectControl;
import javax.microedition.amms.control.imageeffect.OverlayControl;
import javax.microedition.amms.control.ImageFormatControl;
import javax.microedition.amms.control.EffectOrderControl;
        
import javax.microedition.media.MediaException;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class ImageEffects extends MIDlet implements MediaProcessorListener, 
    CommandListener {
    
    static final String format_array[] = new String[] {
        "raw", 
        "jpeg", 
        "png"
    };
            
    static final String stream_array[] = new String[] {
        "/images/lama1_96x96.jpg",
        "/images/su15.jpg",
        "/images/mig15.png"
    };
            
    static final String order_name_array[] = new String[] {
        "TransformControl", 
        "EffectControl", 
        "OverlayControl"
    };
            
    static final String effect_array[] = new String[] {
        "no", //null, 
        "monochrome", 
        "negative", 
        "emboss", 
        "sepia", 
        "solarize", 
        "redeyereduction"
    };
    
    static final int scale_array[] = new int[] {
        80, 100, 120
    };
    
    static final int overlay_size[] = new int[] {50, 10}; //2 overlays with size=50x10 & 10x50
    
    static final String color_name_array[] = new String[] {
        "A", "R", "G", "B"
    };
    
    private InputStream inputStream;
    private ByteArrayOutputStream outputStream;
    private Image sourceImage;
    private Image processedImage;
    
    private int indexImageEffect;
    private int indexOutputImageFormat;
    private int indexInputImageFormat;
    private int indexInputStreamName;

    private int indexOutputScaleW;
    private int indexOutputScaleH;
    
    private int indexOutputRotate; //can be 0,1,2,3 (*90 = clockwise rotation angle)
    private boolean indexOutputBorder[]; // = new boolean[1];
    private boolean indexOutputOverlay[]; // = new boolean[2];
    private int indexEffectOrder[]; // = new int[3];
    
    /*
     * these objects needed for overlay control
     */
    int argb_color[][]; // = new int[2][4]; //current overlay colors
    int argb_buffer[] = new int[overlay_size[0] * overlay_size[1]];
    
    /* 
     * Used to distinguish first appearance of Forms on the screen
     * i.e. case where we can not compare new value with previous.
     */
    private boolean first_form;
    
    /*
     * false - use process() based blocking wait (no callbacks)
     * true - use start() & callbacks ...
     */
    private final boolean need_callback = true;
    /*
     * false - use raw image from resources as input
     *        -> uses RAW ImageProcessor
     * true - use jpeg/png input stream to create jpeg input stream 
            -> uses JPEG/PNG ImageProcessor
     */
    private boolean need_input_stream;
    /*
     * false - use byte array to create processed image in RAW format,
     * true - use (byte array based) Input Stream to create 
     *        processed image in a compressed (JPEG or PNG) format.
     */
    private boolean need_output_stream;
    
    private Command cmdExit;
    private Command cmdDone;
    private Command cmdSetData;
    private Command cmdSetOrder;
    private Command cmdSetScale;
    private Command cmdSetEffect;
    private Command cmdSetOverlay;
    private Command cmdNoEffect;
    private Command cmdMonochrome;
    private Command cmdNegative;
    private Command cmdSepia;
    private Command cmdSolarize;
    private Command cmdEmboss;
    private Command cmdRedEye;
            
    private int processedImageItemIndex;
    private Form dataForm;
    private Form orderForm;
    private Form scaleForm;
    private Form effectForm;
    private Form overlayForm;
    private Form resultViewer;
    
    private ChoiceGroup inputFormatSelector;
    private ChoiceGroup outputFormatSelector;
    private ChoiceGroup widthScaleSelector;
    private ChoiceGroup heightScaleSelector;
    private ChoiceGroup borderScaleSelector;
    private ChoiceGroup rotateScaleSelector;
    private ChoiceGroup effectSelector;
    private ChoiceGroup overlaySelector;
    private TextField[][] overlayColor;
    private TextField[] effectOrder;

    private Display display;
    private Form currentForm;

    public ImageEffects() {
        int i, j;
        
        //SETUP STATE COMPONENTS
        indexImageEffect = 0;
        indexInputStreamName = 0;
        indexInputImageFormat = 0;
        indexOutputImageFormat = 0;
        indexOutputScaleW = 1;
        indexOutputScaleH = 1;
        
        indexOutputRotate = 0;
        indexOutputBorder = new boolean[] { false };
        indexOutputOverlay = new boolean[] { false, false };
        indexEffectOrder = new int[] { 0, 1, 2 };
        
        argb_color = new int[][] {
            { 0xFF, 0x00, 0xFF, 0x00 },
            { 0x7F, 0xFF, 0xFF, 0x00 }
        };
                
        need_input_stream = false;
        need_output_stream = false;
        
        // SETUP GUI COMPONENTS
        first_form = true;
        
        //create display
        display = Display.getDisplay(this);
        //create forms
        dataForm = new Form("Select Image Formats"); //selects input and output image formats
        orderForm = new Form("Select Effect Order"); //selects relative order of image "transform", "effect", "overlay"
        scaleForm = new Form("Select Image Transformations"); //selects output image resize (border only), scale & rotate  
        effectForm = new Form("Select Image Effect"); //selects one of 6 effects
        overlayForm = new Form("Select Image Overlays"); //selects 0,1,2,1&2 overlays 
        resultViewer = new Form("Images");
        
        //create commands
        cmdDone = new Command("Done", Command.BACK, 1);
        cmdExit = new Command("Exit", Command.EXIT, 2);
        cmdSetData = new Command("Set Formats", Command.ITEM, 3);
        cmdSetOrder = new Command("Set Effect Order", Command.ITEM, 3);
        cmdSetScale = new Command("Set Transforms", Command.ITEM, 3);
        cmdSetEffect = new Command("Set Effects", Command.ITEM, 3);
        cmdSetOverlay = new Command("Set Overlays", Command.ITEM, 3);
        cmdNoEffect = new Command("No Effect", Command.ITEM, 4);
        cmdMonochrome = new Command("Monochrome Effect", Command.ITEM, 4);
        cmdNegative = new Command("Negative Effect", Command.ITEM, 4);
        cmdEmboss = new Command("Emboss Effect", Command.ITEM, 4);
        cmdSepia = new Command("Sepia Effect", Command.ITEM, 4);
        cmdSolarize = new Command("Solarize Effect", Command.ITEM, 4);
        cmdRedEye = new Command("RedEye Effect", Command.ITEM, 4);
        
        //fill dataForm with components
        inputFormatSelector = new ChoiceGroup(
                "Input Object Type", 
                Choice.EXCLUSIVE);
        inputFormatSelector.append("Image object", null);
        inputFormatSelector.append("JPEG stream", null);
        inputFormatSelector.append("PNG stream", null);
        inputFormatSelector.setSelectedIndex(indexInputImageFormat, true);
        inputFormatSelector.setFitPolicy(Choice.TEXT_WRAP_OFF);
        
        outputFormatSelector = new ChoiceGroup(
                "Output Image Format", 
                Choice.EXCLUSIVE);
        outputFormatSelector.append("image/raw", null);
        outputFormatSelector.append("image/jpeg", null);
        outputFormatSelector.append("image/png", null);
        outputFormatSelector.setSelectedIndex(indexOutputImageFormat, true);
        outputFormatSelector.setFitPolicy(Choice.TEXT_WRAP_OFF);
        
        dataForm.append(inputFormatSelector);
        dataForm.append(outputFormatSelector);
        
        dataForm.addCommand(cmdExit);
        dataForm.addCommand(cmdDone);
        
        //fill orderForm with components
        effectOrder = new TextField[3];
        for (i = 0; i < effectOrder.length; ++i) {
            effectOrder[i] = new TextField(order_name_array[i] + "\n", 
                    Integer.toString(indexEffectOrder[i]), 
                    10, 
                    TextField.NUMERIC);
            effectOrder[i].setLayout(
                Item.LAYOUT_2 | 
                Item.LAYOUT_NEWLINE_AFTER | 
                //Item.LAYOUT_EXPAND | Item.LAYOUT_VEXPAND | 
                Item.LAYOUT_SHRINK | //Item.LAYOUT_VSHRINK | 
                Item.LAYOUT_CENTER | Item.LAYOUT_TOP);
            orderForm.append(effectOrder[i]);
        }
        
        StringItem orderNote = new StringItem("Attention!", 
                "\nAlthough you are allowed" +
                "\nto set any integer values," +
                "\nAll input values" +
                "\nwill be converted" +
                "\nto range[0..2]" +
                "\nby implementation.\n");
        orderNote.setLayout(
                Item.LAYOUT_2 | 
                //Item.LAYOUT_EXPAND | Item.LAYOUT_VEXPAND | 
                Item.LAYOUT_SHRINK | Item.LAYOUT_VSHRINK | 
                Item.LAYOUT_CENTER | Item.LAYOUT_BOTTOM);
        orderForm.append(orderNote);

        orderForm.addCommand(cmdExit);
        orderForm.addCommand(cmdDone);
        
        //fill scaleForm with components
        widthScaleSelector = new ChoiceGroup(
                "Width Scale", 
                Choice.EXCLUSIVE);
        for (i = 0; i < scale_array.length; ++i) {
            widthScaleSelector.append(
                    Integer.toString(scale_array[i]) + "%", null);
        }
        widthScaleSelector.setSelectedIndex(indexOutputScaleW, true);
        widthScaleSelector.setFitPolicy(Choice.TEXT_WRAP_OFF);
        /*
        widthScaleSelector.setLayout(
                Item.LAYOUT_2 | 
                //Item.LAYOUT_EXPAND | Item.LAYOUT_VEXPAND | 
                Item.LAYOUT_SHRINK | Item.LAYOUT_VSHRINK | 
                Item.LAYOUT_CENTER | Item.LAYOUT_TOP);
         */

        heightScaleSelector = new ChoiceGroup(
                "Height Scale", 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -