Skip to content

Instantly share code, notes, and snippets.

@casidiablo
Created November 19, 2010 19:33
Show Gist options
  • Save casidiablo/707010 to your computer and use it in GitHub Desktop.
Save casidiablo/707010 to your computer and use it in GitHub Desktop.
public class MAIN extends Activity {
private static final int TAKE_PICTURE_CODE = 100;
private static final int MAX_FACES = 5;
private ViewFlipper mFlippler;
private ImageView mThePicture;
private TextView mTheMessage;
private Bitmap cameraBitmap = null;
ImageView photoResultView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.scan_box)).setOnClickListener(btnClick);
((Button) findViewById(R.id.previous_box)).setOnClickListener(btnClick);
((Button) findViewById(R.id.about_box)).setOnClickListener(btnClick);
((Button) findViewById(R.id.close_box)).setOnClickListener(btnClick);
mFlippler = (ViewFlipper)findViewById(R.id.your_flipper);
mThePicture = (ImageView)findViewById(R.id.image_view);
mThePictur = (TextView)findViewById(R.id.message);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//
}
private void openCamera() {
//
}
public void processCameraImage(Intent intent) {
mFlippler.setDisplayedChild(1); // 1 == your second layout
mThePicture = (ImageView) findViewById(R.id.image_view); //
cameraBitmap = (Bitmap) intent.getExtras().get("data");
imageView.setImageBitmap(cameraBitmap);
detectFaces();
}
private void detectFaces() {
((Button) findViewById(R.id.analyze_face)).setOnClickListener(btnClick);
if (null != cameraBitmap) {
int width = cameraBitmap.getWidth();
int height = cameraBitmap.getHeight();
FaceDetector detector = new FaceDetector(width, height,
MAIN.MAX_FACES);
Face[] faces = new Face[MAIN.MAX_FACES];
Bitmap bitmap565 = Bitmap.createBitmap(width, height,
Config.RGB_565);
Paint ditherPaint = new Paint();
Paint drawPaint = new Paint();
ditherPaint.setDither(true);
drawPaint.setColor(Color.RED);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeWidth(2);
Canvas canvas = new Canvas();
canvas.setBitmap(bitmap565);
canvas.drawBitmap(cameraBitmap, 0, 0, ditherPaint);
int facesFound = detector.findFaces(bitmap565, faces);
PointF midPoint = new PointF();
float eyeDistance = 0.0f;
float confidence = 0.0f;
Log.i("FaceDetector", "Number of faces found: " + facesFound);
if (facesFound < 1) {
theMessage.setText(R.string.noFace);
mThePicture.setImageBitmap(bitmap565);
return; // Don't do anything else!
} else {
showToast(this, "Face Detected!");
for (int index = 0; index < facesFound; ++index) {
// Get the eye distance, detected eye mid point and
// confidence
faces[index].getMidPoint(midPoint);
eyeDistance = faces[index].eyesDistance();
confidence = faces[index].confidence();
Log.i("FaceDetector", "Confidence: " + confidence
+ ", Eye distance: " + eyeDistance
+ ", Mid Point: (" + midPoint.x + ", " + midPoint.y
+ ")");
// Draw a small rectangle frame around the eye
canvas.drawRect((int) midPoint.x - eyeDistance,
(int) midPoint.y - eyeDistance, (int) midPoint.x
+ eyeDistance, (int) midPoint.y
+ eyeDistance, drawPaint);
}
}
String filepath = Environment.getExternalStorageDirectory()
+ "/facedetect" + System.currentTimeMillis() + ".jpg";
try {
FileOutputStream fos = new FileOutputStream(filepath);
bitmap565.compress(CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mTheMessage.setText(R.string.yesFace);
mThePicture.setImageBitmap(bitmap565);
}
}
private void showToast(Context mContext, String text) {
Toast.makeText(mContext, text, Toast.LENGTH_LONG).show();
}
private final View.OnClickListener btnClick = new View.OnClickListener() {
//
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment