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

📄 file_jpeg_to_image.java

📁 基于J2ME的JPEG decode和encode
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.io.*;import javax.microedition.lcdui.Image;public class File_JPEG_To_Image {    Main_Canvas par;    DecoderThumbnail j = null;    // Implementation of DecodeListener     int width, height;    int desW, desH;            public File_JPEG_To_Image(Main_Canvas p, InputStream im) { // 1/8 solution        try {            par = p;            par.srcZoomBuf = null;            j = new DecoderThumbnail(this, im);        } catch (Exception ex) {            par.srcZoomBuf = null;        }    }    public void decode() {        if (j.in != null) {            j.refreshFastBuffer();            j.decode(j.fastBuffer);        }    }        public void getImageSize(int width, int height) {        this.width = width;        this.height = height;        par.pW = width / 8 + ((width % 8 == 0) ? 0 : 1);        par.pH = height / 8 + ((height % 8 == 0) ? 0 : 1);        this.desW = par.pW;        this.desH = par.pH;        par.srcZoomBuf = new short[desW * desH];    }    public void close() {        if (j != null) {            try {                j.close();            } catch (Exception e) {            }            j = null;        }    }}class DecoderThumbnail {    File_JPEG_To_Image par;    static int readIndex = 0;    static 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;    static ComponentSpec Comp[];    int chunkData[][][] = new int[3][4][64];    int x = 0, y = 0;    int oriW, oriH;    int[] quTab[] = new int[3][];    int Qtable[][] = new int[4][64];    short[] dcT[] = new short[3][];    short[] acT[] = new short[3][];    static int markNum;    static int markNumI = 0;    int blockNum[] = new int[3];    int FFDDnum = 0;    short HuffmanData[][][] = new short[4][2][50 * 256];    public DecoderThumbnail(File_JPEG_To_Image p, InputStream in) {        par = p;        this.in = in;    }    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;        this.fastBuffer = null;        if(in != null){            try{                in.close();            }catch(Exception e){}        }        in = null;    }    byte[] fastBuffer = new byte[51200];    InputStream in;    public void refreshFastBuffer(){        try{            in.read(fastBuffer);            readIndex = 0;        }catch(Exception e){}    }        int getByte(byte[] in){        try {            if(readIndex == fastBuffer.length){                refreshFastBuffer();            }            return in[readIndex++]& 0x000000ff;        } catch (Exception e) {            return -1;        }    }    int temp = 0;    int ti = 0;    int get2Byte(byte[] in){        try {            temp = getByte(in);            temp <<= 8;            ti = getByte(in);            temp = temp | ti;            return temp;        } catch (Exception e) {            return -1;        }    }    int getNum(byte[] in) {        try {            skip(in, 2);            return get2Byte(in);        } catch (Exception e) {            return -1;        }    }    void skip(byte[] in, int num) {        try {            readIndex += num;            if(readIndex >= fastBuffer.length){                int s = readIndex-fastBuffer.length+1;                refreshFastBuffer();                readIndex = s;            }        } catch (Exception e) {        }    }    void skipChunk(byte[] in) {        int len;        len = get2Byte(in);        skip(in, len-2);    }    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;        int tL[] = new int[16];        int tV[][] = new int[16][200];        while (count < len) {            temp = getByte(in);            count++;            t = temp & 0x0F;            c = temp >> 4;            Th[t] = 1;            Tc[t][c] = 1;            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++;                }                for (; j < 200; j++) {                    tV[i][j] = 0;                }            }            if (Tc[t][c] != 0) {                setHT(HuffmanData[t][c], tL, tV);            }        }        tV = null;        tL = null;    }    void setHT(short tab[], int L[], int V[][]) {        short MASK = (short) 0x8000;        int temp = 256;        int tCount = 0;        int i, j, n;        for (i = 0; i < 8; i++) {            for (j = 0; j < L[i]; j++) {                for (n = 0; n < (temp >> (i + 1)); n++) {                    tab[tCount] = (short) (V[i][j] | ((i + 1) << 8));                    tCount++;                }            }        }        for (i = 1; tCount < 256; i++, tCount++) {            tab[tCount] = (short) (i | MASK);        }        int curTable = 1;        tCount = 0;        for (i = 8; i < 16; i++) {            for (j = 0; j < L[i]; j++) {                for (n = 0; n < (temp >> (i - 7)); n++) {                    tab[curTable * 256 + tCount] = (short) (V[i][j] | ((i + 1) << 8));                    tCount++;                }                if (tCount >= 256) {                    if (tCount > 256) {                        return;                    }                    tCount = 0;                    curTable++;                }            }        }    }    static int code = 0,  input,  mask = 0xFFFF;    static int MASK = (short) 0x8000;    private int countHuffman(short table[], int temp[], int index[], byte[] in){        try {            if (index[0] < 8) {                temp[0] <<= 8;                readStep(in);                temp[0] |= input;            } else {                index[0] -= 8;            }            code = table[temp[0] >> index[0]] & 0x0000ffff;            if ((code & MASK) != 0) {                if (markNumI != 0) {                    markNumI = 0;                    return 0xFF00 | markNum;                }                temp[0] &= (mask >> (16 - index[0]));                temp[0] <<= 8;                readStep(in);                if (index[0] < markNumI) {                    markNumI = 0;                    return 0xFF00 | markNum;                }                temp[0] |= input;                code = table[(code & 0xFF) * 256 + (temp[0] >> index[0])] & 0x0000ffff;                index[0] += 8;            }            index[0] += 8 - (code >> 8);            if (index[0] < 0) {                return 0;            }                        temp[0] &= (mask >> (16 - index[0]));        } catch (Exception e) {//            System.out.println("countHuffman: "+e.toString());        }        return code & 0xFF;    }    void readQualityTable(byte[] in) {        int len;        int Pq[] = new int[4];        int Tq[] = new int[4];        int i, count = 0, temp, t;        len = get2Byte(in);        count += 2;        while (count < len) {            temp = getByte(in);            count++;            t = temp & 0x0F;            if (t > 3) {                return;            }            Pq[t] = temp >> 4;            if (Pq[t] == 0) {                Pq[t] = 8;            } else if (Pq[t] == 1) {                Pq[t] = 16;            } else {                return;            }            Tq[t] = 1;            if (Pq[t] == 8) {                for (i = 0; i < 64; i++) {                    Qtable[t][i] = getByte(in);                    count++;                }                countQuality(Qtable[t]);            } else {                for (i = 0; i < 64; i++) {                    Qtable[t][i] = get2Byte(in);                    count += 2;                }                countQuality(Qtable[t]);            }        }    }    void countQuality(int qt[]) {        int i;        for (i = 0; i < 8; i++) {            qt[JPEG_table[i]] *= 90;            qt[JPEG_table[8 + i]] *= 126;            qt[JPEG_table[16 + i]] *= 118;            qt[JPEG_table[24 + i]] *= 106;            qt[JPEG_table[32 + i]] *= 90;            qt[JPEG_table[40 + i]] *= 71;            qt[JPEG_table[48 + i]] *= 49;            qt[JPEG_table[56 + i]] *= 25;        }        for (i = 0; i < 8; i++) {            qt[JPEG_table[0 + 8 * i]] *= 90;            qt[JPEG_table[1 + 8 * i]] *= 126;            qt[JPEG_table[2 + 8 * i]] *= 118;            qt[JPEG_table[3 + 8 * i]] *= 106;            qt[JPEG_table[4 + 8 * i]] *= 90;            qt[JPEG_table[5 + 8 * i]] *= 71;            qt[JPEG_table[6 + 8 * i]] *= 49;            qt[JPEG_table[7 + 8 * i]] *= 25;        }        for (i = 0; i < 64; i++) {            qt[i] >>= 6;        }    }    void readStep(byte[] in) {        input = getByte(in);        if (input == 0xFF) {            markNum = getByte(in);            if (markNum != 0) {                markNumI = 9;            }        }    }    static int result, one = 1, n_one = -1;//    private int getBit(byte[] in, int n, int temp[], int index[]){//        result = 0; one = 1; n_one = -1;//        mask = 0xFFFF; //        input = 0;//        if (n == 0) {//            return 0;//        }//        index[0] -= n;//        if (index[0] >= 0) {//            if (index[0] < markNumI) {//                markNumI = 0;//                return (0xFF00 | markNum) << 8;//            }//            result = temp[0] >> index[0];//            temp[0] &= (mask >> (16 - index[0]));//        } else {//            temp[0] <<= 8;//            input = getByte(in);//            if (input == 0xFF) {//                markNum = getByte(in);//                if (markNum != 0) {//                    markNumI = 9;//                }//            }//            temp[0] |= input;//            index[0] += 8;//            if (index[0] < 0) {//                if (markNumI != 0) {//                    markNumI = 0;//                    return (0xFF00 | markNum) << 8;//                }//                temp[0] <<= 8;//                input = getByte(in);//                if (input == 0xFF) {//                    markNum = getByte(in);//                    if (markNum != 0) {//                        markNumI = 9;//                    }//                }//                temp[0] |= input;//                index[0] += 8;//            }//            if (index[0] < markNumI) {//                markNumI = 0;//                return (0xFF00 | markNum) << 8;//            }//            result = temp[0] >> index[0];//            temp[0] &= (mask >> (16 - index[0]));//        }//        if (result < (one << (n - 1))) {//            result += (n_one << n) + 1;//        }

⌨️ 快捷键说明

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