Android Socket Method of Transferring Pictures Between Server and Client by String
- 2021-07-09 09:18:35
- OfStack
Send pictures:
First, find the specific transmitted 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 time, put options.inJustDecodeBounds Set back true It's over
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// Return at this time bm Empty
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
// Now there are more mainstream mobile phones 800*480 Resolution, so we set the height and width to
float hh = 100f;// Set the height here to 800f
float ww = 100f;// Here, the width is set to 480f
// Scaling ratio. Because it is a fixed scale, it is only high or wide 1 Data can be calculated
int be = 1;// be=1 Indicates non-scaling
if (w > h && w > ww) {// If the width is large, scale according to the fixed size of the width
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {// If the height is high, it will be scaled according to the fixed size of the width
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;// Set the scale
// Re-read the picture, notice that the options.inJustDecodeBounds Set back false It's over
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return compressImage(bitmap);// After compressing the proportion, mass compression is carried out
}
</span>
The following method is the method of compressing pictures
<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);// Quality compression method, which means not compressing, stores the compressed data to baos Medium
int options = ;
while (baos.toByteArray().length / > ) { // Loop to judge if the compressed picture is larger than kb, Greater than Continue Compression
baos.reset();// Reset baos Empty baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);// Compress here options% The compressed data is stored in the baos Medium
options -= ;// Decrease every time
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// Put the compressed data baos Deposit to ByteArrayInputStream Medium
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// Put ByteArrayInputStream Data generation picture
return bitmap;
}
</span>
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, 100, baos);
return baos.toByteArray();
}
</span>
Formatting byte into Strings
<span style="font-family: comic sans ms,sans-serif; font-size: px;">/**
* Formatting byte
*
* @param b
* @return
*/
public static String bytehex(byte[] b) {
char[] Digit = { '', '', '', '', '', '', '', '', '', '', 'A',
'B', 'C', 'D', 'E', 'F' };
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 pictures:
First convert the passed String into an byte [] array:
<span style="font-family: comic sans ms,sans-serif; font-size: px;">/**
* Inversion formatting 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;
}
</span>
Convert byte [] to 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;
}
}
</span>
Use the setImageBitmap method in android to display the received pictures to the mobile phone.