【问题标题】:Can't get the correct pixel from an image returned from camera无法从相机返回的图像中获取正确的像素
【发布时间】:2019-07-07 21:10:31
【问题描述】:

我正在尝试在图像视图中加载从设备相机返回的图像并获取我触摸的像素的颜色。

我尝试缩放 xml 文件中的图像,但是当我这样做时,虽然我看到图像适合 imageview,但 touchlistener 会以图像的原始尺寸工作。 如果我不缩放它,我只会看到适合 imageview 的图像部分,而 touchlistener 会得到实际的像素。

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath)
        ivCamera.setImageBitmap(bitmap)
        ivCamera.setOnTouchListener { view, motionEvent ->
            val bmp = (ivCamera.drawable as BitmapDrawable).bitmap
            val pixel = bmp.getPixel(motionEvent!!.x.toInt(), motionEvent.y.toInt())
            pixelRed = Color.red(pixel)
            pixelGreen = Color.green(pixel)
            pixelBlue = Color.blue(pixel)
            tvColor.setBackgroundColor(Color.rgb(pixelRed!!, pixelGreen!!, pixelBlue!!))
            true
        }
    }
}


   <ImageView android:layout_width="match_parent"
           android:layout_weight="0.7"
           android:layout_height="0dp"
           android:id="@+id/ivCamera"
           android:scaleType="matrix"
           android:background="@android:drawable/ic_menu_report_image"
           app:layout_constraintEnd_toEndOf="parent"
           android:layout_marginEnd="8dp"
           android:layout_marginStart="8dp"
           app:layout_constraintHorizontal_bias="0.498"
           android:layout_margin="10dp"
           android:layout_marginBottom="8dp"
           android:adjustViewBounds="true"
           app:layout_constraintBottom_toBottomOf="parent"
           android:contentDescription="PicureTaken"/>

如果我在 xml 文件中缩放图像,我会看到图像适合图像视图,但触摸监听器以图像的原始尺寸工作。 如果我不缩放它,我只会看到适合 imageview 的图像部分,而 touchlistener 会得到实际的像素。

【问题讨论】:

  • “缩放”是指代码:scaleType="matrix"?
  • 好吧,很明显我不是“专业人士”,所以谷歌帮助我更接近我想做的是 scaleType。
  • 嗯?我只是问一个问题以帮助理解,以便我可以正确回答
  • 无意侮辱你,只是说明这种方法可能是完全错误的原因
  • 不用担心,给出了答案,但它是一个奇怪的答案!不是我通常的知识领域

标签: android kotlin android-imageview dimensions onactivityresult


【解决方案1】:

这可能是您在 XML 缩放类型生效 ivCamera.setImageBitmap 之前设置触摸侦听器 ivCamera.setOnTouchListener 的疯狂场景之一。即它在scaleType 生效之前设置了原始尺寸。

您可以在 ImageView 完成充气后使用布局侦听器来设置触摸侦听器。

https://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener

ivCamera.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            ivCamera.getViewTreeObserver().removeOnGlobalLayoutListener(this);

            if(ivCamera.getDrawable() != null) {
                  ivCamera.setOnTouchListener {
                  // etc
                  // ...
            }

        }
    });

这不是我有信心的答案,而是一个答案。 :-) 抱歉,这太可怕了/太糟糕了!


再看一遍……

 val bmp = (ivCamera.drawable as BitmapDrawable).bitmap
 val pixel = bmp.getPixel(motionEvent!!.x.toInt(), motionEvent.y.toInt())

您从 ImageView 中获取位图,然后调用 getPixel,但显然 .bitmap 正在返回原始大小的位图,即使您使用 scaleType 缩放它(用于视图)。

我的建议是将触摸事件的 x/y 坐标转换为缩放后的图像大小。

或在 ImageView 上调用 setImageBitmap 之前自己缩放位图。这样你就不需要翻译 x/y。

【讨论】:

  • 好的,我早上第一件事就试试,太累了现在改代码
  • 很遗憾,这不是答案。
  • @GiorgosKavalieratos 更新了我的答案,抱歉我没有给出代码解决方案,但我认为这绝对是解决问题的方法。 (例如Bitmap.createScaledBitmap
  • 是的,你是对的。我无法获得尺寸,因为它们是在加载图像后设置的,所以我得到 0。但翻译工作正常。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-14
  • 1970-01-01
相关资源
最近更新 更多