2013-06-02 42 views
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 screenshot

這裏是一個索尼XPERIA顯示:

enter image description here

有誰知道如何縮放圖像,使其適合任何設備的屏幕?謝謝 !

更新 我曾嘗試以下,但它不工作:

/** 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); 
    } 
} 

回答

1

當繪製位圖,使用Bitmap.createScaledBitmap縮放位圖到您的視圖的寬度和高度。然後它會適合。確保位圖中的任何點保持一致 - 使用原始位圖中的位置或縮放的位圖中的位置,不要混合和匹配,否則會得到奇怪的結果。

2

在繪製之前縮放畫布。

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

編輯:或者產生一個新的位圖作爲加布提到。在支持使用畫布方法進行硬件加速縮放的現代設備上,在運行時非常便宜,但將較大的位圖保留在內存中會比保留較小的版本消耗更多的內存。如果由於其他原因,您已經需要將全分辨率版本保存在內存中,則在繪製時對其進行縮放將會在新設備上更高效。如果您再也不需要全分辨率版本,則如果源圖像較大,則創建縮放位圖並丟棄原圖將使用較少的內存。

+0

我怎樣才能弄清楚有多大的規模呢?我用我試過的東西更新了我的問題,但沒有奏效。 – TomSelleck

+0

通過x中的view.getWidth()/ bitmap.getWidth()和y中的view.getHeight()/ bitmap.getHeight()進行縮放。 –

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); 
相關問題