我正在使用自定义相机来捕捉图片,并且支持所有设备和屏幕方向我写了两个不同的 - 不同的xmls 。
一个 人像模式(在 res>布局下)和第二个 横向模式(在 res> layout-land 下)举个例子:
我的相机已经初始化,我已准备好拍摄/拍摄图像 ---->>>我的 相机预览模式仍为横向
现在我决定使用人像模式(所以我改变了相机的旋转度)从 LANDSCAPE模式到PORTRAIT模式 ,但在以人像模式启动相机之前,关闭并打开相机镜头......(简而言之,重新初始化我的相机)
CameraLauncherActivity.java: -
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
mCamera = getCameraInstance();
mCameraPreview = new PreviewSurface(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);
Button captureButton = (Button) findViewById(R.id.btnCapture);
Log.d(CameraLauncherActivity.LOG_TAG, "captureButton :: " + captureButton);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(null, null, mPicture);
Log.d(CameraLauncherActivity.LOG_TAG, "mCamera.takePicture :: " + mCamera);
}
});
}
private Camera getCameraInstance() {
Camera camera = null;
Log.d(CameraLauncherActivity.LOG_TAG, "getCameraInstance()Camera:: " + camera);
try {
camera = Camera.open(0);
Log.d(CameraLauncherActivity.LOG_TAG, "getCameraInstance()open:: " + camera);
} catch (Exception e) {
// cannot get camera or does not exist
}
if(camera!=null){
// get supported sizes:
// List<Size> localSizes = camera.getParameters().getSupportedPreviewSizes();
// loop through localSizes to get closest size
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(640, 480);
camera.setParameters(parameters);
}
return camera;
}
PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
pictureFile = getOutputMediaFile();
camera.startPreview();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
PreviewSurface.java: -
public class PreviewSurface extends SurfaceView implements
SurfaceHolder.Callback {
public static final String LOG_TAG = "CameraPreview";
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;
// Constructor that obtains context and camera
@SuppressWarnings("deprecation")
public PreviewSurface(Context context, Camera camera) {
super(context);
this.mCamera = camera;
this.mSurfaceHolder = this.getHolder();
this.mSurfaceHolder.addCallback(this);
this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.mSurfaceHolder.setFixedSize(100, 100);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
Camera.Parameters parameters = mCamera.getParameters();
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
{
parameters.set("orientation", "portrait");
mCamera.setDisplayOrientation(90);
parameters.setRotation(90);
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
}
else
{
// This is an undocumented although widely known feature
parameters.set("orientation", "landscape");
// For Android 2.2 and above
mCamera.setDisplayOrientation(0);
// Uncomment for Android 2.0 and above
parameters.setRotation(0);
}
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (IOException e) {
// left blank for now
}
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
mCamera.stopPreview();
mCamera.release();
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
int width, int height) {
try {
Camera.Parameters parameters = mCamera.getParameters();
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
parameters.set("orientation", "portrait");
mCamera.setDisplayOrientation(90);
parameters.setRotation(90);
}
else {
// This is an undocumented although widely known feature
parameters.set("orientation", "landscape");
// For Android 2.2 and above
mCamera.setDisplayOrientation(0);
// Uncomment for Android 2.0 and above
parameters.setRotation(0);
}
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (IOException e) {
// left blank for now
}
}
的Manifest.xml: -
<activity
android:name="com.example.camera.CameraLauncherActivity"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" />
答案 0 :(得分:0)
是的,这就是它的工作方式,关闭活动,破坏表面。但如果您认为camera.open()太慢而无法经常调用,则可以将camera.release()
延迟到Activity.onStop()。