我已經對用戶顯示圖像下面的代碼屏幕,在臉部周圍繪製一個矩形自定義視圖:如何擴大在不同設備
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以下內容:
這裏是一個索尼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);
}
}
我怎樣才能弄清楚有多大的規模呢?我用我試過的東西更新了我的問題,但沒有奏效。 – TomSelleck
通過x中的view.getWidth()/ bitmap.getWidth()和y中的view.getHeight()/ bitmap.getHeight()進行縮放。 –