【问题标题】:Android imageview get pixel color from scaled imageAndroid imageview从缩放图像中获取像素颜色
【发布时间】:2012-09-19 13:57:42
【问题描述】:

我的家庭自动化应用程序具有一项功能,人们可以将带有平面图和仪表板的图像上传到手机,他们可以使用它们来控制他们的家庭自动化软件。我让他们上传两张图像:一张带有他们想要显示的图形的可见图像,以及第二张带有与他们想要从可见图像定位的对象区域对应的纯色的彩色地图。两个图像必须是相同的大小,以像素为单位。当他们点击屏幕时,我希望能够从颜色图叠加中获取颜色,然后我继续执行与该颜色相关的任何操作。问题是,图像的缩放把我搞砸了。他们使用的图像可能比设备屏幕大,所以我对它们进行缩放,以便它们适合显示器。我现在真的不需要捏缩放功能,但我可能会在以后实现它。现在,我只希望图像以最大尺寸显示,以便适合屏幕。所以,我的问题是,如何修改此代码,以便从缩放图像中获得正确的接触点颜色。图像缩放本身似乎工作正常。它已缩放并正确显示。我就是找不到正确的接触点。

 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;
 }

【问题讨论】:

    标签: android imageview


    【解决方案1】:

    我想通了。我换了

     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]);
    

    【讨论】:

    • 太棒了!使用getPixel(xCoord,yCoord) 现在返回准确的颜色! ((BitmapDrawable)mImageView.getDrawable()).getBitmap().getPixel(xCoord,yCoord)
    • 请解释一下反向映射,新 x,y 到旧 xy
    • 优秀的答案!谢谢。
    【解决方案2】:

    7 年后我必须提供另一个答案,因为从 LG G6 升级到 LG G8s 后,接受答案的代码停止返回正确的颜色。

    这个解决方案适用于我在家里找到的所有手机,但谁知道呢……也许这个也不是通用的。

    (使用Xamarin C#但原理简单易懂)

    首先我们必须将ImageView 的实际大小设为float 才能进行浮点除法

    private float imageSizeX;
    private float imageSizeY;
    
    private void OnCreate(...) {
        ...
        FindViewById<ImageView>(Resource.Id.colpick_Image).LayoutChange += OnImageLayout;
        FindViewById<ImageView>(Resource.Id.colpick_Image).Touch += Image_Touch;
        ...
    }
    
    //Event called every time the layout of our ImageView is updated
    private void OnImageLayout(object sender, View.LayoutChangeEventArgs e) {
        imageSizeX = Image.Width;
        imageSizeY = Image.Height;
        Image.LayoutChange -= OnImageLayout; //Just need it once then unsubscribe
    }
    
    //Called every time user touches/is touching the ImageView
    private void Image_Touch(object sender, View.TouchEventArgs e) {
        MotionEvent m = e.Event;
    
        ImageView img = sender as ImageView;
    
        int x = Convert.ToInt32(m.GetX(0));
        int y = Convert.ToInt32(m.GetY(0));
    
        Bitmap bmp = (img.Drawable as BitmapDrawable).Bitmap;
    
        // The scale value (how many times is the image bigger)
        // I only use it with images where Width == Height, so I get
        // scaleX == scaleY
        float scaleX = bmp.Width / imageSizeX;
        float scaleY = bmp.Height / imageSizeY;
    
        x = (int)(x * scaleX);
        y = (int)(y * scaleY);
    
        // Check for invalid values/outside of image bounds etc...
    
        // Get the correct Color ;)
        int color = bmp.GetPixel(x, y);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-21
      • 2019-07-27
      • 2011-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      相关资源
      最近更新 更多