程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> j2me 圖片的高效翻轉

j2me 圖片的高效翻轉

編輯:J2ME

在MIDP1.0中開發者自己實現翻轉,在MIDP2.0中系統已經提供了翻轉方法,但有些機型翻轉效果不正確(廠商實現問題),所以自己實現翻轉方法。本想一次將圖讀到一個大INT數據,但如果圖片過大,會內存溢出,所以通過對圖片的每行進行變換來達到高速翻轉。如果你有更高效的方法歡迎討論。


import Javax.microedition.lcdui.Graphics;
import Javax.microedition.lcdui.Image;

/** *//** 
 * @功能:實現圖片的高效翻轉
 * @作者: imuse
 * @MAIL: [email protected]
 *  
 */

public class ImageUtil ...{
    

    /** *//**
     * 無變化
     * */
    public final static int TRANS_NONE = 0;

    /** *//**
     * 順時針翻轉90度
     * */
    public final static int TRANS_ROT90 = 5;

    /** *//**
     * 順時針翻轉180度
     * */
    public final static int TRANS_ROT180 = 3;

    /** *//**
     * 順時針翻轉270度
     * */
    public final static int TRANS_ROT270 = 6;
    
    /** *//**
     * 水平鏡像翻轉
     * */
    public final static int TRANS_MIRROR = 2;

    /** *//**
     * 水平鏡像翻轉並順時針翻轉90度
     * */
    public final static int TRANS_MIRROR_ROT90 = 7;

    /** *//**
     * 水平鏡像翻轉並順時針翻轉180度
     * */
    public final static int TRANS_MIRROR_ROT180 = 1;

    /** *//**
     * 水平鏡像翻轉並順時針翻轉270度
     * */
    public final static int TRANS_MIRROR_ROT270 = 4;

    /** *//**
     * @功能 實現圖片的翻轉
     * @參數 Image srcImage    原圖<br>int transform 翻轉方向<br>
     * @返回值 轉換後的圖片
     * */
    public static Image transformImage(Image srcImage, int transform) ...{
        int srcW = srcImage.getWidth();
        int srcH = srcImage.getHeight();

        Image imgRet = null;
        switch (transform) ...{
        case TRANS_NONE:
            imgRet = srcImage;
            break;
        case TRANS_ROT90: 
            ...{
                /** *//**
                 * 原:            目標:
                 *         1234        951
                 *         5678        062
                 *         90ab        a73
                 *                     b84
                 * */
                int[] srcRGB = new int[srcW];
    
                imgRet = Image.createImage(srcH, srcW);
                Graphics g = imgRet.getGraphics();
                for (int i = 0; i < srcH; i++) ...{
                    srcImage.getRGB(srcRGB, 0, srcW, 0, i, srcW, 1);
                    g.drawRGB(srcRGB, 0, 1, srcH - i - 1, 0, 1, srcW, true);
                }
            }
            break;
        case TRANS_ROT180: 
            ...{
                /** *//**
                 * 原:            目標:
                 *         1234        ba09
                 *         5678        8765
                 *         90ab        4321
                 *                     
                 * */
                int[] srcRGB = new int[srcW];
                
                imgRet = Image.createImage(srcW, srcH);
                Graphics g = imgRet.getGraphics();
                for (int i = 0; i < srcH; i++) ...{
                    srcImage.getRGB(srcRGB, 0, srcW, 0, i, srcW, 1);
                    arrayExchange(srcRGB);
                    g.drawRGB(srcRGB, 0, srcW, 0, srcH-1-i, srcW, 1, true);
                }
            }
            break;
        case TRANS_ROT270: 
            ...{
                /** *//**
                 * 原:            目標:
                 *         1234        48b
                 *         5678        37a
                 *         90ab        260
                 *                     159
                 * */
                int[] srcRGB = new int[srcW];
                
                imgRet = Image.createImage(srcH, srcW);
                Graphics g = imgRet.getGraphics();
                for (int i = 0; i < srcH; i++) ...{
                    srcImage.getRGB(srcRGB, 0, srcW, 0, i, srcW, 1);
                    arrayExchange(srcRGB);
                    g.drawRGB(srcRGB, 0, 1, i, 0, 1, srcW, true);
                }
            }
            break;
        case TRANS_MIRROR: 
            ...{
                /** *//**
                 * 原:            目標:
                 *         1234        4321
                 *         5678        8765
                 *         90ab        ba09
                 *                     
                 * */
                int[] srcRGB = new int[srcW];
                
                imgRet = Image.createImage(srcW, srcH);
                Graphics g = imgRet.getGraphics();
                for (int i = 0; i < srcH; i++) ...{
                    srcImage.getRGB(srcRGB, 0, srcW, 0, i, srcW, 1);
                    arrayExchange(srcRGB);
                    g.drawRGB(srcRGB, 0, srcW, 0, i, srcW, 1, true);
                }
            }
            break;
        case TRANS_MIRROR_ROT90: 
            ...{
                /** *//**
                 * 原:            目標:
                 *         1234        b84
                 *         5678        a73
                 *         90ab        062
                 *                     951
                 * */
                int[] srcRGB = new int[srcW];
                
                imgRet = Image.createImage(srcH, srcW);
                Graphics g = imgRet.getGraphics();
                for (int i = 0; i < srcH; i++) ...{
                    srcImage.getRGB(srcRGB, 0, srcW, 0, i, srcW, 1);
                    arrayExchange(srcRGB);
                    g.drawRGB(srcRGB, 0, 1, srcH - i - 1, 0, 1, srcW, true);
                }
            }
            break;
        case TRANS_MIRROR_ROT180:
            ...{
                /** *//**
                 * 原:            目標:
                 *         1234        90ab
                 *         5678        5678
                 *         90ab        1234
                 *                     
                 * */
                int[] srcRGB = new int[srcW];
                
                imgRet = Image.createImage(srcW, srcH);
                Graphics g = imgRet.getGraphics();
                for (int i = 0; i < srcH; i++) ...{
                    srcImage.getRGB(srcRGB, 0, srcW, 0, i, srcW, 1);
                    g.drawRGB(srcRGB, 0, srcW, 0, srcH-1-i, srcW, 1, true);
                }
            }
            break;
        case TRANS_MIRROR_ROT270:
            ...{
                /** *//**
                 * 原:            目標:
                 *         1234        159
                 *         5678        260
                 *         90ab        37a
                 *                     48b
                 * */
                int[] srcRGB = new int[srcW];
                
                imgRet = Image.createImage(srcH, srcW);
                Graphics g = imgRet.getGraphics();
                for (int i = 0; i < srcH; i++) ...{
                    srcImage.getRGB(srcRGB, 0, srcW, 0, i, srcW, 1);
                    g.drawRGB(srcRGB, 0, 1, i, 0, 1, srcW, true);
                }
            }
            break;
        default:
            imgRet = srcImage;
            break;
        }
        return imgRet;
    }

    /** *//**
     * @功能 翻轉一個INT數組
     * @參數 要轉換的INT數組
     * @返回值    無
     * */
    public static final void arrayExchange(int[] b)...{
        int nCount = b.length;
        int nLen = nCount/2;
        nCount--;
        
        for (int i = 0; i < nLen; i++) ...{
            int nTemp = b[i];
            b[i] = b[nCount-i];
            b[nCount-i] = nTemp;
        }
    }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved