2

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;
        }
Community
  • 1
  • 1
NoviceMe
  • 3,126
  • 11
  • 57
  • 117
  • Post the code you're using right now please. – Mike Perrenoud Nov 29 '13 at 20:47
  • @MichaelPerrenoud - Posted code above. – NoviceMe Nov 30 '13 at 03:47
  • All formats as in the ones supported by Java (GIF, PNG, BMP, WBMP) or other ones as well (ICO, TIFF, etc.)? Also, could you be more specific in which kinds of image formats you are looking for, or is it literally all images? I'm assuming these would be raster? – Vineet Kosaraju Dec 03 '13 at 07:12
  • @Bucco - Looking for all formats. But if some wont work it would be fine. If most of them can work it is good for me. – NoviceMe Dec 03 '13 at 15:42

2 Answers2

5

At the end of your decodeFile() method, you have a Bitmap. You can easily rely on Android to convert that to a JPEG by using

Bitmap.compress(Bitmap.CompressFormat format, int quality, OutputStream stream)

with Bitmap.CompressFormat.JPEG

There's more information here: Conversion of bitmap into jpeg in android

You should be able to either save it to a file, or compress it into memory (maybe use a ByteArrayOutputStream?)

Community
  • 1
  • 1
Matt
  • 3,837
  • 26
  • 29
0

Actually, standing to the FaceDetector.findFaces documentation, the input Bitmap has to be in 565 format. Therefore:

BitmapFactory.Options o = new BitmapFactory.Options();
o.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap b = BitmapFactory.decodeFile( selectedImagePath, o );

should work fine! The BitmapFactory should handle the input file format properly.

Sdra
  • 2,297
  • 17
  • 30