我正在创建一个Android应用,需要一个无需用户互动即可拍摄照片的功能。
我只想要一个课程,例如,CameraService.java'它有一个构造函数,它采用相机设置(例如质量,资源等)和一个名为“拍摄照片”的公共函数。它通过类型位图返回图像。我一直在寻找如何使用Camera API 2来解决这个问题,但每次都失败了。
执行此操作的大部分示例都要求在MainActivity中或在扩展Activity的类中制作相机(我想尽量避免这种情况)。
CameraService.java
我使用了StackOverflow帖子(下面引用)中的以下代码,该帖子仅将图像保存到硬编码位置(并使用Camera API 1),但我遇到了错误。
package me.sam.camtest;
import android.hardware.Camera;
import android.util.Log;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CameraService {
Camera mCamera;
private int quality;
public CameraService(int quality){
this.quality = quality;
}
private int findBackFacingCamera() {
int cameraId = -1;
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
cameraId = i;
break;
}
}
return cameraId;
}
private boolean safeCameraOpen(int id) {
boolean qOpened = false;
try {
releaseCamera();
mCamera = Camera.open(id);
qOpened = (mCamera != null);
} catch (Exception e) {
e.printStackTrace();
}
return qOpened;
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
Camera.PictureCallback mCall = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream("/sdcard/Image.jpg");
outStream.write(data);
outStream.close();
} catch (FileNotFoundException e){
Log.d("CAMERA", e.getMessage());
} catch (IOException e){
Log.d("CAMERA", e.getMessage());
}
}
};
public void takePhoto(){
// Should return bitmap in future
int back_cam = findBackFacingCamera();
if(back_cam != -1){
safeCameraOpen(1);
mCamera.startPreview();
mCamera.takePicture(null, null, mCall);
}
}
}
运行时错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: me.sam.camtest, PID: 985
java.lang.RuntimeException: Unable to start activity ComponentInfo{me.sam.camtest/me.sam.camtest.MainActivity}: java.lang.RuntimeException: takePicture failed
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1588)
at android.hardware.Camera.takePicture(Camera.java:1530)
at me.sam.camtest.CameraService.takePhoto(CameraService.java:74)
at me.sam.camtest.Reply.<init>(Reply.java:47)
at me.sam.camtest.MainActivity.onCreate(MainActivity.java:80)
at android.app.Activity.performCreate(Activity.java:6999)
at android.app.Activity.performCreate(Activity.java:6990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我researched这个错误似乎人们只能通过添加一个Surface View来修复它,这需要将CameraService作为一个活动或传递一个上下文(不是我想要的)。
总之,我怎样才能实现以下目标:
PS:我是StackOverFlow社区的新手,我试图以最好的能力遵循指南,请不要太讨厌:)
我的研究: