Android Bitmap.getPixel() 非常不准确

发布于 2024-09-30 21:10:35 字数 2690 浏览 0 评论 0原文

项目概览:有两张图片。单击顶部图像(在我的应用程序中,我使用猫图片),它将把底部图像背景设置为您单击位置的像素颜色。

问题:背景颜色与单击位置的像素颜色不匹配,有时是随机颜色,有时是有一些色差。这很奇怪。

这是主要的 java 代码:

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
public class ColorRead extends Activity 
implements OnTouchListener{
 Integer myColor;
 String TAG;
 Bitmap bMap,bMap2;
 ImageView image,image2;
 Button BTN;
 MotionEvent mEvent;
 Integer touchX,touchY;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle Icicle) {
        super.onCreate(Icicle);
        setContentView(R.layout.main);
        bMap = BitmapFactory.decodeResource(getResources(), R.drawable.catpic);
        image2 = (ImageView) findViewById(R.id.bitmap2);
        image = (ImageView) findViewById(R.id.bitmap);
        image.setImageBitmap(bMap);
        image.setOnTouchListener(this);
    }
    public void scanForColor(int xMouse, int yMouse)
    {
     myColor = bMap.getPixel(xMouse,yMouse);
     Log.i(TAG, " R:"+Color.red(myColor)+" G:"+Color.green(myColor)+" B:"+Color.blue(myColor));
     image2.setBackgroundColor(myColor);
    }

 @Override
 public boolean onTouch(View v, MotionEvent e) {
  Integer id = v.getId();
  switch(id)
  {
  case R.id.bitmap:
   if(e.getAction() == MotionEvent.ACTION_DOWN)
         {
                 touchX = (int)e.getX();
                 touchY = (int)e.getY();
         }
   scanForColor(touchX,touchY);
         Log.i(TAG,touchX.toString());
         break;
    default:
     break;
  }
        return false;
 }
}

这是 Main.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView android:id="@+id/bitmap"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/catpic"
    />
    <ImageView android:id="@+id/bitmap2"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/catpic"
    />
    <Button android:id="@+id/button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
    android:text="@string/myButton"
    />

</LinearLayout>

Project OverView : Theres two images. Click the top image (in my app i use a cat pic) and it will set the bottom image background to the color of the pixel on the location you clicked at.

The Problem: The background color doesn't match the color of the pixel at the location of your click at times its randomly colored, other times its off by a few shades. Its wierd.

This is the main java code :

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
public class ColorRead extends Activity 
implements OnTouchListener{
 Integer myColor;
 String TAG;
 Bitmap bMap,bMap2;
 ImageView image,image2;
 Button BTN;
 MotionEvent mEvent;
 Integer touchX,touchY;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle Icicle) {
        super.onCreate(Icicle);
        setContentView(R.layout.main);
        bMap = BitmapFactory.decodeResource(getResources(), R.drawable.catpic);
        image2 = (ImageView) findViewById(R.id.bitmap2);
        image = (ImageView) findViewById(R.id.bitmap);
        image.setImageBitmap(bMap);
        image.setOnTouchListener(this);
    }
    public void scanForColor(int xMouse, int yMouse)
    {
     myColor = bMap.getPixel(xMouse,yMouse);
     Log.i(TAG, " R:"+Color.red(myColor)+" G:"+Color.green(myColor)+" B:"+Color.blue(myColor));
     image2.setBackgroundColor(myColor);
    }

 @Override
 public boolean onTouch(View v, MotionEvent e) {
  Integer id = v.getId();
  switch(id)
  {
  case R.id.bitmap:
   if(e.getAction() == MotionEvent.ACTION_DOWN)
         {
                 touchX = (int)e.getX();
                 touchY = (int)e.getY();
         }
   scanForColor(touchX,touchY);
         Log.i(TAG,touchX.toString());
         break;
    default:
     break;
  }
        return false;
 }
}

Heres the Main.XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView android:id="@+id/bitmap"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/catpic"
    />
    <ImageView android:id="@+id/bitmap2"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/catpic"
    />
    <Button android:id="@+id/button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
    android:text="@string/myButton"
    />

</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文