1

我有以下代码用于在用户屏幕上显示图像,并在脸部周围绘制一个矩形:

public class AndroidFaceDetector extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);
        setContentView(new myView(this));
    }

    private class myView extends View {

        private int imageWidth, imageHeight;
        private int numberOfFace = 5;
        private FaceDetector myFaceDetect;
        private FaceDetector.Face[] myFace;
        float myEyesDistance;
        int numberOfFaceDetected;

        Bitmap myBitmap;

        public myView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub

            BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
            BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;

            myBitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/face.jpg", BitmapFactoryOptionsbfo);

            imageWidth = myBitmap.getWidth();
            imageHeight = myBitmap.getHeight();
            myFace = new FaceDetector.Face[numberOfFace];
            myFaceDetect = new FaceDetector(imageWidth, imageHeight,
                    numberOfFace);
            numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace);

        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub

            canvas.drawBitmap(myBitmap, 0, 0, null);

            Paint myPaint = new Paint();
            myPaint.setColor(Color.GREEN);
            myPaint.setStyle(Paint.Style.STROKE);
            myPaint.setStrokeWidth(3);

            for (int i = 0; i < numberOfFaceDetected; i++) {
                Face face = myFace[i];
                PointF myMidPoint = new PointF();
                face.getMidPoint(myMidPoint);
                myEyesDistance = face.eyesDistance();
                canvas.drawRect((int) (myMidPoint.x - myEyesDistance),
                        (int) (myMidPoint.y - myEyesDistance),
                        (int) (myMidPoint.x + myEyesDistance),
                        (int) (myMidPoint.y + myEyesDistance), myPaint);
            }
        }
    }
}

它在我的索尼爱立信 x8 上显示以下内容:

x8 截图

这是索尼 xperia 上的显示:

在此处输入图像描述

有人知道如何缩放图像以使其适合任何设备的屏幕吗?谢谢 !

更新 我尝试了以下方法,但它不起作用:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    DisplayMetrics metrics=new DisplayMetrics();

    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    height=metrics.heightPixels/metrics.xdpi;
    width=metrics.widthPixels/metrics.ydpi;
    // setContentView(R.layout.main);
    setContentView(new myView(this));
}

@Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub

        canvas.drawBitmap(myBitmap, 0, 0, null);

        Paint myPaint = new Paint();
        myPaint.setColor(Color.GREEN);
        myPaint.setStyle(Paint.Style.STROKE);
        myPaint.setStrokeWidth(3);

        for (int i = 0; i < numberOfFaceDetected; i++) {
            Face face = myFace[i];
            PointF myMidPoint = new PointF();
            face.getMidPoint(myMidPoint);
            myEyesDistance = face.eyesDistance();
            canvas.drawRect((int) (myMidPoint.x - myEyesDistance),
                    (int) (myMidPoint.y - myEyesDistance),
                    (int) (myMidPoint.x + myEyesDistance),
                    (int) (myMidPoint.y + myEyesDistance), myPaint);
        }


        canvas.scale(width, height);
    }
}
4

3 回答 3

2
DisplayMetrics metrics=new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(metrics);
float height=metrics.heightPixels/metrics.xdpi;
float width=metrics.widthPixels/metrics.ydpi;
canvas.drawRect(0, 0, width, height, paint);
于 2013-06-02T19:53:53.560 回答
2

在绘制之前缩放画布。

http://developer.android.com/reference/android/graphics/Canvas.html#scale(float,%20float)

编辑:或者像 Gabe 提到的那样生成一个新的位图。在支持硬件加速缩放的现代设备上,使用 canvas 方法在运行时非常便宜,但是在内存中保留较大的位图将比保留较小的版本消耗更多的内存。如果您出于其他原因已经需要将完整分辨率版本保留在内存中,则在绘图时缩放它在较新的设备上会更有效。如果您不再需要完整分辨率版本,则如果源图像较大,则创建缩放位图并丢弃原始图像将使用更少的内存。

于 2013-06-02T19:36:06.103 回答
1

绘制位图时,使用 Bitmap.createScaledBitmap 将位图缩放到视图的宽度和高度。然后它会适合。确保位图上的任何点保持一致 - 使用原始位图中或缩放位图中的位置,不要混合和匹配,否则你会得到奇怪的结果。

于 2013-06-02T19:35:47.347 回答