title: Browse Image and Face Detection
body: <p>I am having some issues with detecting faces of a browsed image. I know the issue is that I do not know how to apply the face detection code I am testing out on an imported image. The example code I am testing was written for an image stored locally. I believe I am close but can you help me out?</p>
  
  <p>First, I created a gallery method</p>
  
  <pre><code>    protected void gallery() {
      Intent intent = new Intent();
      intent.setType("image/*");
      intent.setAction("android.intent.action.GET_CONTENT");
      startActivityForResult(Intent.createChooser(intent, "Choose An Image"), 1);         
  }
  </code></pre>
  
  <p>I am still learning about intents and such, but as far as I understand I needed to use the intent to use Android's gallery, and because I setAction to get content, I am also using the intent to pass information to it. With that said, I tried to then pass the information in the intent to a uri. So this is what I did next.</p>
  
  <pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
      super.onActivityResult(requestCode, resultCode, intent);
      if(requestCode == 1 &amp;&amp; resultCode == RESULT_OK)
      {
          Uri uri = intent.getData();
          try {
              InputStream is = getContentResolver().openInputStream(uri);
              Bitmap bitmap = BitmapFactory.decodeStream(is);
              ImageView image = (ImageView)findViewById(R.id.img_view);
              image.setImageBitmap(bitmap);
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>So here is the confusing part for me. I guess InputStream has the image information? Well I tried to apply the face detection code inside this same try-catch. I figured that after image.setImageBitmap(bitmap) is completed, that is the time to apply face detection. Here is the face detection code.</p>
  
  <pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
      super.onActivityResult(requestCode, resultCode, intent);
      if(requestCode == 1 &amp;&amp; resultCode == RESULT_OK)
      {
          Uri uri = intent.getData();
          try {
              InputStream is = getContentResolver().openInputStream(uri);
              Bitmap bitmap = BitmapFactory.decodeStream(is);
              ImageView image = (ImageView)findViewById(R.id.image_view);
              image.setImageBitmap(bitmap);
  
              BitmapFactory.Options options = new BitmapFactory.Options();
              options.inPreferredConfig=Bitmap.Config.RGB_565;
              bitmap = BitmapFactory.decodeResource(getResources(), R.id.img_view, options);
  
              imageWidth = bitmap.getWidth();
              imageHeight = bitmap.getHeight();
              detectedFaces = new FaceDetector.Face[NUM_FACES];
              faceDetector= new FaceDetector(imageWidth, imageHeight, NUM_FACES);
              NUM_FACE_DETECTED = faceDetector.findFaces(bitmap, detectedFaces);
              mIL.invalidate();
          } catch (Exception e) {
              e.printStackTrace();
          }   
      }
  }
  </code></pre>
  
  <p>I do not know how to change "mFaceBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.smilingfaces, options);" which is for local images, to the image that I think is stored inside the InputStream (or is it? Where is the selected image?) I came up with the idea to instead do the imageView layout, since the image is in the layout. I do not understand how that all transfers and works together. Anyway, that code snippet is suppose to detect faces. And then onDraw() draws squares around the detected faces. I am not sure where to put it, but I placed it outside of the onActivityResult()</p>
  
  <pre><code>protected void onDraw(Canvas canvas) {
  
      Paint myPaint = new Paint();
      myPaint.setColor(Color.RED);
      myPaint.setStyle(Paint.Style.STROKE);
      myPaint.setStrokeWidth(3);
      myPaint.setDither(true);
  
      for (int count = 0; count &lt; NUM_FACE_DETECTED; count++) {
          Face face = detectedFaces[count];
          PointF midPoint = new PointF();
          face.getMidPoint(midPoint);
  
          eyeDistance = face.eyesDistance();
          canvas.drawRect(midPoint.x-eyeDistance, midPoint.y-eyeDistance, midPoint.x+eyeDistance, midPoint.y+eyeDistance, myPaint);   
      }
  }
  </code></pre>
  
  <p>Any advice? I am very close to getting this to work!</p>