1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| private Camera.PreviewCallback mPreviewCallback = new Camera.PreviewCallback() {
@Override public void onPreviewFrame(byte[] data, Camera camera) {
if (!mNeedToSend) { return; } mNeedToSend = false;
Camera.Parameters parameters = camera.getParameters();
Log.d(TAG, "Data sends to the server!"); try { YuvImage yuvImage = new YuvImage(data, parameters.getPreviewFormat(), parameters.getPreviewSize().width, parameters.getPreviewSize().height, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); yuvImage.compressToJpeg( new Rect(0, 0, parameters.getPreviewSize().width, parameters.getPreviewSize().height), 90, baos); yuvImage = null;
byte cdata[] = baos.toByteArray(); Bitmap bitmap = BitmapFactory.decodeByteArray(cdata, 0, cdata.length); baos.close(); baos = null;
Matrix matrix = new Matrix(); matrix.postRotate(-90); matrix.postScale(-1, -1); Bitmap rBitmap = Bitmap .createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); bitmap.recycle(); bitmap = null;
int w = 252; int h = 336; float scaleWidth = ((float) w) / rBitmap.getWidth(); float scaleHeight = ((float) h) / rBitmap.getHeight();
Matrix matrix2 = new Matrix(); matrix2.postScale(scaleWidth, scaleHeight); Bitmap rrrBitmap = Bitmap.createBitmap(rBitmap, 0, 0, rBitmap.getWidth(), rBitmap.getHeight(), matrix2, true); Log.d(TAG, "(" + rrrBitmap.getWidth() + "," + rrrBitmap.getHeight() + ")"); rBitmap.recycle(); rBitmap = null;
if (null != rrrBitmap) {
ByteArrayOutputStream rbaos = new ByteArrayOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(rbaos); rrrBitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush(); bos.close(); sendJpeg2Server(rbaos.toByteArray()); rbaos.close(); rrrBitmap.recycle(); rrrBitmap = null; }
} catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { }
}
|