How to scale custom image views in android?

330 views Asked by At

In my face detector project, I'm using a custom ImageView (MyView) to detect faces and draw rectangles on the detected faces. For this I have set fixed dimensions for my customview (MyView) and trying to scale the bitmap to fit the customview. But somehow I'm not able to achieve this. I have gone throught all similar questions on SO. Please help.

activity_main.xml

<RelativeLayout  
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" 
tools:context=".MainActivity">


<com.sample.facedetector.MyView
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:id="@+id/MyViewId"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="28dp"
    android:scaleType="centerCrop"/>

MainActivity

package com.sample.facedetector;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MyView faceDetectorView = (MyView) findViewById(R.id.MyViewId);
    Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.face_detector).copy(Bitmap.Config.RGB_565,true);

    faceDetectorView.setImage(bmp);
    faceDetectorView.setScaleType(ImageView.ScaleType.CENTER_CROP);

    faceDetectorView.detectFaces();




}

MyView

package com.sample.facedetector;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.support.annotation.DrawableRes;
import android.util.AttributeSet;
import android.widget.ImageView;

public class MyView extends ImageView {

private Bitmap mFaceBitmap;
private int count = 0;
private FaceDetector.Face[] faces;
private Bitmap image;

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
}


public MyView(Context context) {
    super(context);
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public void detectFaces(){

    FaceDetector myFaceDetector = null;
    mFaceBitmap = image.copy(Bitmap.Config.RGB_565, true);

    int targetWidth = mFaceBitmap.getWidth();
    int targetHeight = mFaceBitmap.getHeight();

    faces= new FaceDetector.Face[5];


    int[] fpx =null;
    int[] fpy = null;
    myFaceDetector = new FaceDetector(targetWidth,targetHeight,5);
    count = myFaceDetector.findFaces(mFaceBitmap, faces);
    setWillNotCacheDrawing(true);
    requestLayout();
    this.invalidate();
}

/*@Override
public void setScaleType(ScaleType scaleType) {
    super.setScaleType(ScaleType.CENTER);
    invalidate();
}*/

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(mFaceBitmap,0,0,null);
    Paint myPaint = new Paint();
    myPaint.setColor(Color.GREEN);
    myPaint.setStyle(Paint.Style.STROKE);
    myPaint.setStrokeWidth(3);

    if (count > 0) {
        for (int i = 0; i < count; i++) {
            FaceDetector.Face face = faces[i];
            PointF midPoint = new PointF();
            face.getMidPoint(midPoint);
            float eyeDistance = face.eyesDistance();
            canvas.drawRect(midPoint.x-eyeDistance, midPoint.y-eyeDistance, midPoint.x+eyeDistance, midPoint.y+eyeDistance, myPaint);

        }
    }

}



@Override
public void setImageResource(@DrawableRes int resId) {
    super.setImageResource(resId);
    invalidate();
}

public void setImage(Bitmap image) {
    this.image = image;
}

}

0

There are 0 answers