安卓相机不工作[关闭]
Posted
技术标签:
【中文标题】安卓相机不工作[关闭]【英文标题】:ANDROID CAMERA DOESN'T WORK [closed] 【发布时间】:2014-04-07 18:29:58 【问题描述】:我的安卓相机在真机上测试时不能用这种代码拍照和闪光,没有错误发生。为什么会这样?
public class CameraShooting implements SurfaceHolder.Callback
Context context;
Camera camera;
CameraInfo camerainfo;
Camera.Parameters paras;
SurfaceView surfaceview;
SurfaceHolder surfaceholder;
PictureCallback picturecallback;
public CameraShooting (Context context)
this.context = context;
public void cameraStart()
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))
surfaceview = new SurfaceView(context);
surfaceholder = surfaceview.getHolder();
surfaceholder.addCallback(this);
;
;
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
@Override
public void surfaceCreated(SurfaceHolder holder)
int numbersofCamera = camera.getNumberOfCameras();
int cameraID = 0;
camerainfo = new CameraInfo();
for(int i =1; i < numbersofCamera; i++)
camera.getCameraInfo(i, camerainfo);
if (camerainfo.facing == CameraInfo.CAMERA_FACING_FRONT)
cameraID = i;
camera = Camera.open(i);
break;
if(camerainfo.canDisableShutterSound)
camera.enableShutterSound(true);
paras = camera.getParameters();
paras.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
try
camera.setPreviewDisplay(surfaceholder);
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
camera.setParameters(paras);
camera.startPreview();
camera.takePicture(null, null, mPicture);
Toast.makeText(context, "Picture taken", Toast.LENGTH_SHORT).show();
;
@Override
public void surfaceDestroyed(SurfaceHolder holder)
private PictureCallback mPicture = new PictureCallback()
@Override
public void onPictureTaken(byte[] data, Camera camera)
File pictureFile = new File(context.getFilesDir(), "picture taken");
try
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
catch (FileNotFoundException e)
catch (IOException e)
;
;
非常感谢您的帮助! 非常感谢。
【问题讨论】:
Xml 文件:no errors occured
。您有几个没有记录的异常被捕获,所以您可能只是忽略这些错误。此外,您的break;
似乎不合适。在前置摄像头的情况下,您永远不会调用startPreview
或setPreviewDisplay
。
为什么“在前置摄像头的情况下,您从不调用 startPreview 或 setPreviewDisplay”?
休息不合适。 for 循环不会绕过后置摄像头,这可能是带有闪光灯的摄像头。
【参考方案1】:
假设带闪光灯的是后置摄像头,而不是前置摄像头。然后有类似的东西
for(int i =1; i < numbersofCamera; i++)
camera.getCameraInfo(i, camerainfo);
if (camerainfo.facing == CameraInfo.CAMERA_FACING_FRONT)
continue; //NOT break;
cameraID = i;
camera = Camera.open(i);
// ... etc, etc
.
【讨论】:
以上是关于安卓相机不工作[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
阻止相机,关闭选项并保持 GPS 和移动数据启用 - android