从缩放后的图像的Andr​​oid ImageView的GET像素颜色缩放、像素、图像、颜色

2023-09-05 01:29:59 作者:刻骨的寂寞

我的家庭自动化的应用程序有,人们可以上传图片到自己的手机与平面布置图和仪表板,他们可以用它来控制他们的家庭自动化软件的功能。我把它们上传两张图片:一个可见的图像,他们希望显示的图形,并与相应的,他们想要的目标从可见光图像区域的对象纯色第二彩色地图。两个图像必须是相同的大小,逐像素。当他们点击屏幕,我希望能够从颜色表覆盖的颜色,然后我着手做任何行动用这种颜色被关联。的问题是,图像的缩放拧我。他们所使用的图像可以比手机屏幕大,所以我缩放他们,因此他们将适合在显示器内。我并不真的需要捏变焦能力的权利,但我以后可能会实现它。现在,我只希望,因此在屏幕上适合图像显示的最大尺寸。所以,我的问题是,我怎么能修改此code,这样我可以从缩放后的图像正确的接触点的颜色。图像缩放本身似乎是工作的罚款。它被正确缩放和显示。我就不能得到正确的接触点。

 最后的位图BM =去codeSampledBitmapFromFile(visible_image,尺寸,大小);
 如果(BM!= NULL){
    imageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    imageview.setImageBitmap(BM);
 }

 最后的位图BM2 =去codeSampledBitmapFromFile(image_overlay,尺寸,大小);
 如果(BM2!= NULL){
    overlayimageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    overlayimageview.setImageBitmap(BM2);

    imageview.setOnTouchListener(新OnTouchListener(){

        @覆盖
        公共布尔onTouch(视图V,MotionEvent兆电子伏){
            德codeActionDownEvent(V,兆电子伏,BM2);
            返回false;
        }

    });
 }

 私人无效德codeActionDownEvent(视图V,MotionEvent EV,位图BM2)
 {
    XCOORD = Integer.valueOf((int)的ev.getRawX());
    YCOORD = Integer.valueOf((int)的ev.getRawY());
    尝试 {
        //这里就是麻烦的是。
        //返回未缩放的像素的缩放触摸点的价值?
        colorTouched = bm2.getPixel(XCOORD,YCOORD);
    }赶上(抛出:IllegalArgumentException E){
        colorTouched = Color.WHITE; //触摸白当什么也没发生
    }
 }

 私有静态位图德codeSampledBitmapFromFile(字符串文件名,
         INT reqWidth,诠释reqHeight){
     从// code
     // http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
     最后BitmapFactory.Options选项=新BitmapFactory.Options();
     options.inJustDe codeBounds = TRUE;
     BitmapFactory.de codeFILE(文件名,选择);

     //计算inSampleSize
     options.inSampleSize = calculateInSampleSize(选项,reqWidth,reqHeight);

     与inSampleSize集//德code位图
     options.inJustDe codeBounds = FALSE;
     返回BitmapFactory.de codeFILE(文件名,选择);
 }

 私有静态诠释calculateInSampleSize(
      BitmapFactory.Options选项,INT reqWidth,诠释reqHeight){
     从// code
     // http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
     //原始高度和宽度的图像
     最终诠释身高= options.outHeight;
     最终诠释宽度= options.outWidth;
     INT inSampleSize = 1;

     如果(高度> reqHeight ||宽度GT; reqWidth){
       如果(宽>高度){
          inSampleSize = Math.round((浮动)的高度/(浮点)reqHeight);
       } 其他 {
          inSampleSize = Math.round((浮点)宽/(浮点)reqWidth);
       }
     }
     返回inSampleSize;
 }
 

解决方案

我想它了。我换成

  XCOORD = Integer.valueOf((INT)ev.getRawX());
 YCOORD = Integer.valueOf((int)的ev.getRawY());
 

 矩阵反=新的Matrix();
 。v.getImageMatrix()反(反向);
 浮动[]接触点=新的浮动[] {ev.getX(),ev.getY()};
 inverse.mapPoints(接触点);
 XCOORD = Integer.valueOf((int)的接触点[0]);
 YCOORD = Integer.valueOf((int)的接触点[1]);
 

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

解决方案

I figured it out. I replaced

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

with

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