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

📄 file_image_to_jpg.java

📁 基于J2ME的JPEG decode和encode
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                        writeBits(baos, HTAC[nrzeroes*16+category[32767+DU[i]]],needToPrint);
                        writeBits(baos, bitcode_val[32767+DU[i]],bitcode_len[32767+DU[i]]-1,needToPrint);//bitcode[32767+DU[i]]
                        i++;
                }
                if ( end0pos != 63 ) {
                        writeBits(baos, HTAC[0x00],needToPrint);
                }
                if(needToPrint){
                    System.out.println("");
                }
            } catch (Exception e) {
                    // TODO Auto-generated catch block
                    System.out.println("processDU "+e);
                    //e.printStackTrace();
            }
            return DC;
	}

	private int YDU[] = new int[64];
	private int UDU[] = new int[64];
	private int VDU[] = new int[64];

	private void RGB2YUV(int[] img, int xpos, int ypos, int width, int height)
	{
		int pos=0;
		for (int y=0; y<8; y++) {
			for (int x=0; x<8; x++) {
				try {
					int P = 0;
					if(xpos + x >= width) P = 0;
					else if(ypos + y >= height) P = 0;
					else P = img[(ypos+y)*width+xpos+x];
					int R = ((P>>>16)&0xFF);
					int G = ((P>>>8)&0xFF);
					int B = ((P	)&0xFF);
					YDU[pos]=((( 29900)*R+( 58700)*G+( 11400)*B))/100000-128;			//sure: -128
					UDU[pos]=(((-16874)*R+(-33126)*G+( 50000)*B))/100000;		//sure
					VDU[pos]=((( 50000)*R+(-41869)*G+(-8131)*B))/100000;		//sure
					
//					if(YDU[pos] < 0)YDU[pos] += 128;
//					if(UDU[pos] < 0)UDU[pos] += 128;
//					if(VDU[pos] < 0)VDU[pos] += 128;
//					if(YDU[pos] < 0)YDU[pos] *= -1;
//					if(UDU[pos] < 0)UDU[pos] *= -1;
//					if(VDU[pos] < 0)VDU[pos] *= -1;
					//System.out.println(YDU[pos]+" "+UDU[pos]+" "+VDU[pos]);
					pos++;
				} catch (RuntimeException e) {
					//System.out.println("x "+x+" y "+y+" img "+img.length+" index "+((ypos+y)*width+xpos+x)+": "+e);
					// TODO Auto-generated catch block
					//e.printStackTrace();
				}
			}
		}
	}
	
	private void RGB2YUV(short[] img, int xpos, int ypos, int width, int height)
	{
		int pos=0;
		for (int y=0; y<8; y++) {
			for (int x=0; x<8; x++) {
				try {
					int P = 0;
					if(xpos + x >= width) P = 0;
					else if(ypos + y >= height) P = 0;
					else P = img[(ypos+y)*width+xpos+x];
					
					byte red = (byte) (P >>> 11);
					red <<= 3;
					byte green = (byte) ((P << 5) >>> 10);
					green <<= 2;
					byte blue = (byte) ((P << 11) >>> 11);
					blue <<= 3;
					
					int R = (red &0xFF);
					int G = (green &0xFF);
					int B = (blue &0xFF);
					YDU[pos]=((( 29900)*R+( 58700)*G+( 11400)*B))/100000-128;			//sure: -128
					UDU[pos]=(((-16874)*R+(-33126)*G+( 50000)*B))/100000;		//sure
					VDU[pos]=((( 50000)*R+(-41869)*G+(-8131)*B))/100000;		//sure
					
//					if(YDU[pos] < 0)YDU[pos] += 128;
//					if(UDU[pos] < 0)UDU[pos] += 128;
//					if(VDU[pos] < 0)VDU[pos] += 128;
//					if(YDU[pos] < 0)YDU[pos] *= -1;
//					if(UDU[pos] < 0)UDU[pos] *= -1;
//					if(VDU[pos] < 0)VDU[pos] *= -1;
					//System.out.println(YDU[pos]+" "+UDU[pos]+" "+VDU[pos]);
					pos++;
				} catch (RuntimeException e) {
					System.out.println("short x "+x+" y "+y+" img "+img.length+" index "+((ypos+y)*width+xpos+x)+": "+e);
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public void JPEGEncoder(int quality) //:Number = 50
	{
		if (quality <= 0) {
			quality = 1;
		}
		if (quality > 100) {
			quality = 100;
		}
		int sf = 0;
		if (quality < 50) {
			sf = 5000 / quality;
		} else {
			sf = 200 - quality*2;
		}
		// Create tables
		//MainCanvas.printMemory("initJPEGEncoder start");
		initHuffmanTbl();
		//MainCanvas.printMemory("initHuffman");
		initCategoryNumber();
		//MainCanvas.printMemory("initCategoryNumber");
		initQuantTables(sf);
		//MainCanvas.printMemory("initQuantTables");
	}
        
        public void close(){
                ZigZag = null;
                YTable = null;
                UVTable = null;
                fdtbl_Y = null;
                fdtbl_UV = null;
                YDC_HT = null;
                UVDC_HT = null;
                YAC_HT = null;
                UVAC_HT = null;
                std_dc_luminance_nrcodes = null;
                std_dc_luminance_values = null;
                std_ac_luminance_nrcodes = null;
                std_ac_luminance_values = null;

                std_dc_chrominance_nrcodes = null;
                std_dc_chrominance_values = null;
                std_ac_chrominance_nrcodes = null;
                std_ac_chrominance_values = null;
                
                category = null;
                bitcode_len = null;
                bitcode_val = null;
                
                if(byteout != null){
                    try{
                        byteout.close();
                    }catch(Exception e){}
                    byteout = null;
                }
                DU = null;
                YDU = null;
                UDU = null;
                VDU = null;
                chunkData = null;
        }

	public byte[] encode(int[] image, int width, int height) {
		// Initialize bit writer
		//byteout = new ByteArray();
                byteout = null;
		byteout = new ByteArrayOutputStream();
		
		bytenew=0;
		bytepos=7;

		// Add JPEG headers
		writeWord(0xFFD8); // SOI
		writeAPP1(width, height, false);
		writeDQT();
		writeSOF0(width,height);
		writeDHT();
		writeSOS();
		// Encode 8x8 macroblocks
		int DCY=0;
		int DCU=0;
		int DCV=0;
		bytenew=0;
		bytepos=7;
		for (int ypos=0; ypos<height; ypos+=8) {
			for (int xpos=0; xpos<width; xpos+=8) {
				//System.out.println("image "+image.length+" width "+width+" height "+height+" xpos "+xpos+" ypos "+ypos);
				RGB2YUV(image, xpos, ypos, width, height);
				DCY = processDU(YDU, fdtbl_Y, DCY, YDC_HT, YAC_HT);
				DCU = processDU(UDU, fdtbl_UV, DCU, UVDC_HT, UVAC_HT);
				DCV = processDU(VDU, fdtbl_UV, DCV, UVDC_HT, UVAC_HT);
			}
		}
		//System.out.println("encode 2");
		// Do the bit alignment of the EOI marker
		if ( bytepos >= 0 ) {
			BitString fillbits = new BitString();
			fillbits.len = (byte)(bytepos+1);
			fillbits.val = (byte)((1<<(bytepos+1))-1);
			writeBits(fillbits);
		}
		//System.out.println("encode 3");
		writeWord(0xFFD9); //EOI
		image = null;
		System.gc();
		return byteout.toByteArray();
	}
        byte[] thumbnail = null;
        
	public byte[] encode(short[] image, int width, int height) {
            return encode(image, width, height, null);
        }
        
	public byte[] encode(short[] image, int width, int height, byte[] thumbnailD) {
		// Initialize bit writer
		//byteout = new ByteArray();
                byteout = null;
		byteout = new ByteArrayOutputStream();
		
		bytenew=0;
		bytepos=7;
                // Add JPEG headers
		writeWord(0xFFD8); // SOI
                if(thumbnailD != null){
                    thumbnail = thumbnailD;
                    writeAPP1(width, height, true);
                }else writeAPP1(width, height, false);
		writeDQT();
		writeSOF0(width,height);
		writeDHT();
		writeSOS();
		// Encode 8x8 macroblocks
		int DCY=0;
		int DCU=0;
		int DCV=0;
		bytenew=0;
		bytepos=7;
		for (int ypos=0; ypos<height; ypos+=8) {
			for (int xpos=0; xpos<width; xpos+=8) {
				//System.out.println("image "+image.length+" width "+width+" height "+height+" xpos "+xpos+" ypos "+ypos);
				RGB2YUV(image, xpos, ypos, width, height);
				DCY = processDU(YDU, fdtbl_Y, DCY, YDC_HT, YAC_HT);
				DCU = processDU(UDU, fdtbl_UV, DCU, UVDC_HT, UVAC_HT);
				DCV = processDU(VDU, fdtbl_UV, DCV, UVDC_HT, UVAC_HT);
			}
		}
		// Do the bit alignment of the EOI marker
		if ( bytepos >= 0 ) {
			BitString fillbits = new BitString();
			fillbits.len = (byte)(bytepos+1);
			fillbits.val = (byte)((1<<(bytepos+1))-1);
			writeBits(fillbits);
		}
//		 .println("encode 3");
		writeWord(0xFFD9); //EOI
		image = null;
		System.gc();
		return byteout.toByteArray();
	}
        
        int readyDCY;
        int readyDCU;
        int readyDCV;
        
        byte[][] chunkData; 
        
        public void readyEncode(int width, int height){
            readyEncode(width, height, null);
        }
        
        public void readyEncode(int width, int height, byte[] thumbnailD) {
		// Initialize bit writer
		//byteout = new ByteArray();
                int chunkNum = ((width/8)+((width%8)>0?1:0))*((height/8)+((height%8)>0?1:0));
                chunkData = new byte[chunkNum][];
                byteout = null;
		byteout = new ByteArrayOutputStream();
		
		bytenew=0;
		bytepos=7;
                // Add JPEG headers
		writeWord(0xFFD8); // SOI
                if(thumbnailD != null){
                    thumbnail = thumbnailD;
                    writeAPP1(width, height, true);
                }else writeAPP1(width, height, false);
		writeDQT();
		writeSOF0(width,height);
		writeDHT();
		writeSOS();
		// Encode 8x8 macroblocks
		bytenew=0;
		bytepos=7;
	}
        public void doEncode(ByteArrayOutputStream baos, int[] YDU, int[] UDU, int[] VDU, int DC_YUV[]){
                doEncode(baos, YDU, UDU, VDU, DC_YUV, false);
        }
        public void doEncode(ByteArrayOutputStream baos, int[] YDU, int[] UDU, int[] VDU, int DC_YUV[], boolean needPrint){
                bytenew = 0;
                bytepos = 7;
//                if(needPrint)System.out.print("YDU: ");
                DC_YUV[0] = encodeProcessDU(baos, YDU, fdtbl_Y, DC_YUV[0], YDC_HT, YAC_HT, needPrint);
//                if(needPrint)System.out.print("UDU: ");
                DC_YUV[1] = encodeProcessDU(baos, UDU, fdtbl_UV, DC_YUV[1], UVDC_HT, UVAC_HT, needPrint);
//                if(needPrint)System.out.print("VDU: ");
                DC_YUV[2] = encodeProcessDU(baos, VDU, fdtbl_UV, DC_YUV[2], UVDC_HT, UVAC_HT, needPrint);
                baos.write((byte) bytenew);
                baos.write((byte) bytepos);
        }
        
        public int[] getDC_YUV(int[] YDU, int[] UDU, int[] VDU){
                int tempYUV[] = new int[3];
                tempYUV[0] = fDCTQuant(YDU, fdtbl_Y)[0];
                tempYUV[1] = fDCTQuant(UDU, fdtbl_UV)[0];
                tempYUV[2] = fDCTQuant(VDU, fdtbl_UV)[0];
                return tempYUV;
        }
        
        public void chunkRotate(int ro, int width, int height){
            try{
                int bytebuf = 0;
                int bytebufpos = 7;
                int bytenew = 0;
                int bytepos = 7;
                int bottom = 0;
                for(int a=0;a<chunkData.length;a++){
                    Main_Canvas.rotateProcess = 70+(a*20/chunkData.length);
//                    if(a == 899 || a== 900){
//                        System.out.println(a+": "+Main_Canvas.rotateProcess);
//                    }
                    if(chunkData[a] == null && a > 0){
                        chunkData[a] = chunkData[a-1];
                    }
                        for(int b=0;b<chunkData[a].length-1;b++){
                            bytenew = chunkData[a][b];
                            if(b == chunkData[a].length-2){
                                bottom = chunkData[a][chunkData[a].length-1]+1;
                            }else bottom = 0;
                            bytepos = 7;
                            while(bytepos >= bottom){
                                int tx = (bytenew & (1 << bytepos));
                                if (tx != 0) {
                                        bytebuf |= (1 << bytebufpos);
                                }
                                bytepos--;
                                bytebufpos--;
                                if (bytebufpos < 0) {
                                        if (bytebuf == 0xFF && (bytepos >= bottom || (bytepos == bottom-1 && bottom > 0))) {
                                                writeByte((byte)(0xFF));
                                                writeByte(0);
//                                                if(a >= 75 && a <= 149){
//                                                    System.out.print((byte)(0xFF)+" ");
//                                                    System.out.print(0+" ");
//                                                }
                                        }
                                        else {
                                                writeByte((byte)bytebuf);
//                                                if(a >= 75 && a <= 149){
//                                                    System.out.print((byte)bytebuf+" ");
//                                                }
                                        }
                                        bytebufpos=7;
                                        bytebuf=0;
                                }
                            }
                            if(bytenew == -1){
                                if(bytebufpos != 7){
                                    b++;
                                }
                            }
                        }
                        
//                        if(a >= 75 && a <= 149){
//                            System.out.println("");
//                        }
                        if(a >0)chunkData[a-1] = null;
                    }
//                }
//                for(int a=0;a<chunkData.length;a++){
//                    if(chunkData[a] != null){
//                        writeByte(chunkData[a]);
//                        for(int b=0;b<chunkData[a].length;b++){
//                            System.out.print(chunkData[a][b]+" ");
//                        }
//                        System.out.println("");
//                    }else System.out.println("chunkData null: "+a);
//                }
            }catch(Exception e){
                System.out.println("Chunk Rotate: "+e);
            }
        }
        
        public byte[] endEncode(){
                // Do the bit alignment of the EOI marker
		if ( bytepos >= 0 ) {
			BitString fillbits = new BitString();
			fillbits.len = (byte)(bytepos+1);
			fillbits.val = (byte)((1<<(bytepos+1))-1);
			writeBits(fillbits);
		}
		writeWord(0xFFD9); //EOI
		System.gc();
                return byteout.toByteArray();
        }
}

⌨️ 快捷键说明

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