Send images to servlet (error correction) through)

Source: Internet
Author: User

SorrySend images to ServletIn that article, I ignored a detail and did not convert the data type. This will cause image pixel loss and distortion of the images sent to the server. Please forgive us for the inconvenience caused by updating the code again.

Public synchronized void connhttp (){
While (true ){
System. Out. println ("httpconn wait now ");
If (midplet. getissending ())){
System. Out. println ("Search ");
Try {
Con = (httpconnection) connector. Open (URL );
Con. setrequestmethod (httpconnection. post );
Con. setrequestproperty ("If-modified-since ",
"29 Oct 1999 19:43:31 GMT ");
Con. setrequestproperty ("User-Agent ",
"Profile/MIDP-2.0 configuration/CLDC-1.0 ");
Con. setrequestproperty ("content-language", "En-us ");

OS = new dataoutputstream (con. openoutputstream ());
Int [] rgbarg = new int [100*100];
Midplet. getimage (). getrgb (rgbarg, 0,100, 100,100 );
Byte [] bytes = inttobyte (rgbarg );
OS. Write (bytes );

// For (INT I = 0; I <rgbarg. length; I ++ ){
// OS. writeint (rgbarg [I]);
// System. Out. println (rgbarg [I]);
//}

OS. Flush ();

} Catch (exception e ){
System. Out. println ("conn Err ");
E. printstacktrace ();
} Finally {
If (OS! = NULL ){
Try {
OS. Close ();
Midplet. setissending (false );
Midplet. setissend (true );
} Catch (ioexception IOE ){};
}
}

 

 

Midplet. setissending (false );
Midplet. setissend (true );
}
Try {
Wait ();
} Catch (exception e ){
System. Out. println ("Wait exception ");
E. printstacktrace ();
}
}

}

Public synchronized void wakeup (){
Policyall ();
}
Private byte [] inttobyte (INT [] ints ){
Byte [] bytes = new byte [ints. length * 4];
For (INT I = 0; I <ints. length; I ++ ){
Bytes [I * 4] = (byte) (0xff & (ints [I] >>> 24 ));
Bytes [I * 4 + 1] = (byte) (0xff & (ints [I] >>> 16 ));
Bytes [I * 4 + 2] = (byte) (0xff & (ints [I] >>> 8 ));
Bytes [I * 4 + 3] = (byte) (0xff & (ints [I] >>> 0 ));
If (I <100 ){
System. Out. println (ints [I]);
}
}

Return bytes;
}

The client is more than the previous one. The inttobyte () method obtains an array of the byte type, which is directly sent to the server, restore the byte array to an int array.

========================================================== =
Private void createiamge (datainputstream Io, outputstream out ){
Int width = 100;
Int Height = 100;
Int [] RGB = new int [width * Height];
Try {
Int [] bytes = new int [RGB. length * 4];
// Io. readu (bytes );
// RGB = bytetoint (bytes );
For (INT I = 0; I <bytes. length; I ++ ){
Bytes [I] = Io. readunsignedbyte ();
}
Bytetoint (bytes, RGB );

} Catch (ioexception IOE) {IOE. printstacktrace ();}
Bufferedimage Bi = new bufferedimage (width, height, bufferedimage. type_int_rgb );
Bi. setrgb (100,100, 0,100, RGB );
Bi. Flush ();
// Encode:
Required imageencoder encoder = required codec. createjpegencoder (out );
Export encodeparam Param = encoder. getdefaultjpegencodeparam (BI );
Param. setquality (1.0f, false );
Encoder. setincluencodeparam (PARAM );
System. Out. println ("yeah ");
Try {
Encoder. encode (BI );
}
Catch (ioexception IOE ){
IOE. printstacktrace ();
}

}

Private void bytetoint (INT [] bytes, int [] RGB ){

For (INT I = 0; I <RGB. length; I ++ ){
RGB [I] = (
(Bytes [I * 4] & 0xff) <24) |
(Bytes [I * 4 + 1] & 0xff) <16) |
(Bytes [I * 4 + 2] & 0xff) <8) |
(Bytes [I * 4 + 3] & 0xff) <0 ));
// System. Out. Print (ints [I] + "/t" + (ints [I] <8) + "/t ");
If (I <100 ){

System. Out. println (RGB [I]);

}
}

}

The above is the server code, with emphasis on readunsignedbyte. Otherwise, it is difficult to restore the int type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.