從Android文檔摘自:http://developer.android.com/training/camera/photobasics.html
你應該調用攝像頭的意圖是這樣的:
首先添加必要的權限,您的應用程序在androidmanifest文件:
<manifest ... >
<uses-feature android:name="android.hardware.camera"
android:required="true" />
...
</manifest>
之後請撥打相應的啓動相機的意圖:
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
從相機將在下面的方法,在那裏,你想,你將能夠存儲或使用它被稱爲接收數據:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
如果你想在一個服務來實現這一點,採取從這個link的代碼,你應該能夠採取從服務一個畫面:
mPreview = new CameraPreview(this, mCamera, jpegCallback);
WindowManager wm = (WindowManager) this
.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSPARENT);
params.height = 1;
params.width = 1;
wm.addView(mPreview, params);
如前所述所作的評論,請注意,這需要許可SYSTEM_ALERT_WINDOW工作,用戶可能不希望允許應用程序使用此權限。
我在服務中說我不能在服務中使用該代碼。 – Emma22