Send Picture:
First, find the specific picture:
private Bitmap getimage (String srcpath) { Bitmapfactory.options newopts = new Bitmapfactory.options ();//start reading the picture, this time options.injustdecodebounds Set back to True newopts.injustdecodebounds = true; Bitmap Bitmap = Bitmapfactory.decodefile (Srcpath, newopts);//return BM At this time is empty newopts.injustdecodebounds = false;int W = newopts . outwidth;int h = newopts.outheight;//Now the mainstream phone is more 800*480 resolution, so the height and width we set to float hh = 100f;//Here Set the height of 800ffloat ww = 100f;//here Set Set width to 480f//zoom ratio. Because it is a fixed scale, only one of the data of high or wide can be calculated by the int be = 1;//be=1 means not to scale if (W > H && w > ww) {//If the width is large then scale be = (int) According to the fixed width size OPTS.OUTWIDTH/WW);} else if (W < h && h > hh) {//If height is high, scale is = (int) (NEWOPTS.OUTHEIGHT/HH) According to the fixed width size;} if (be <= 0) is = 1;newopts.insamplesize = be;//Set scaling//Reread the picture, note that the options.injustdecodebounds has been set back to false bitmap = Bitm Apfactory.decodefile (Srcpath, newopts); return Compressimage (bitmap);//compress good proportions and then mass compress}
The following method is a way to compress a picture
Private Bitmap Compressimage (Bitmap image) {Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); Image.compress ( Bitmap.CompressFormat.JPEG, BAOs);//Quality compression method, here 100 means no compression, the compressed data stored in the BAOs int options = 100;while (Baos.tobytearray ( ). length/1024 > 100) {//cycle to determine if the image is larger than 100kb after compression, the compression is greater than the baos.reset ();//Reset BAOs is empty baosimage.compress ( Bitmap.CompressFormat.JPEG, Options, BAOs);//compress options% here, store compressed data into BAOs options-= 10;//reduce each time by 10} Bytearrayinputstream ISBM = new Bytearrayinputstream (Baos.tobytearray ());// The compressed data BAOs stored in the bytearrayinputstream bitmap bitmap = Bitmapfactory.decodestream (ISBM, NULL, NULL);// Generate a picture of the Bytearrayinputstream data return bitmap;}
Convert bitmap to byte[] Array
Public byte[] Bitmap2bytes (Bitmap BM) {Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); Bm.compress ( Bitmap.CompressFormat.PNG, BAOs); return Baos.tobytearray ();}
Format byte into a string
/** * Format byte * * @param b * @return */public static String Byte2hex (byte[] b) {char[] Digit = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};char[] out = new char[b.length * 2];FOR (int i = 0; i < B.le Ngth; i++) {byte c = b[i];out[i * 2] = digit[(c >>> 4) & 0x0f];out[i * 2 + 1] = digit[c & 0X0F];} return new String (out);}
Receive pictures:
First convert the passed string into an byte[] array:
/** * Reformat byte * * @param s * @return */public static byte[] Hex2byte (String s) {byte[] src = s.tolowercase (). getBytes (); byte[] ret = new Byte[src.length/2];for (int i = 0; i < src.length; i + = 2) {byte hi = src[i];byte low = src[i + 1 ];hi = (byte) ((Hi >= ' a ' && hi <= ' f ')? 0x0a + (Hi-' a '): Hi-' 0 '); low = (byte) (Low >= ' a ' &&am P Low <= ' F ')? 0x0a + (Low-' a '): Low-' 0 '), RET[I/2] = (byte) (Hi << 4 | low);} return ret;}
Convert byte[] to bitmap:
Public Bitmap Bytes2bimap (byte[] b) {if (b.length! = 0) {return Bitmapfactory.decodebytearray (b, 0, b.length);} else {Retu RN null;}}
Using the Setimagebitmap method in Android, you can display the received picture to your phone.
Android-socket the server and the client pass the picture to each other in string form