我有一个在后台拍照的服务......这项服务有三种模式:
前两个工作很棒,但是当启用闪光灯到相机参数时," Camera.PictureCallback"中没有返回任何图像... 事实上,回调永远不会被触发。
闪光灯闪光,一切似乎都没问题,但没有收到任何图像。
这是我的onStartCommand:
public int onStartCommand(Intent intent, int flags, int startId) {
try {
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_TOAST,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
PixelFormat.TRANSLUCENT
);
params.gravity = Gravity.TOP | Gravity.START;
params.width = 1;
params.height = 1;
params.x = 0;
params.y = 0;
sv = new SurfaceView(getApplicationContext());
windowManager.addView(sv, params);
SurfaceHolder sHolder = sv.getHolder();
sHolder.addCallback(this);
} catch (Exception e) {
e.printStackTrace();
}
return START_NOT_STICKY;
}
这是我的相机初始化代码:
myCamera = Camera.open();
if (myCamera != null) {
myCamera.setPreviewDisplay(sv.getHolder());
Camera.Parameters parameters = myCamera.getParameters();
parameters.setFlashMode(FLASH_MODE_ON);
parameters.setPictureFormat(ImageFormat.JPEG);
parameters.setJpegQuality(70);
// set camera parameters
myCamera.setParameters(parameters);
myCamera.startPreview();
myCamera.setErrorCallback(errorCallBack);
myCamera.takePicture(mShutter, null, mCall);
}
答案 0 :(得分:0)
我使用以下代码,它正在使用三星Galaxy 6,7; Moto G4,LG(我无法记住模型)。
public void onMyClick(View v){
startTakingPictures();
}
private void startTakingPictures() {
try {
android.hardware.Camera cam = android.hardware.Camera.open();
if (cam != null) {
int presentOrientation = getResources().getConfiguration().orientation;
SurfaceView sv = new SurfaceView(getBaseContext());
cam.setPreviewDisplay(sv.getHolder());
Camera.Parameters para = cam.getParameters();
para.setRotation(ORIENTATIONS.get(presentOrientation));
para.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
// Log.e(TAG, "lat lng == " + loc.getLatitude());
// para.setGpsLatitude(loc.getLatitude());
// para.setGpsLongitude(loc.getLongitude());
cam.setParameters(para);
cam.startPreview();
cam.takePicture(null, null, getJpegCallBack());
}
}
catch (Exception ex){
Log.e(TAG, ex.getMessage());
}
}
private Camera.PictureCallback getJpegCallBack() {
Camera.PictureCallback jpg = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream fos;
String path = getPathBaseName();
if(path.isEmpty()){
return;
}
path = "image.jpg";
Log.e(TAG, "Image path == " + path);
try {
fos = new FileOutputStream(path);
fos.write(data);
fos.close();
}
catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}
};
return jpg;
}
编辑:
由于Camera
现已弃用,您可能希望实施android.hardware.camera2
。
您的设备可以支持的参数类型似乎存在问题。看一下可用的flash参数:
Camera.Parameters para = cam.getParameters();
for(String s : para.getSupportedFlashModes()){
Log.e(TAG, "Supported Flash Mode = " + s);
}
您可能需要像这样设置闪光模式:
param.set("flash-mode", "on");
cam.setParameters(param);
除此之外,您可能希望在此帖中查看Karl Morrison的实施情况: Android camera: stopPreview vs releaseCamera