Example usage for android.graphics YuvImage getWidth

List of usage examples for android.graphics YuvImage getWidth

Introduction

In this page you can find the example usage for android.graphics YuvImage getWidth.

Prototype

public int getWidth() 

Source Link

Usage

From source file:com.example.android.camera.CameraActivity.java

@Override
protected void onResume() {
    super.onResume();

    Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {

        @Override//from w w  w.  j  av  a2 s.c om
        public void onPreviewFrame(byte[] data, Camera camera) {

            if (notRequesting && mPreview.faces.size() >= 1 && imageFormat == ImageFormat.NV21) {

                // Block Request.
                notRequesting = false;

                try {
                    Camera.Parameters parameters = camera.getParameters();
                    Size size = parameters.getPreviewSize();

                    textServerView.setText("Preparing Image to send");
                    YuvImage previewImg = new YuvImage(data, parameters.getPreviewFormat(), size.width,
                            size.height, null);
                    pWidth = previewImg.getWidth();
                    pHeight = previewImg.getHeight();

                    Log.d("face View", "Width: " + pWidth + " x Height: " + pHeight);
                    prepareMatrix(matrix, 0, pWidth, pHeight);
                    List<Rect> foundFaces = getFaces();

                    for (Rect cRect : foundFaces) {
                        // Cropping
                        ByteArrayOutputStream bao = new ByteArrayOutputStream();
                        previewImg.compressToJpeg(cRect, 100, bao);
                        byte[] mydata = bao.toByteArray();

                        // Resizing
                        ByteArrayOutputStream sbao = new ByteArrayOutputStream();
                        Bitmap bm = BitmapFactory.decodeByteArray(mydata, 0, mydata.length);
                        Bitmap sbm = Bitmap.createScaledBitmap(bm, 100, 100, true);
                        bm.recycle();
                        sbm.compress(Bitmap.CompressFormat.JPEG, 100, sbao);
                        byte[] mysdata = sbao.toByteArray();

                        RequestParams params = new RequestParams();
                        params.put("upload", new ByteArrayInputStream(mysdata), "tmp.jpg");

                        textServerView.setText("Sending Image to the Server");

                        FaceMatchClient.post(":8080/match", params, new JsonHttpResponseHandler() {
                            @Override
                            public void onSuccess(JSONArray result) {
                                Log.d("face onSuccess", result.toString());

                                try {
                                    JSONObject myJson = (JSONObject) result.get(0);
                                    float dist = (float) Double.parseDouble(myJson.getString("dist"));
                                    Log.d("distance", "" + dist);
                                    int level = (int) ((1 - dist) * 100);
                                    if (level > previousMatchLevel) {
                                        textView.setText("Match " + level + "% with " + myJson.getString("name")
                                                + " <" + myJson.getString("email") + "> ");

                                        loadImage(myJson.getString("classes"), myJson.getString("username"));
                                    }

                                    previousMatchLevel = level;
                                    trialCounter++;

                                    if (trialCounter < 100 && level < 74) {
                                        textServerView.setText("Retrying...");
                                        notRequesting = true;
                                    } else if (trialCounter == 100) {
                                        textServerView.setText("Fail...");
                                    } else {
                                        textServerView.setText("Found Good Match? If not try again!");
                                        fdButtonClicked = false;
                                        trialCounter = 0;
                                        previousMatchLevel = 0;
                                        mCamera.stopFaceDetection();
                                        button.setText("StartFaceDetection");
                                    }

                                } catch (JSONException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                                //                                        informationView.showInfo(myJson);
                            }

                        });
                    }

                    textServerView.setText("POST Sent");
                    textServerView.setText("Awaiting for response");

                } catch (Exception e) {
                    e.printStackTrace();
                    textServerView.setText("Error AsyncPOST");
                }
            }
        }
    };

    // Open the default i.e. the first rear facing camera.
    mCamera = Camera.open();
    mCamera.setPreviewCallback(previewCallback);

    // To use front camera
    //        mCamera = Camera.open(CameraActivity.getFrontCameraId());
    mPreview.setCamera(mCamera);
    parameters = mCamera.getParameters();
    imageFormat = parameters.getPreviewFormat();
    PreviewSize = parameters.getPreviewSize();
}