gpt4 book ai didi

java - 使用 IntentService 使用 Camera2 拍照

转载 作者:太空狗 更新时间:2023-10-29 15:25:19 25 4
gpt4 key购买 nike

我正在尝试创建一个无需显示预览即可拍照的应用程序。使用本教程- https://www.youtube.com/watch?v=oPu42I0HSi4如果我使用 Activity,我设法让它工作。现在,我正尝试将处理拍照操作的代码移动到 IntentService 中。

这是我到目前为止得到的:

MainActivity.java - Activity

private void startCameraService()
{
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,new String[]{
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE
},REQUEST_CAMERA_PERMISSION);
return;
}

Intent cameraService = new Intent(this, CameraService.class);
startService(cameraService);
}

CameraService.java -( Intent 服务)

public class CameraService extends IntentService {

//Input image state
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
static{
ORIENTATIONS.append(Surface.ROTATION_90,90);
ORIENTATIONS.append(Surface.ROTATION_0,0);
ORIENTATIONS.append(Surface.ROTATION_180,270);
ORIENTATIONS.append(Surface.ROTATION_270,180);
}

//Camera variables
private String cameraId;
private CameraDevice cameraDevice;
private CameraCaptureSession cameraCaptureSessions;
private CaptureRequest.Builder captureRequestBuilder;
private Size imageDimension;
private ImageReader imageReader;

//Output file variables
private File file;
private static final int REQUEST_CAMERA_PERMISSION = 200;
private boolean mFlashSupported;
private Handler mBackgroundHandler;
private HandlerThread mBackgroundThread;

//the orientation of the device
int rotation;
//bitmap of the taken photo
private Bitmap bitmapImage;

//camera state callback
CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
cameraDevice = camera;
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
cameraDevice.close();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onError(@NonNull CameraDevice cameraDevice, int i) {
cameraDevice.close();
cameraDevice=null;
}
};


public CameraService() {
super("CameraService");
}

@Override
protected void onHandleIntent(Intent intent)
{
takePicture();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void takePicture() {
Log.d("CameraService","inside TakePicture");
if(cameraDevice == null)
{
return;
}
CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
try{
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
Size[] jpegSizes = null;
if(characteristics != null)
jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
.getOutputSizes(ImageFormat.JPEG);

//Capture image with custom size
int width = 640;
int height = 480;
if(jpegSizes != null && jpegSizes.length > 0)
{
width = jpegSizes[0].getWidth();
height = jpegSizes[0].getHeight();
}
final ImageReader reader = ImageReader.newInstance(width,height,ImageFormat.JPEG,1);
List<Surface> outputSurface = new ArrayList<>(2);
outputSurface.add(reader.getSurface());

final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(reader.getSurface());
captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);

File file = new File(Environment.getExternalStorageDirectory() + "/"+UUID.randomUUID().toString()+".jpg");

ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader imageReader) {
Image image = null;
try{
image = reader.acquireLatestImage();

ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
save(bytes);
bitmapImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);

//TODO: fix the orientation so despite the phone position the photo is always correct and not rotated
// use ORIENTATIONS.get(rotation)
bitmapImage = rotateImage(bitmapImage, 270);
}
catch (Exeption e)
{
e.printStackTrace();
}
finally {
{
if(image != null)
image.close();
}
}
}
private void save(byte[] bytes) throws IOException {
OutputStream outputStream = null;
try{
outputStream = new FileOutputStream(file);
outputStream.write(bytes);
}finally {
if(outputStream != null)
outputStream.close();
}
}
};

reader.setOnImageAvailableListener(readerListener,mBackgroundHandler);
final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
Log.d("CameraService","image is saved");
}
};

cameraDevice.createCaptureSession(outputSurface, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
try{
cameraCaptureSession.capture(captureBuilder.build(),captureListener,mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}

@Override
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {

}
},mBackgroundHandler);


} catch (CameraAccessException e) {
e.printStackTrace();
}
}

public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void openCamera() {
CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
try{
cameraId = manager.getCameraIdList()[1];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
assert map != null;
imageDimension = map.getOutputSizes(SurfaceTexture.class)[0];

manager.openCamera(cameraId,stateCallback,null);

} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}

我的问题是,当我调用该服务时,cameraDevicenull。它应该在这里填充:

//camera state callback
CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
cameraDevice = camera;
}

CameraDevice 相机 为空。

对于为什么这是作为一个Activity 而不是作为一个IntentService 有什么建议吗?如果你能帮助我,我将不胜感激

最佳答案

调用 onHandleIntent() 后,调用 stopSelf()。因此,服务会在相机事件到来时停止。我猜你必须以某种方式让 Looper.loop() 在你的 onHandleIntent() 中运行。

关于java - 使用 IntentService 使用 Camera2 拍照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48768663/

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