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

📄 file_jpeg_to_image.java

📁 基于J2ME的JPEG decode和encode
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
import java.io.*;
public class File_JPEG_To_Image{

    Main_Canvas par;
    DecoderStream j = null;

    // Implementation of DecodeListener 
    int width, height;
    int rotate = 0;
    boolean needZoom = false;
    boolean lostTrue = false;
    int desW, desH;
    short[] tabY;
    short[] tabX;

    public File_JPEG_To_Image(Main_Canvas p, /*InputStream im*/byte[] im, int ro,
            boolean needZoom, int desW, int desH, boolean lostTrue) {
        try {
            par = p;
            rotate = ro;
            par.srcZoomBuf = null;
            if (needZoom) {
                this.desW = desW;
                this.desH = desH;
                this.needZoom = true;
                this.lostTrue = lostTrue;
            }
            j = new DecoderStream(this);
            if (im != null) {
                j.decode(im);
            }
        } catch (Exception ex) {
            par.srcZoomBuf = null;
        }
    }
    byte[] thumbnailD;
    File_Image_To_JPG fileImageToJPG;

    public File_JPEG_To_Image(Main_Canvas p, byte[]im, int ro, boolean needZoom, int desW, int desH, boolean lostTrue, File_Image_To_JPG fileImageToJPG, byte[] thumbnailD) {
        try {
            par = p;
            rotate = ro;
            par.srcZoomBuf = null;
            if (needZoom) {
                this.desW = desW;
                this.desH = desH;
                this.needZoom = true;
                this.lostTrue = lostTrue;
            }
            j = new DecoderStream(this);
            this.fileImageToJPG = fileImageToJPG;
            this.thumbnailD = thumbnailD;
            if (im != null) {
                par.printMemory("before decode:");
                Main_Canvas.rotateProcess = 1;
                j.decode(im);
                par.log("par.pW: " + par.pW + " par.pH " + par.pH);
                Main_Canvas.rotateProcess = 70;
                par.printMemory("before chunk Rotate:");
                this.fileImageToJPG.chunkRotate(ro, par.pW, par.pH);
                Main_Canvas.rotateProcess = 90;
                par.decodeFileBuf = this.fileImageToJPG.endEncode();
                Main_Canvas.rotateProcess = 93;
                par.log("par.decodeFileBuf: " + par.decodeFileBuf.length);
            }
        } catch (Exception ex) {
            par.log("File_JPEG_To_Image: "+ ex);
            par.srcZoomBuf = null;
        }
    }

    public void getImageSize(int width, int height) {
        this.width = width;
        this.height = height;
        if (this.needZoom) {
            if (!lostTrue) {
                int newWidth = 0;
                int newHeight = 0;
                if (width > desW || height > desH) {
                    if ((width - desW) > (height - desH)) {
                        newWidth = desW;
                        newHeight = newWidth * height / width;
                    } else {
                        newHeight = desH;
                        newWidth = newHeight * width / height;
                    }
                    desW = newWidth;
                    desH = newHeight;
                } else {
                    if ((desW - width) < (desH - height)) {
                        newWidth = desW;
                        newHeight = newWidth * height / width;
                    } else {
                        newHeight = desH;
                        newWidth = newHeight * width / height;
                    }
                    desW = newWidth;
                    desH = newHeight;
                }
            }
            switch (rotate) {
                case 0:
                case 3:
                    par.pW = desW;
                    par.pH = desH;
                    break;
                case 5:
                case 6:
                    par.pW = desH;
                    par.pH = desW;
                    break;
            }
//                        System.gc();
//                        par.Log("Before srcZoomBuf: "+((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1024));
            par.srcZoomBuf = new short[desW * desH];
//                        System.gc();
//                        par.Log("after srcZoomBuf: "+((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1024));

            tabY = new short[desH];
            tabX = new short[desW];
            short sb = 0;
            int db = 0;
            int tems = 0;
            int temd = 0;
            int distance = height > desH ? height : desH;
            for (int i = 0; i <= distance; i++) {
                tabY[db] = sb;
                tems += height;
                temd += desH;
                if (tems > distance) {
                    tems -= distance;
                    sb++;
                }
                if (temd > distance) {
                    temd -= distance;
                    db++;
                }
            }
            sb = 0;
            db = 0;
            tems = 0;
            temd = 0;
            distance = width > desW ? width : desW;
            for (int i = 0; i <= distance; i++) {
                tabX[db] = sb;
                tems += width;
                temd += desW;
                if (tems > distance) {
                    tems -= distance;
                    sb++;
                }
                if (temd > distance) {
                    temd -= distance;
                    db++;
                }
            }
            short tempdY[] = new short[height];
            short tempdX[] = new short[width];
            for (int a = 0; a < tempdY.length; a++) {
                tempdY[a] = -1;
            }
            for (int a = 0; a < tempdX.length; a++) {
                tempdX[a] = -1;
            }
            for (int a = 0; a < tabY.length; a++) {
                tempdY[tabY[a]] = (short) a;
            }
            for (int a = 0; a < tabX.length; a++) {
                tempdX[tabX[a]] = (short) a;
            }
            tabY = null;
            tabX = null;
            tabY = tempdY;
            tabX = tempdX;
        } else {
			switch(rotate){
			case 0:
			case 3:
				par.pW = width;
				par.pH = height;
				break;
			case 5:
			case 6:
				par.pW = height;
				par.pH = width;
				break;
			}
//            par.pW = width;
//            par.pH = height;
            par.printMemory("BildStream set size");
//            par.log(par.pW + " " + par.pH);
            if (fileImageToJPG != null) {
                fileImageToJPG.readyEncode(par.pW, par.pH, thumbnailD);
            } else {
                par.srcZoomBuf = new short[par.pW * par.pH];
            }
        }
    }
    
