所以,我一直在引用这个:http://developer.android.com/reference/android/hardware/Camera.html
我使用LibGDX这就是为什么你会看到一些不同之处,我也将mCamera改为androidCamera以便于阅读。
这是我的代码:
package com.fmeg.tapdat.android.camera;
import java.io.IOException;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.fmeg.tapdat.android.AndroidLauncher;
import com.fmeg.tapdat.camera.CameraInterface;
public class AndroidCamera extends SurfaceView implements
SurfaceHolder.Callback, CameraInterface {
private AndroidLauncher application;
private SurfaceHolder mHolder;
private Camera androidCamera;
public AndroidCamera(AndroidLauncher _application) {
super(_application.getContext());
this.application = _application;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceCreated(mHolder);
}
@Override
public void showCamera(boolean show) {
androidCamera.startPreview();
}
/**
* Organized by call order
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
androidCamera = Camera.open();
try {
androidCamera.setPreviewDisplay(mHolder);
} catch (Exception e) {
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Camera.Parameters params = androidCamera.getParameters();
params.setPreviewSize(width, height);
androidCamera.setParameters(params);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
androidCamera.stopPreview();
androidCamera = null;
}
}
我有一个名为CameraInterface的接口,代码如下:
package com.fmeg.tapdat.camera;
public interface CameraInterface {
public void showCamera(boolean show);
}
在我的主类(android项目)中,我的代码看起来像这样
[代码]
public class AndroidLauncher extends AndroidApplication {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
AndroidCamera cam = new AndroidCamera(this);
initialize(new TapdatGame(cam), config);
}
}
并在我的主要课程(核心项目)
package com.fmeg.tapdat;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.fmeg.tapdat.camera.CameraInterface;
public class TapdatGame extends ApplicationAdapter {
private CameraInterface camera;
public TapdatGame(CameraInterface cameraInterface) {
this.camera = cameraInterface;
}
@Override
public void create () {
camera.showCamera(true);
}
@Override
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
public CameraInterface getCamera() {
return camera;
}
}
请帮助我实现此功能,当前实现会调用相机故障
Tag(Camera) - Error 1001