2012-09-19 51 views
4

我的家庭自動化應用程序具有一項功能,人們可以使用平面圖和儀表板將圖像上載到手機,以便用戶控制其家庭自動化軟件。我讓他們上傳兩個圖像:一個可見圖像和他們想要顯示的圖形,第二個帶有純色的彩色地圖對應於他們想要從可視圖像中定位區域的對象。兩個圖像都必須具有相同的尺寸,像素級。當他們點擊屏幕時,我希望能夠從顏色貼圖覆蓋圖中獲取顏色,然後繼續執行與該顏色關聯的任何操作。問題是,圖像的縮放令我興奮不已。他們使用的圖像可能比設備屏幕大,因此我會縮放它們以使它們適合顯示屏。我現在不需要捏縮放功能,但我可能會稍後實施。目前,我只希望圖像以最大尺寸顯示,以便它適合屏幕。所以,我的問題是,我如何修改此代碼,以便從縮放的圖像中獲取正確的接觸點顏色。圖像縮放本身似乎工作正常。它被縮放並正確顯示。我無法獲得正確的接觸點。Android imageview從縮放圖像中獲取像素顏色

final Bitmap bm = decodeSampledBitmapFromFile(visible_image, size, size); 
if (bm!=null) { 
    imageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
    imageview.setImageBitmap(bm); 
} 

final Bitmap bm2 = decodeSampledBitmapFromFile(image_overlay, size, size); 
if (bm2!=null) { 
    overlayimageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
    overlayimageview.setImageBitmap(bm2); 

    imageview.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent mev) { 
      DecodeActionDownEvent(v, mev, bm2); 
      return false; 
     } 

    }); 
} 

private void DecodeActionDownEvent(View v, MotionEvent ev, Bitmap bm2) 
{ 
    xCoord = Integer.valueOf((int)ev.getRawX()); 
    yCoord = Integer.valueOf((int)ev.getRawY()); 
    try { 
     // Here's where the trouble is. 
     // returns the value of the unscaled pixel at the scaled touch point? 
     colorTouched = bm2.getPixel(xCoord, yCoord); 
    } catch (IllegalArgumentException e) { 
     colorTouched = Color.WHITE; // nothing happens when touching white 
    } 
} 

private static Bitmap decodeSampledBitmapFromFile(String fileName, 
     int reqWidth, int reqHeight) { 
    // code from 
    // http://developer.android.com/training/displaying-bitmaps/load-bitmap.html 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(fileName, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeFile(fileName, options); 
} 

private static int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // code from 
    // http://developer.android.com/training/displaying-bitmaps/load-bitmap.html 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 
     if (width > height) { 
      inSampleSize = Math.round((float)height/(float)reqHeight); 
     } else { 
      inSampleSize = Math.round((float)width/(float)reqWidth); 
     } 
    } 
    return inSampleSize; 
} 

回答

20

我想通了。我換成

xCoord = Integer.valueOf((int)ev.getRawX()); 
yCoord = Integer.valueOf((int)ev.getRawY()); 

Matrix inverse = new Matrix(); 
v.getImageMatrix().invert(inverse); 
float[] touchPoint = new float[] {ev.getX(), ev.getY()}; 
inverse.mapPoints(touchPoint); 
xCoord = Integer.valueOf((int)touchPoint[0]); 
yCoord = Integer.valueOf((int)touchPoint[1]); 
+2

太棒了!使用'getPixel(xCoord,yCoord)'現在可以返回準確的顏色! '((BitmapDrawable)mImageView.getDrawable())。getBitmap()。getPixel(xCoord,yCoord)' –

+0

請解釋反向映射,new xy到old xy – Godwin