    public void close() {
        j.close();
        thumbnailD = null;
        if (fileImageToJPG != null) {
            fileImageToJPG.close();
            fileImageToJPG = null;
        }
        tabY = null;
        tabX = null;
    }
}

class DecoderStream {
    File_JPEG_To_Image par;
    int readIndex = 0;
    int DCTable[] = new int[64];
    int JPEG_table[] = {
        0, 1, 5, 6, 14, 15, 27, 28,
        2, 4, 7, 13, 16, 26, 29, 42,
        3, 8, 12, 17, 25, 30, 41, 43,
        9, 11, 18, 24, 31, 40, 44, 53,
        10, 19, 23, 32, 39, 45, 52, 54,
        20, 22, 33, 38, 46, 51, 55, 60,
        21, 34, 37, 47, 50, 56, 59, 61,
        35, 36, 48, 49, 57, 58, 62, 63
    };
    int scanNum;
    ComponentSpec Comp[];
    int chunkData[][][] = new int[10][4][64];
    int x = 0,  y = 0;
    int oriW, oriH;
    int[] quTab[] = new int[10][];
    int Qtable[][] = new int[4][64]; 
    short[] dcT[] = new short[10][];
    short[] acT[] = new short[10][];
    int markNum;
    int markNumI = 0;
    int blockNum[] = new int[10];
    int FFDDnum = 0;
    short HuffmanData[][][] = new short[4][2][50 * 256];
    
    public DecoderStream(File_JPEG_To_Image p) {
        par = p;
    }

    void close() {
        quTab = null;
        dcT = null;
        acT = null;
        blockNum = null;
        chunkData = null;
        DCTable = null;
        Init_DCTable = null;
        JPEG_table = null;
        HuffmanData = null;
        for(int a=0;a<Comp.length;a++){
            Comp[a] = null;
        }
        Comp = null;
        Qtable = null;
        Y_D = null;
        U_D = null;
        V_D = null;
        jpgChunk = null;
        try {
            baos.close();
        } catch (Exception e) {
        }
        baos = null;
    }

    int getByte(byte[] in){
        try {
//            byte tba[] = new byte[1];
//            in.read(tba);
            int ti = 0;
            ti = ti | in[readIndex++];
            ti = ti & 0x000000ff;
            return ti;
        } catch (Exception e) {
            return -1;
        }
    }

    int get2Byte(byte[] in){
        int temp = 0;
        try {
//            byte tba[] = new byte[2];
            
//            in.read(tba);
            temp = temp | in[readIndex++];
            temp = temp & 0x000000ff;
            temp <<= 8;
            int ti = 0;
            ti = ti | in[readIndex++];
            ti = ti & 0x000000ff;
            temp = temp | ti;
            return temp;
        } catch (Exception e) {
            return -1;
        }
    }

    int getNum(byte[] in){
        try {
            readIndex += 2;
//            in.skip(2);
            return get2Byte(in);
        }catch (Exception e) {
            return -1;
        }
    }
    
    void skip(byte[] in, int num){
        try{
            readIndex += num;
//            in.skip(num);
        }catch(Exception e){}
    }
    
    void skipString(byte[] in){
        int len, count = 0;
        len = get2Byte(in);
        count += 2;
        for (; count < len; count++) {
            getByte(in);
        }
    }

    void skipChunk(byte[] in){
        int len = get2Byte(in);
        int count = 2;
        while (count < len) {
            getByte(in);
            count++;
        }
    }
    
    void readHuffmanTable(byte[] in){
        int count = 0;
        int len;
        int Tc[][] = new int[4][2];
        int Th[] = new int[4];     
        int i, j, temp, t, c;
        len = get2Byte(in);
        count += 2;
        while (count < len) {
            temp = getByte(in);
            count++;
            t = temp & 0x0F;
            c = temp >> 4;
            Th[t] = 1;
            Tc[t][c] = 1;
            int tL[] = new int[16];
            int tV[][] = new int[16][200];
            for (i = 0; i < 16; i++) {
                tL[i] = getByte(in);
                count++;
            }
            for (i = 0; i < 16; i++) {
                for (j = 0; j < tL[i]; j++) {
                    tV[i][j] = getByte(in);
                    count++;
                }
            }
            if (Tc[t][c] != 0) {
                setHT(HuffmanData[t][c], tL, tV);
            }
        }
    }
    
    void setHT(short tab[], int L[], int V[][]){
        short MASK = (short) 0x8000;
        int temp = 256;
        int tCount = 0;
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < L[i]; j++) {
                for (int n = 0; n < (temp >> (i + 1)); n++) {
                    tab[tCount] = (short) (V[i][j] | ((i + 1) << 8));
                    tCount++;

⌨️ 快捷键说明

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