Android:从imageview上显示的相机中获取像素

时间:2013-06-05 12:24:35

标签: android bitmap android-camera drawable android-imageview

我在imageview上显示相机预览。我想知道位置imageview上像素的值但是我的Aplication无法安装并且已经在Android手机上停止了。我的代码:

public class MainActivity extends Activity {

private CameraPreview camPreview;
private ImageView MyCameraPreview = null;
private FrameLayout mainLayout;
private TextView colorRGB;
private static int PreviewSizeWidth = 640;
private int PreviewSizeHeight = 480;
//private Bitmap bitmap;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MyCameraPreview = new ImageView(this);

    SurfaceView camView = new SurfaceView(this);
    SurfaceHolder camHolder = camView.getHolder();
    camPreview = new CameraPreview(PreviewSizeWidth, PreviewSizeHeight, MyCameraPreview);

    camHolder.addCallback(camPreview);

    mainLayout = (FrameLayout) findViewById(R.id.framelayout1);

    mainLayout.addView(camView, new LayoutParams(PreviewSizeWidth, PreviewSizeHeight));
    mainLayout.addView(MyCameraPreview,new LayoutParams(PreviewSizeWidth, PreviewSizeHeight));

    colorRGB=(TextView)findViewById(R.id.textView1);

    float Ex=48;
    float Ey=28;
    float[] eventXY = new float[]{Ex,Ey};

    Matrix invertMatrix = new Matrix();
    MyCameraPreview.getImageMatrix().invert(invertMatrix);

    invertMatrix.mapPoints(eventXY);
    int x=Integer.valueOf((int)eventXY[0]);
    int y=Integer.valueOf((int)eventXY[1]);

    Drawable imgDrawable = MyCameraPreview.getDrawable();

    Bitmap bitmap = ((BitmapDrawable) imgDrawable).getBitmap();
    int getRGB = bitmap.getPixel(x, y);
    colorRGB.setText(Integer.toHexString(getRGB));

   }

错误日志报告:

  

06-05 19:07:59.610:E / AndroidRuntime(6665):引起:java.lang.NullPointerException   06-05 19:07:59.610:E / AndroidRuntime(6665):at com.proyek.androsign2.MainActivity.onCreate(MainActivity.java:73)   06-05 19:07:59.610:E / AndroidRuntime(6665):在android.app.Activity.performCreate(Activity.java:4465)   06-05 19:07:59.610:E / AndroidRuntime(6665):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)   06-05 19:07:59.610:E / AndroidRuntime(6665):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)

由于

2 个答案:

答案 0 :(得分:0)

线索在你的logcat中:NullPointerException

如果我是你,请逐行检查并检查哪个方法返回null。它最有可能是getDrawable()或getBitmap()

答案 1 :(得分:-1)

你应该在一个单独的线程中做你的位图或drawable尚未创建。