我在 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)
谢谢