Since face detector works only with jpg files, how can you make it work with all formats? I am letting user select any image from sd card so image can be in any format. Than i use decodefile method as mentioned in this post to scale the image down: Android: Resize a large bitmap file to scaled output file
My questions are:
Can we add some method inside decodefile method to convert image to jpg first?
What is the best and efficient way to convert images to jpg so the face detector class detect faces?
My code so far:
public FaceView(Context context) {
super(context);
File photo = new File(selectedImagePath);
sourceImage = decodeFile(photo);
picWidth = sourceImage.getWidth();
picHeight = sourceImage.getHeight();
sourceImage = Bitmap.createScaledBitmap (sourceImage, picWidth, picHeight, false);
arrayFaces = new FaceDetector( picWidth, picHeight, NUM_FACES );
arrayFaces.findFaces(sourceImage, getAllFaces);
for (int i = 0; i < getAllFaces.length; i++)
{
getFace = getAllFaces[i];
try {
PointF eyesMP = new PointF();
getFace.getMidPoint(eyesMP);
eyesDistance[i] = getFace.eyesDistance();
Log.d("1st eye distance", "" + getFace.eyesDistance());
eyesMidPts[i] = eyesMP;
}
catch (Exception e)
{
if (DEBUG) Log.e("Face", i + " is null");
}
}
}
Decode method:
private Bitmap decodeFile(File f){
Bitmap b = null;
int screenWidth = 0;
int screenHeight = 0;
Point size = new Point();
WindowManager w = getWindowManager();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
{
w.getDefaultDisplay().getSize(size);
screenWidth = size.x;
screenHeight = size.y;
}
else
{
Display d = w.getDefaultDisplay();
screenWidth = d.getWidth();
screenHeight = d.getHeight();
}
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream fis = null;
try{
fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
// was > IMAGE_MAX_SIZE for both i changed it****
int scale = 1;
if (o.outHeight > screenHeight || o.outWidth > screenWidth) {
scale = (int)Math.pow(2, (int) Math.round(Math.log(screenHeight /
(double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return b;
}