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

📄 file_image_to_jpg.java

📁 基于J2ME的JPEG decode和encode
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

			data[dataOff+ 0] = tmp10 + tmp11; /* phase 3 */
			data[dataOff+32] = tmp10 - tmp11;

			z1 = (tmp12 + tmp13) * 7071 / 10000; /* c4 */  //sure: 0.707106781
			data[dataOff+16] = tmp13 + z1; /* phase 5 */
			data[dataOff+48] = tmp13 - z1;

			/* Odd part */
			tmp10 = tmp4 + tmp5; /* phase 2 */
			tmp11 = tmp5 + tmp6;
			tmp12 = tmp6 + tmp7;

			/* The rotator is modified from fig 4-8 to avoid extra negations. */
			z5 = (tmp10 - tmp12) * 3827 / 10000; /* c6 */  //sure: 0.382683433
			z2 = 5412 * tmp10 / 10000 + z5; /* c2-c6 */  //sure: 0.541196100
			z4 = 13066 * tmp12 / 10000 + z5; /* c2+c6 */  //sure: 1.306562965
			z3= tmp11 * 7071 / 10000; /* c4 */  //sure: 0.707106781

			z11 = tmp7 + z3;	/* phase 5 */
			z13 = tmp7 - z3;

			data[dataOff+40] = z13 + z2; /* phase 6 */
			data[dataOff+24] = z13 - z2;
			data[dataOff+ 8] = z11 + z4;
			data[dataOff+56] = z11 - z4;

			dataOff++; /* advance pointer to next column */
		}
                long temp;
		// Quantize/descale the coefficients
		for (int i=0; i<64; i++) {
			// Apply the quantization and scaling factor & Round to nearest integer
			temp = (data[i]*fdtbl[i]);
                        data[i] = (int)(temp/10000);
			if(temp%10000 >= 5000) data[i] ++;
			//data[i] = (int)((data[i]*fdtbl[i])/10000);
		}
		return data;
	}

	// Chunk writing

	private void writeAPP0() {
		writeWord(0xFFE0); // marker
		writeWord(16); // length
		writeByte(0x4A); // J
		writeByte(0x46); // F
		writeByte(0x49); // I
		writeByte(0x46); // F
		writeByte(0); // = "JFIF",'\0'
		writeByte(1); // versionhi
		writeByte(1); // versionlo
		writeByte(0); // xyunits
		writeWord(1); // xdensity
		writeWord(1); // ydensity
		writeByte(0); // thumbnwidth
		writeByte(0); // thumbnheight
	} 
        
        private void writeAPP1(int width, int height, boolean needAddThumbnail) {
            try{
                byte Exif[] = {
                    0x45, 0x78, 0x69, 0x66, 0x00, 0x00, 0x49, 0x49, 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00,     //Exif00II*0800080
                    0x08, 0x00
                };
                byte entriesIFD0[] = {0x13, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,   //YCbCr-Positioning
                                  0x1A, 0x01, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x6E, 0x00, 0x00, 0x00,       //x-Resolution
                                  0x1B, 0x01, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x76, 0x00, 0x00, 0x00,       //y-Resolution
                                  0x28, 0x01, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,       //Resolution Unit
                                  0x32, 0x01, 0x02, 0x00, 0x14, 0x00, 0x00, 0x00, 0x7E, 0x00, 0x00, 0x00,       //Date Modify
                                  0x0F, 0x01, 0x02, 0x00, 0x14, 0x00, 0x00, 0x00, (byte)0x92, 0x00, 0x00, 0x00, //Make
                                  0x10, 0x01, 0x02, 0x00, 0x14, 0x00, 0x00, 0x00, (byte)0xA6, 0x00, 0x00, 0x00, //Model
                                  0x69, (byte)0x87, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, (byte)0xBA, 0x00, 0x00, 0x00,   //End
                };
                byte IFDoffset[] = new byte[4];  //(Need to Modify) IFDoffset
                if(needAddThumbnail){
                    IFDoffset[0] = 0x08;
                    IFDoffset[1] = 0x01;
                    IFDoffset[2] = 0x00;
                    IFDoffset[3] = 0x00;
                }
                byte valueIFD0[] = {0x48, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,             //x-Resolution  8bytes
                                  0x48, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,               //y-Resolution  8bytes
                                  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   //modify Date 20bytes   
                                  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   //(Need to Modify) 
                                  0x41, 0x76, 0x61, 0x6E, 0x71, 0x75, 0x65, 0x73, 0x74, 0x20,   //Make  Avanquest SPM
                                  0x53, 0x50, 0x4D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                  0x41, 0x76, 0x61, 0x6E, 0x71, 0x75, 0x65, 0x73, 0x74, 0x20,   //Model Avanquest SENDPHOTOS
                                  0x53, 0x45, 0x4E, 0x44, 0x50, 0x48, 0x4F, 0x54, 0x4F, 0x53,
                };
                Date tim = new Date(System.currentTimeMillis());
                String curTime[] = Main_Canvas.splitString(tim.toString(), " ");
                String tM[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
		for(int a=0;a<tM.length;a++){
			if(tM[a].equals(curTime[1])){
				curTime[1] = (a<9?"0":"")+(a+1);
			}
		}
                byte timeMark[] = (curTime[5]+":"+curTime[1]+":"+curTime[2]+" "+curTime[3]+" ").getBytes();
                for(int a=0;a<timeMark.length;a++){
                    valueIFD0[16+a] = timeMark[a];
                }
                timeMark = null;
                curTime = null;
                tim = null;
                byte entriesIFD1[] = {
                    0x06, 0x00,                 //6 entries
                    0x00, (byte)0x90, 0x07, 0x00, 0x04, 0x00, 0x00, 0x00, 0x30, 0x32, 0x32, 0x30, //Exif version = '0220'
                    0x00, (byte)0xA0, 0x07, 0x00, 0x04, 0x00, 0x00, 0x00, 0x30, 0x30, 0x31, 0x30, //FlashPix version = '0010'
                    0x01, (byte)0xA0, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, //Color space = 'sRGB'
                    0x01, (byte)0x91, 0x07, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x00, //Compnents configuration = 'YCbCr'
                    0x02, (byte)0xA0, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //Image width (Need to Modify) 
                    0x03, (byte)0xA0, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //Image height (Need to Modify) 
                    0x00, 0x00, 0x00, 0x00,
                };
                for(int a=0;a<4;a++){
                    entriesIFD1[58+a] = (byte)(width & 0x000000ff);
                    width = width >>> 8;
                }
                for(int a=0;a<4;a++){
                    entriesIFD1[70+a] = (byte)(height & 0x000000ff);
                    height = height >>> 8;
                }
                byte entriesIFD[] = {
                    0x05, 0x00,                 //5 entries
                    0x1A, 0x01, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x4A, 0x01, 0x00, 0x00, //x-Resolution
                    0x1B, 0x01, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x52, 0x01, 0x00, 0x00, //y-Resolution
                    0x28, 0x01, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, //Resolution Unit
                    0x01, 0x02, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x5A, 0x01, 0x00, 0x00, //Thumbnail offset
                    0x02, 0x02, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //Thumbnail size    (Need to Modify)
                    0x00, 0x00, 0x00, 0x00,
                    0x48, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,                         //x-Resolution  8bytes
                    0x48, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,                         //y-Resolution  8bytes
                };
                if(needAddThumbnail){
                    int thumbnailLen = this.thumbnail.length;
                    for(int a=0;a<4;a++){
                        entriesIFD[58+a] = (byte)(thumbnailLen & 0x000000ff);
                        thumbnailLen = thumbnailLen >>> 8;
                    }
                }
                writeWord(0xFFE1); // JPEG APP1 HEAD
                int app1Size = Exif.length+entriesIFD0.length+IFDoffset.length+valueIFD0.length+entriesIFD1.length;
                if(needAddThumbnail){
                    app1Size += entriesIFD.length+thumbnail.length;
                }
                app1Size += 2;
		writeWord(app1Size); // APP1 length
                writeByte(Exif);
                writeByte(entriesIFD0);
                writeByte(IFDoffset);
                writeByte(valueIFD0);
                writeByte(entriesIFD1);
                if(needAddThumbnail){
                    writeByte(entriesIFD);
                    writeByte(thumbnail);
                }
                Exif = null;
                entriesIFD0 = null;
                IFDoffset = null;
                valueIFD0 = null;
                curTime = null;
                tM = null;
                tim = null;
                entriesIFD1 = null;
                entriesIFD = null;
            }catch(Exception e){
                
            }
	} 

	private void writeSOF0(int width, int height) {
		writeWord(0xFFC0); // marker
		writeWord(17);   // length, truecolor YUV JPG
		writeByte(8);    // precision
		writeWord(height);
		writeWord(width);
		writeByte(3);    // nrofcomponents
		writeByte(1);    // IdY
		writeByte(0x11); // HVY
		writeByte(0);    // QTY
		writeByte(2);    // IdU
		writeByte(0x11); // HVU
		writeByte(1);    // QTU
		writeByte(3);    // IdV
		writeByte(0x11); // HVV
		writeByte(1);    // QTV
	}

	private void writeDQT() {
		writeWord(0xFFDB); // marker
		writeWord(132);	   // length
		writeByte(0);
		for (int i=0; i<64; i++) {
			writeByte((byte)YTable[i]);
		}
		writeByte(1);
		for (int i=0; i<64; i++) {
			writeByte((byte)UVTable[i]);
		}
	}

	private void writeDHT() {
		writeWord(0xFFC4); // marker
		writeWord(0x01A2); // length

		writeByte(0); // HTYDCinfo
		for (int i=0; i<16; i++) {
			writeByte(std_dc_luminance_nrcodes[i+1]);
		}
		for (int i=0; i<=11; i++) {
			writeByte(std_dc_luminance_values[i]);
		}

		writeByte(0x10); // HTYACinfo
		for (int i=0; i<16; i++) {
			writeByte(std_ac_luminance_nrcodes[i+1]);
		}
		for (int i=0; i<=161; i++) {
			writeByte(std_ac_luminance_values[i]);
		}

		writeByte(1); // HTUDCinfo
		for (int i=0; i<16; i++) {
			writeByte(std_dc_chrominance_nrcodes[i+1]);
		}
		for (int i=0; i<=11; i++) {
			writeByte(std_dc_chrominance_values[i]);
		}

		writeByte(0x11); // HTUACinfo
		for (int i=0; i<16; i++) {
			writeByte(std_ac_chrominance_nrcodes[i+1]);
		}
		for (int i=0; i<=161; i++) {
			writeByte(std_ac_chrominance_values[i]);
		}
	}

	private void writeSOS() {
		writeWord(0xFFDA); // marker
		writeWord(12); // length
		writeByte(3); // nrofcomponents
		writeByte(1); // IdY
		writeByte(0); // HTY
		writeByte(2); // IdU
		writeByte(0x11); // HTU
		writeByte(3); // IdV
		writeByte(0x11); // HTV
		writeByte(0); // Ss
		writeByte(0x3f); // Se
		writeByte(0); // Bf
	}

	// Core processing
	int DU[] = new int[64];
        
	private int processDU(int CDU[], int fdtbl[], int DC, BitString HTDC[], BitString HTAC[]) {
		try {
			int DU_DCT[] = fDCTQuant(CDU, fdtbl);
			//ZigZag reorder
			for (int i=0;i<64;i++) {
				DU[ZigZag[i]]=DU_DCT[i];
			}
			int Diff = DU[0] - DC; DC = DU[0];
			//Encode DC
			if (Diff==0) {
				writeBits(HTDC[0]); // Diff might be 0
			} else {
				writeBits(HTDC[category[32767+Diff]]);
				BitString t = new BitString();
				t.len = bitcode_len[32767+Diff];
				t.val = bitcode_val[32767+Diff];
				writeBits(t);//bitcode[32767+Diff];
			}
			//Encode ACs
			int end0pos = 63;
			for (; (end0pos>0)&&(DU[end0pos]==0); end0pos--) {
			};
			//end0pos = first element in reverse order !=0
			if (end0pos == 0) {
				writeBits(HTAC[0x00]);
				return DC;
			}
			int i = 1;
			while ( i <= end0pos ) {
				int startpos = i;
				for (; (DU[i]==0) && (i<=end0pos); i++) {
				}
				int nrzeroes = i-startpos;
				if ( nrzeroes >= 16 ) {
					for (int nrmarker=1; nrmarker <= nrzeroes/16; nrmarker++) {
						writeBits(HTAC[0xF0]);
					}
					nrzeroes = (int)(nrzeroes&0xF);
				}
				writeBits(HTAC[nrzeroes*16+category[32767+DU[i]]]);
				BitString t = new BitString();
				t.len = bitcode_len[32767+DU[i]];
				t.val = bitcode_val[32767+DU[i]];
				writeBits(t);//bitcode[32767+DU[i]]
				i++;
			}
			if ( end0pos != 63 ) {
				writeBits(HTAC[0x00]);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			System.out.println("processDU "+e);
			//e.printStackTrace();
		}
		return DC;
	}
        
//        private int processDU(int CDU[], int fdtbl[], int DC, BitString HTDC[], BitString HTAC[]y){
//                int DU_DCT[] = fDCTQuant(CDU, fdtbl);
//                //ZigZag reorder
//                for (int i=0;i<64;i++) {
//                        DU[ZigZag[i]]=DU_DCT[i];
//                }
//                int Diff = DU[0] - DC; DC = DU[0];
//                //Encode DC
//                if (Diff==0) {
//                        writeBits(HTDC[0]); // Diff might be 0
//                } else {
//                        writeBits(HTDC[category[32767+Diff]]);
//                        writeBits(bitcode[32767+Diff]);
//                }
//                //Encode ACs
//                var end0pos:int = 63;
//                for (; (end0pos>0)&&(DU[end0pos]==0); end0pos--) {
//                };
//                //end0pos = first element in reverse order !=0
//                if ( end0pos == 0) {
//                        writeBits(HTAC[0x00]);
//                        return DC;
//                }
//                var i:int = 1;
//                while ( i <= end0pos ) {
//                        var startpos:int = i;
//                        for (; (DU[i]==0) && (i<=end0pos); i++) {
//                        }
//                        var nrzeroes:int = i-startpos;
//                        if ( nrzeroes >= 16 ) {
//                                for (var nrmarker:int=1; nrmarker <= nrzeroes/16; nrmarker++) {
//                                        writeBits(HTAC[0xF0]);
//                                }
//                                nrzeroes = int(nrzeroes&0xF);
//                        }
//                        writeBits(HTAC[nrzeroes*16+category[32767+DU[i]]]);
//                        writeBits(bitcode[32767+DU[i]]);
//                        i++;
//                }
//                if ( end0pos != 63 ) {
//                        writeBits(EOB);
//                }
//                return DC;
//        }
        
        int encodeProcessDU(ByteArrayOutputStream baos, int CDU[], int fdtbl[], int DC, BitString HTDC[], BitString HTAC[]){
            return encodeProcessDU(baos, CDU, fdtbl, DC, HTDC, HTAC, false);
        }
        int encodeProcessDU(ByteArrayOutputStream baos, int CDU[], int fdtbl[], int DC, BitString HTDC[], BitString HTAC[], boolean needToPrint) {
            try {
                int DU_DCT[] = fDCTQuant(CDU, fdtbl);
                //ZigZag reorder
                for (int i=0;i<64;i++) {
                        DU[ZigZag[i]]=DU_DCT[i];
                }
                int Diff = DU[0] - DC; DC = DU[0];
                //Encode DC
                if (Diff==0) {
                        writeBits(baos, HTDC[0],needToPrint); // Diff might be 0
                } else {
                        writeBits(baos, HTDC[category[32767+Diff]],needToPrint);
                        writeBits(baos, bitcode_val[32767+Diff],bitcode_len[32767+Diff]-1,needToPrint);//bitcode[32767+Diff];
                }
                //Encode ACs
                int end0pos = 63;
                for (; (end0pos>0)&&(DU[end0pos]==0); end0pos--) {
                };
                //end0pos = first element in reverse order !=0
                if (end0pos == 0) {
                        writeBits(baos, HTAC[0x00],needToPrint);
                        if(needToPrint){
                            System.out.println("");
                        }
                        return DC;
                }
                int i = 1;
                int startpos;
                int nrzeroes;
                while ( i <= end0pos ) {
                        startpos = i;
                        for (; (DU[i]==0) && (i<=end0pos); i++) {
                        }
                        nrzeroes = i-startpos;
                        if ( nrzeroes >= 16 ) {
                                for (int nrmarker=1; nrmarker <= nrzeroes/16; nrmarker++) {
                                        writeBits(baos, HTAC[0xF0],needToPrint);
                                }
                                nrzeroes = (int)(nrzeroes&0xF);
                        }

⌨️ 快捷键说明

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