gpt4 book ai didi

java - takePicture 服务无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 13:24:35 27 4
gpt4 key购买 nike

我正在尝试使用后置摄像头拍照,然后在我使用以下代码的服务中获取它的字节:

Camera camera = Camera.open();
SurfaceView view = new SurfaceView(getApplicationContext());
SurfaceHolder holder = view.getHolder();
camera.getParameters().setPreviewSize(1, 1);
camera.setPreviewDisplay(holder);
camera.startPreview();
camera.takePicture(null, pictureCallback, null);

但是它不起作用。我没有收到异常,但 pictureCallback 从未被调用。

最佳答案

取自 Android 文档:http://developer.android.com/training/camera/photobasics.html

您应该像这样调用相机 Intent :

首先在 androidmanifest 文件中为您的应用添加必要的权限:

<manifest ... >
<uses-feature android:name="android.hardware.camera"
android:required="true" />
...
</manifest>

之后调用相应的intent来启动摄像头:

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 权限才能工作,用户可能不希望允许应用程序使用此权限。

关于java - takePicture 服务无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32789854/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com