我有以下代码用于在用户屏幕上显示图像,并在脸部周围绘制一个矩形:
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上显示以下内容:
这是sony 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);
}
}
答案 0 :(得分:2)
在绘制之前缩放画布。
http://developer.android.com/reference/android/graphics/Canvas.html#scale(float,%20float)
编辑:或者像Gabe提到的那样生成一个新的位图。在使用canvas方法支持硬件加速扩展的现代设备上,在运行时非常便宜,但在内存中保留较大的位图将比保留较小版本消耗更多内存。如果由于其他原因需要将完整版本保留在内存中,则在绘图时对其进行缩放将在新设备上更有效。如果您再也不需要全分辨率版本,如果源图像较大,创建缩放位图并丢弃原始版本将使用更少的内存。
答案 1 :(得分: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);
答案 2 :(得分:1)
绘制位图时,使用Bitmap.createScaledBitmap将位图缩放到视图的宽度和高度。然后它会适合。确保位图上的任何点保持一致 - 使用原始位图中的位置或缩放的位图,不要混合和匹配,否则您将得到奇怪的结果。