我已經在imageview上顯示相機預覽。我想知道在一個位置imageview像素的價值,但我的應用程序無法安裝,並已停止在Android手機。我的代碼: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):at android.app。 Activity.performCreate(Activity.java:4465) 06-05 19:07:59.610:E/AndroidRuntime(6665):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 06-05 19:07: 59.610:E/An droidRuntime(6665):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
感謝
主要問題是在getBitmap()中。 – DroidImage
這是因爲您沒有在ImageView MyCameraPreview上設置任何圖像。您是否試圖從相機預覽或資源/資產中檢索位圖?如果您需要將相機預覽作爲位圖,那麼您需要查看http://developer.android.com/reference/android/hardware/Camera.PreviewCallback.html,然後將byte []數據轉換爲位圖格式。 –
我試圖從相機預覽中檢索位圖。我已經在CameraPreview類中聲明瞭public void onPreviewFrame(byte [] data,Camera camera)。我應該再次在MainActivity類中聲明onPreviewFrame(byte [] data,Camera camera)嗎? – DroidImage