-1

我在 imageview 上显示相机预览。我想知道位置 imageview 的像素值,但我的应用程序无法安装并且已在 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):在 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)

谢谢

4

2 回答 2

0

线索在你的 logcat 中:NullPointerException

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

于 2013-06-05T12:51:03.647 回答
-1

您应该在单独的线程中执行此操作,因为您的位图或可绘制尚未创建。

于 2015-07-20T11:05:46.280 回答