The way the Android socket server and the client pass pictures to each other in strings _android

Source: Internet
Author: User

Send a Picture:

First find the specific transfer of the picture:

<span style= "Font-family:comic sans ms,sans-serif; font-size:16px; " >private Bitmap getimage (String srcpath) {bitmapfactory.options newopts = new Bitmapfactory.options ();//start reading the picture, at this point O
Ptions.injustdecodebounds set back to True newopts.injustdecodebounds = true;
Bitmap Bitmap = Bitmapfactory.decodefile (Srcpath, newopts);//This time return bm is empty newopts.injustdecodebounds = false;
int w = newopts.outwidth;
int h = newopts.outheight; Now the mainstream mobile phone is more 800*480 resolution, so high and wide we set for float HH = 100f;//here set height for 800f float ww = 100f;//here set width to 480f//zoom ratio. Because it is a fixed scaling scale, only one of the data is high or wide, the int is = 1;//Be=1 represents not scaling if (W > H && w > ww) {//If the width is large, scale the BE = (int) According to the Width fixed size (
NEWOPTS.OUTWIDTH/WW); else if (W < h && h > hh) {//if height is scaled to be = (int) (NEWOPTS.OUTHEIGHT/HH) based on width fixed size;} if (is <= 0) be
= 1; Newopts.insamplesize = be;//Set scaling//re-read the picture, note that the options.injustdecodebounds has been set back to false bitmap =
Bitmapfactory.decodefile (Srcpath, newopts); Return Compressimage (bitmap);//compress a good proportional size and then mass compress} &LT;/SPAN>  

The following method is a way to compress a picture

<span style= "Font-family:comic sans ms,sans-serif; font-size:px; " >private Bitmap compressimage (Bitmap image) {
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Image.compress (Bitmap.CompressFormat.JPEG,, BAOs);//mass compression method, where the compression is not compressed, the compressed data stored in the BAOs
int options =;
while (Baos.tobytearray (). Length/>) {//loops to determine if the picture is larger than KB after compression, greater than the continued compression
Baos.reset (), or reset BAOs is empty BAOs
Image.compress (Bitmap.CompressFormat.JPEG, Options, BAOs)//Here compressed options%, the compressed data stored in the BAOs options-
=;
}
Bytearrayinputstream ISBM = new Bytearrayinputstream (Baos.tobytearray ())//The compressed data BAOs stored in the Bytearrayinputstream
Bitmap Bitmap = bitmapfactory.decodestream (ISBM, NULL, NULL);//Turn bytearrayinputstream data to generate picture return
Bitmap;
}

Convert bitmap to byte[] Array

<span style= "Font-family:comic sans ms,sans-serif; font-size:16px; " >public byte[] Bitmap2bytes (Bitmap BM) {
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Bm.compress (Bitmap.CompressFormat.PNG, BAOs);
return Baos.tobytearray ();
}

Format byte As String

<span style= "Font-family:comic sans ms,sans-serif; font-size:px; " >/**
* Format byte
* 
* @param b
* @return/public
static String Bytehex (byte[] b) {
Char[] Digit = {', '
, ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ' Char[] out = new char[b.length *];
for (int i =; i < b.length; i++) {
byte c = b[i];
Out[i *] = digit[(c >>>) & XF];
Out[i * +] = digit[c & XF];
}
return new String (out);
</span>

Receive Picture:

First converts a string passed over into a byte[] array:

<span style= "Font-family:comic sans ms,sans-serif; font-size:px; " >/**
* Reverse format byte
* 
* @param s
* @return/public
static byte[] HexByte (String s) {
byte[ ] src = s.tolowercase (). GetBytes ();
byte[] ret = new byte[src.length/];
for (int i =; I < src.length i + =) {
byte hi = src[i];
byte low = Src[i +];
hi = (byte) (Hi >= ' a ' && hi <= ' f ')? Xa + (Hi-' a ')
: Hi-');
Low = (byte) (Low >= ' a ' && low <= ' f '), XA + (low-' a ')
: low-');
Ret[i/] = (byte) (Hi << | low);
return ret;
}

To convert byte[] into bitmap:

<span style= "Font-family:comic sans ms,sans-serif; font-size:px; " >public Bitmap Bytesbimap (byte[] b) {
if (b.length!=) {return
Bitmapfactory.decodebytearray (b, b.length );
} else {return
null;
}
}

Use the Setimagebitmap method in Android to display the received picture to the phone.

Related Article

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.