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

📄 file_jpegstream_to_image.java

📁 基于J2ME的JPEG decode和encode
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        }        return 0;    }    boolean tempPrintFlag = false;    int tempA[][] = new int[8][8];    int t0, t1, t2, t3;    int s0, s1, s2, s3, s4, s5, s6, s7;    int d0, d1, d2, d3, d4, d5, d6, d7;    int index = 0;    private void getDU(int chunkData[]) {        for (int i = 0; i < 8; i++) {            s0 = (DCTable[i] + DCTable[8 + i] + DCTable[16 + i] + DCTable[24 + i]);            s1 = DCTable[i] * 256 - DCTable[8 + i] * 256 + DCTable[16 + i] * 128 - DCTable[24 + i] * 640;            s2 = DCTable[i] * 256 - DCTable[8 + i] * 256 - DCTable[16 + i] * 128 + DCTable[24 + i] * 640;            s3 = (DCTable[i] + DCTable[8 + i] - DCTable[24 + i] - DCTable[16 + i]) * 256;            s4 = DCTable[32 + i] * 416 - DCTable[40 + i] * 352 - DCTable[48 + i] * 928 - DCTable[56 + i] * 160;            s5 = DCTable[32 + i] * 64 + DCTable[40 + i] * 160 + DCTable[48 + i] * 1120 + DCTable[56 + i] * 320;            s6 = DCTable[40 + i] * 224 - DCTable[48 + i] * 736 - DCTable[32 + i] * 448 - DCTable[56 + i] * 64;            s7 = (DCTable[32 + i] + DCTable[40 + i] + DCTable[48 + i] + DCTable[56 + i]);            tempA[0][i] = (s0 + s7 + (1 << 4)) >> 5;            tempA[1][i] = (s1 + s6 + (1 << 12)) >> 13;            tempA[2][i] = (s2 + s5 + (1 << 12)) >> 13;            tempA[3][i] = (s3 + s4 + (1 << 12)) >> 13;            tempA[4][i] = (s3 - s4 + (1 << 12)) >> 13;            tempA[5][i] = (s2 - s5 + (1 << 12)) >> 13;            tempA[6][i] = (s1 - s6 + (1 << 12)) >> 13;            tempA[7][i] = (s0 - s7 + (1 << 4)) >> 5;        }        index = 0;        for (int i = 0; i < 8; i++) {            s0 = (tempA[i][0] + tempA[i][1] + tempA[i][2] + tempA[i][3]);            s1 = tempA[i][0] * 256 - tempA[i][1] * 256 + tempA[i][2] * 128 - tempA[i][3] * 640;            s2 = tempA[i][0] * 256 - tempA[i][1] * 256 - tempA[i][2] * 128 + tempA[i][3] * 640;            s3 = (tempA[i][0] + tempA[i][1] - tempA[i][3] - tempA[i][2]) * 256;            s4 = tempA[i][4] * 416 - tempA[i][5] * 352 - tempA[i][6] * 928 - tempA[i][7] * 160;            s5 = tempA[i][4] * 64 + tempA[i][5] * 160 + tempA[i][6] * 1120 + tempA[i][7] * 320;            s6 = tempA[i][5] * 224 - tempA[i][6] * 736 - tempA[i][4] * 448 - tempA[i][7] * 64;            s7 = (tempA[i][4] + tempA[i][5] + tempA[i][6] + tempA[i][7]);            chunkData[index++] = (s0 + s7 + (1 << 4)) >> 5;            chunkData[index++] = (s1 + s6 + (1 << 12)) >> 13;            chunkData[index++] = (s2 + s5 + (1 << 12)) >> 13;            chunkData[index++] = (s3 + s4 + (1 << 12)) >> 13;            chunkData[index++] = (s3 - s4 + (1 << 12)) >> 13;            chunkData[index++] = (s2 - s5 + (1 << 12)) >> 13;            chunkData[index++] = (s1 - s6 + (1 << 12)) >> 13;            chunkData[index++] = (s0 - s7 + (1 << 4)) >> 5;        }    }    int m = 0;    public void decode(byte[] in){        readIndex = 0;        int current, /*m,*/ i, scan_num = 0, RST_num;        int PRED[] = new int[10];        if (in == null) {            return;        }        x = 0;        y = 0;        current = get2Byte(in);        if (current != 0xFFD8) {            return;        }        current = get2Byte(in);        while (current == 0xFFC4 || current == 0xFFCC || current >> 4 != 0x0FFC) {   //SOF 0~15            switch (current) {                case 0xFFC4:                      readHuffmanTable(in);                    break;                case 0xFFCC:                      return;                case 0xFFDB:                    readQualityTable(in);                    break;                case 0xFFDD:                    FFDDnum = getNum(in);                    break;                case 0xFFE0:                case 0xFFE1:                case 0xFFE2:                case 0xFFE3:                case 0xFFE4:                case 0xFFE5:                case 0xFFE6:                case 0xFFE7:                case 0xFFE8:                case 0xFFE9:                case 0xFFEA:                case 0xFFEB:                case 0xFFEC:                case 0xFFED:                case 0xFFEE:                case 0xFFEF:                case 0xFFFE:                    skipChunk(in);                    break;                default:                    if (current >> 8 != 0xFF) {                        return;                    }            }            current = get2Byte(in);        }        if (current < 0xFFC0 || current > 0xFFC7) {            return;        }                //read frame header        int ti, c;        skip(in, 2);        skip(in, 1);        oriH = get2Byte(in);        oriW = get2Byte(in);        int Nf = getByte(in);        Comp = new ComponentSpec[Nf + 1];        for (i = 0; i <= Nf; i++) {            Comp[i] = new ComponentSpec();        }        for (i = 1; i <= Nf; i++) {            c = getByte(in);            Comp[c].Cid = c;            ti = getByte(in);            Comp[c].smaplingH = ti >> 4;            Comp[c].samplingV = ti & 0x0F;            Comp[c].qTable = getByte(in);        }        //read frame header over                 current = get2Byte(in);        if(MIDlet_Template.vbia != null)MIDlet_Template.vbia.process = 15;        par.getImageSize(oriW, oriH);        do {            while (current != 0x0FFDA) {   //SOS                switch (current) {                    case 0xFFC4:  //DHT                        readHuffmanTable(in);                        break;                    case 0xFFCC:  //DAC                        return;                    case 0xFFDB:                        readQualityTable(in);                        break;                    case 0xFFDD:                        FFDDnum = getNum(in);                        break;                    case 0xFFE0:                    case 0xFFE1:                    case 0xFFE2:                    case 0xFFE3:                    case 0xFFE4:                    case 0xFFE5:                    case 0xFFE6:                    case 0xFFE7:                    case 0xFFE8:                    case 0xFFE9:                    case 0xFFEA:                    case 0xFFEB:                    case 0xFFEC:                    case 0xFFED:                    case 0xFFEE:                    case 0xFFEF:                    case 0xFFFE:                        skipChunk(in);                        break;                    default:                        if (current >> 8 != 0xFF) {                            return;                        }                }                current = get2Byte(in);            }                        //read scan JPEG_table            get2Byte(in); //len            scanNum = getByte(in);            int tb;            for (i = 0; i < scanNum; i++) {                int CompN = getByte(in);                quTab[i] = Qtable[Comp[CompN].qTable];                blockNum[i] = Comp[CompN].samplingV * Comp[CompN].smaplingH;                tb = getByte(in);                dcT[i] = HuffmanData[tb >> 4][0];                acT[i] = HuffmanData[tb & 0x0F][1];            }//            try{//                Qtable = null;//                JPEG_table = null;//                HuffmanData = null;////                System.gc();//                Thread.sleep(1000);//            }catch(Exception e){}            skip(in, 3);            //read scan JPEG_table over            //            if (par.fileImageToJPG != null) {//                int wChunkNum = oriW / 8 + (oriW % 8 > 0 ? 1 : 0);//                int HChunkNum = oriH / 8 + (oriH % 8 > 0 ? 1 : 0);//                jpgChunk = new JPGChunkData[wChunkNum * HChunkNum];//                for (int a = 0; a < jpgChunk.length; a++) {//                    jpgChunk[a] = new JPGChunkData();//                }//            }            if(MIDlet_Template.vbia != null){                int wChunkNum = oriW / 8 + (oriW % 8 > 0 ? 1 : 0);                int HChunkNum = oriH / 8 + (oriH % 8 > 0 ? 1 : 0);                MIDlet_Template.vbia.chunkNum = wChunkNum * HChunkNum;                MIDlet_Template.vbia.process = 20;            }            scan_num++;            m = 0;            int Mnum;            int temp[] = new int[1];  // to store remainded bits            int index[] = new int[1];            for (RST_num = 0;; RST_num++) {  //Decode one scan                Mnum = 0;                temp[0] = 0;                index[0] = 0;                for (i = 0; i < 10; i++) {                    PRED[i] = 0;                }                if (FFDDnum == 0) {                    current = getDUArray(in, PRED, temp, index);                    while (current == 0) {                        m++;                        outputPixel();                        current = getDUArray(in, PRED, temp, index);                    }                    break;  //current=MARKER                }                for (Mnum = 0; Mnum < FFDDnum; Mnum++) {                    current = getDUArray(in, PRED, temp, index);//                    if (par.needZoom) {                        outputPixel();//                    } else {//                        outputPixelThumbnail();//                    }                    if (current != 0) {                        break;                    }                }                if (current == 0) {                    if (markNumI != 0) {                        current = (0xFF00 | markNum);                        markNumI = 0;                    } else {                        current = get2Byte(in);                    }                }                if (current >= 0xFFD0 && current <= 0xFFD7) {                } else {                    break;                } //current=MARKER            }            if (current == 0xFFDC && scan_num == 1) { //DNL                getNum(in);                current = get2Byte(in);            }        } while (current != 0xFFD9);//     ByteArrayOutputStream baos = new ByteArrayOutputStream();//     int tempyuv[] = new int[3];//     for(int a=0; a<jpgChunk.length; a++){//         if(a== 0 || a == 1){//             System.out.print("Y_D: ");//             for(int j=0;j<64;j++){//                 System.out.print(jpgChunk[a].Y_D[j]+" ");//             }//             System.out.println("");//             System.out.print("U_D: ");//             for(int j=0;j<64;j++){//                 System.out.print(jpgChunk[a].U_D[j]+" ");//             }//             System.out.println("");//             System.out.print("V_D: ");//             for(int j=0;j<64;j++){//                 System.out.print(jpgChunk[a].V_D[j]+" ");//             }//             System.out.println("");//             System.out.print("last DC_YUV: ");//             for(int j=0;j<3;j++){//                 System.out.print(tempyuv[j]+" ");//             }//             System.out.println("");//         }//         par.fileImageToJPG.doEncode(baos, jpgChunk[a].Y_D, jpgChunk[a].U_D, jpgChunk[a].V_D, tempyuv, (a==1?true:false));//         if(a== 0 || a == 1){//             System.out.print("DC_YUV: ");//             for(int j=0;j<3;j++){//                 System.out.print(tempyuv[j]+" ");//             }//             System.out.println("");//         }//         if(a < par.fileImageToJPG.chunkData.length){//             par.fileImageToJPG.chunkData[a] = baos.toByteArray();//             if(a== 0 || a == 1){//                 System.out.print("chunkData: ");//                 for(int j=0;j<par.fileImageToJPG.chunkData[a].length;j++){//                     System.out.print(par.fileImageToJPG.chunkData[a][j]+" ");//                 }//                 System.out.println("");//             }//             baos.reset();//         }//         if(a < jpgChunk.length-1){//             for(int b=0;b<3;b++){//                 tempyuv[b] = jpgChunk[a].DC_YUV[b];//             }//         }//     }//    for(int a = 0;a<testInt.length;a++){//        System.out.print("("+a+"): ");//        for(int b=0;b<3;b++){//            System.out.print(testInt[a][b]+" ");//        }//        System.out.println("");//    }//    for(int j=0;j<par.fileImageToJPG.chunkData.length;j++){//        System.out.print("j: ");//        for(int t=0;t<par.fileImageToJPG.chunkData[j].length;t++){//            System.out.print(par.fileImageToJPG.chunkData[j][t]+" ");//        }//        System.out.println("");//    }//    System.out.println("chunkCount = "+chunkCount);    }}

⌨️ 快捷键说明

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