Android imageview从缩放图像中获取像素颜色 [英] Android imageview get pixel color from scaled image

查看:21
本文介绍了Android imageview从缩放图像中获取像素颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My home automation app has a feature where people can upload images to their phone with floorplans and dashboards that they can use to control their home automation software. I have them upload two images: one visible image with the graphics that they want displayed, and a second color map with solid colors corresponding to the objects they want to target areas from the visible image. Both images have to be the same size, pixel-wise. When they tap the screen, I want to be able to get the color from the colormap overlay, and then I proceed to do what ever action has been associated with that color. The problem is, the scaling of the image is screwing me up. The images that they use can be larger than the device screen, so I scale them so they will fit within the display. I don't really need pinch to zoom capability right now, but I might implement it later. For now, I just want the image to be displayed at the largest size so it fits on the screen. So, my question is, how could I modify this code so that I can get the correct touchpoint color from the scaled image. The image scaling itself seems to be working fine. It is scaled and displays correctly. I just can't get the right touchpoint.

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

推荐答案

我想通了.我换了

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

这篇关于Android imageview从缩放图像中获取像素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