这是我第一次使用相机。我已经阅读了很多例子和文档,所以我试着让自己的班级用前置摄像头拍照。
这就是我提出的:
public class CameraController {
private Context context;
private boolean hasCamera;
private Camera camera;
private int cameraId;
public CameraController(Context c){
context = c.getApplicationContext();
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
cameraId = getFrontCameraId();
if(cameraId != -1){
hasCamera = true;
}else{
hasCamera = false;
}
}else{
hasCamera = false;
}
}
public boolean hasCamera(){
return hasCamera;
}
public void getCameraInstance(){
camera = null;
if(hasCamera){
try{
camera = Camera.open(cameraId);
}
catch(Exception e){
}
}
}
public void takePicture(){
if(hasCamera){
camera.takePicture(null,null,mPicture);
}
}
public void releaseCamera(){
if(camera != null){
camera.release();
camera = null;
}
}
private int getFrontCameraId(){
int camId = -1;
int numberOfCameras = Camera.getNumberOfCameras();
CameraInfo ci = new CameraInfo();
for(int i = 0;i < numberOfCameras;i++){
Camera.getCameraInfo(i,ci);
if(ci.facing == CameraInfo.CAMERA_FACING_FRONT){
camId = i;
}
}
return camId;
}
private PictureCallback mPicture = new PictureCallback(){
@Override
public void onPictureTaken(byte[] data, Camera camera){
File pictureFile = getOutputMediaFile();
if(pictureFile == null){
Log.d("TEST", "Error creating media file, check storage permissions");
return;
}
try{
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
}catch(FileNotFoundException e){
Log.d("TEST","File not found: "+e.getMessage());
} catch (IOException e){
Log.d("TEST","Error accessing file: "+e.getMessage());
}
}
};
private File getOutputMediaFile(){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if(!mediaStorageDir.exists()){
if(!mediaStorageDir.mkdirs()){
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath()+File.separator+"IMG_"+timeStamp+".jpg");
return mediaFile;
}
}
所以上课看起来很好,但是我无法从中得到任何照片。 在LogCat中,我得到以下内容:
03-11 19:23:58.556: D/TEST(18655): SNAPSHOT GO!
03-11 19:23:58.556: I/caladbolg(272): 2752686164 cald_client.c (464) 24404 I [INF] + Cald_Client_ICamera_GetBufInfo index[0x2]
03-11 19:23:58.556: I/caladbolg(272): 2752687080 cald_client.c (487) 24404 I [INF] - Cald_Client_ICamera_GetBufInfo (0)
03-11 19:23:58.556: I/caladbolg(272): 2752687355 cald_client.c (464) 24404 I [INF] + Cald_Client_ICamera_GetBufInfo index[0x3]
03-11 19:23:58.566: I/caladbolg(272): 2752688057 cald_client.c (487) 24404 I [INF] - Cald_Client_ICamera_GetBufInfo (0)
03-11 19:23:58.566: I/caladbolg(272): 2752691108 cald_client.c (755) 24404 I [INF] + Cald_Client_ICamera_EnableThumbnail
03-11 19:23:58.566: I/caladbolg(272): 2752691749 cald_client.c (772) 24404 I [INF] pBufNum[1]
03-11 19:23:58.566: I/caladbolg(272): 2752692024 cald_client.c (778) 24404 I [INF] pBuf[0]:0x4311b000
03-11 19:23:58.566: I/caladbolg(272): 2752692817 cald_client.c (792) 24404 I [INF] - Cald_Client_ICamera_EnableThumbnail (0)
03-11 19:23:58.566: I/caladbolg(272): 2752693519 cald_client.c (832) 24404 I [INF] + Cald_Client_ICamera_TakeSnapshot
03-11 19:23:58.566: I/caladbolg(272): 2752693763 cald_client.c (849) 24404 I [INF] pBufNum[1]
03-11 19:23:58.566: I/caladbolg(272): 2752694007 cald_client.c (855) 24404 I [INF] pBuf[0]:0x4ad01000
03-11 19:23:58.566: I/caladbolg(272): 2752694831 cald_camctrl.c (6197) 20324 P [PFM] 2752694831 Cald_CamCtrl_ICamera_TakeSnapshot
03-11 19:23:58.566: E/caladbolg(272): 2752695137 cald_camctrl.c (19650) 20324 E [CAM] Error: The event is not ready in the current state.
03-11 19:23:58.566: E/libcamera(272): receivePictureDone: unnecessary callback was received.
03-11 19:23:58.566: I/caladbolg(272): 2752696205 cald_client.c (869) 24404 I [INF] - Cald_Client_ICamera_TakeSnapshot (0)
答案 0 :(得分:10)
我解决了这个问题。在阅读了一些其他帖子和教程之后,即使我们不希望显示预览,相机也需要“虚拟”表面进行预览。
这是我的最终代码,以防任何人需要它。请注意,“hasCamera”是指前置摄像头:
public class CameraController {
private Context context;
private boolean hasCamera;
private Camera camera;
private int cameraId;
public CameraController(Context c){
context = c.getApplicationContext();
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
cameraId = getFrontCameraId();
if(cameraId != -1){
hasCamera = true;
}else{
hasCamera = false;
}
}else{
hasCamera = false;
}
}
public boolean hasCamera(){
return hasCamera;
}
public void getCameraInstance(){
camera = null;
if(hasCamera){
try{
camera = Camera.open(cameraId);
prepareCamera();
}
catch(Exception e){
hasCamera = false;
}
}
}
public void takePicture(){
if(hasCamera){
camera.takePicture(null,null,mPicture);
}
}
public void releaseCamera(){
if(camera != null){
camera.stopPreview();
camera.release();
camera = null;
}
}
private int getFrontCameraId(){
int camId = -1;
int numberOfCameras = Camera.getNumberOfCameras();
CameraInfo ci = new CameraInfo();
for(int i = 0;i < numberOfCameras;i++){
Camera.getCameraInfo(i,ci);
if(ci.facing == CameraInfo.CAMERA_FACING_FRONT){
camId = i;
}
}
return camId;
}
private void prepareCamera(){
SurfaceView view = new SurfaceView(context);
try{
camera.setPreviewDisplay(view.getHolder());
}catch(IOException e){
throw new RuntimeException(e);
}
camera.startPreview();
Camera.Parameters params = camera.getParameters();
params.setJpegQuality(100);
camera.setParameters(params);
}
private PictureCallback mPicture = new PictureCallback(){
@Override
public void onPictureTaken(byte[] data, Camera camera){
File pictureFile = getOutputMediaFile();
if(pictureFile == null){
Log.d("TEST", "Error creating media file, check storage permissions");
return;
}
try{
Log.d("TEST","File created");
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
}catch(FileNotFoundException e){
Log.d("TEST","File not found: "+e.getMessage());
} catch (IOException e){
Log.d("TEST","Error accessing file: "+e.getMessage());
}
}
};
private File getOutputMediaFile(){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if(!mediaStorageDir.exists()){
if(!mediaStorageDir.mkdirs()){
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath()+File.separator+"IMG_"+timeStamp+".jpg");
return mediaFile;
}
}
答案 1 :(得分:0)
请查看此问题以获取信息并编码如何执行此操作:Taking pictures with camera on Android programmatically
你在这里使用的代码并没有错,但是这个代码有效。
如果您想在不向用户显示预览的情况下拍摄照片,请查看以下代码:https://github.com/aporter/coursera-android/blob/master/Examples/AudioVideoCamera/src/course/examples/AudioVideo/Camera/AudioVideoCameraActivity.java
您需要摆脱向用户显示预览的SurfaceView。