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

📄 file_jpegstream_to_image.java

📁 基于J2ME的JPEG decode和encode
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.io.*;import javax.microedition.lcdui.Image;public class File_JPEGStream_To_Image{    DecoderThumbnail j = null;    // Implementation of DecodeListener     int width, height;    boolean needZoom = false;    boolean lostTrue = false;    int desW, desH;    short[][] tabY;    short[][] tabX;        public File_JPEGStream_To_Image(InputStream im) { // 1/8 solution        try {            MIDlet_Template.srcZoomBuf = null;            j = new DecoderThumbnail(this, im);        } catch (Exception ex) {            MIDlet_Template.srcZoomBuf = null;        }    }    public void decode(boolean needZoom, int desW, int desH, boolean lostTrue){        if (needZoom) {            this.desW = desW;            this.desH = desH;            this.needZoom = true;            this.lostTrue = lostTrue;        }        if (j.in != null) {            j.refreshFastBuffer();            j.decode(j.fastBuffer);        }    }//    public Image decodeThumbnail(){//        if (in != null) {//            j.decode(in);//        }//        return Image.createRGBImage(thumbnailData, width, height, false);//    }    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 {                    desW = width;                    desH = height;                }            }            MIDlet_Template.pW = desW;            MIDlet_Template.pH = desH;            MIDlet_Template.srcZoomBuf = new short[desW * desH];            short[] tY = new short[desH];            short[] tX = 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++) {                tY[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++) {                tX[db] = sb;                tems += width;                temd += desW;                if (tems > distance) {                    tems -= distance;                    sb++;                }                if (temd > distance) {                    temd -= distance;                    db++;                }            }                        tabY = new short[height][];            tabX = new short[width][];            int tI = -1;            for (int a = 0; a < tY.length; a++) {                tI = tY[a];                for(int b=a;b<tY.length;b++){                    if(tY[b] != tI){                        tabY[tI] = new short[b-a];                        for(int c=0;c<b-a;c++){                            tabY[tI][c] = (short)(a+c);                        }                        a = b-1;                        break;                    }else if(b == (tY.length-1)){                        tabY[tI] = new short[b-a+1];                        for(int c=0;c<b-a+1;c++){                            tabY[tI][c] = (short)(a+c);                        }                    }                }            }            tI = -1;            for (int a = 0; a < tX.length; a++) {                tI = tX[a];                for(int b=a;b<tX.length;b++){                    if(tX[b] != tI){                        tabX[tI] = new short[b-a];                        for(int c=0;c<b-a;c++){                            tabX[tI][c] = (short)(a+c);                        }                        a = b-1;                        break;                    }else if(b == (tX.length-1)){                        tabX[tI] = new short[b-a+1];                        for(int c=0;c<b-a+1;c++){                            tabX[tI][c] = (short)(a+c);                        }                    }                }            }            tY = null;            tX = null;        } else {        }    }        public void close() {        if(j != null){            try{                j.close();            }catch(Exception e){}            j = null;        }    }}class DecoderThumbnail {    File_JPEGStream_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[3][10][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][];    int markNum;    int markNumI = 0;    int blockNum[] = new int[10];    int FFDDnum = 0;    short HuffmanData[][][] = new short[4][2][50 * 256];    byte[] fastBuffer = new byte[51200];    InputStream in;        public DecoderThumbnail(File_JPEGStream_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;        try{            if(in != null){                in.close();            }        }catch(Exception e){}        in = null;        fastBuffer = null;//        jpgChunk = null;//        try {//            baos.close();//        } catch (Exception e) {//        }//        baos = null;    }        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;        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++) {

⌨️ 快捷键说明

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