由于Android自身的关系,camera必须在landscape模式下才可以显示正常,如果是在portrait模式下,就要把脖子转90度才能“正常”观看了

听说Android的SDK升级到1.5(cupcake)后已经可以解决这个问题了,就上网找了一下,发现有些人说可以尝试使用parameters.set("rotation", 90),试了一下,无效。

最近由于项目原因,继续研究了下,找到了一个“曲线救国”的方法
parameters.set("orientation", "portrait");

由于这个方法没有出现在Android的官方的api文档中,所以可能会有些问题。。。吧
谁知道呢,先用着再说 :wink:

(哎,这个方法被2.0毙了)

[size=large][b]-----------------------更新-----------------------[/b][/size]

通过反射,貌似可以解决[color=red][b]预览[/b][/color]
1.5 - [color=green]OK[/color]
1.6 - [color=green]OK[/color]
2.2 - [color=green]OK[/color]
2.3 - [color=green]OK[/color]
4.0 - [color=green]OK[/color]
其余的没有测试,希望有能力的朋友帮忙测一下,然后给个结果


public class CameraView extends SurfaceView implements SurfaceHolder.Callback {

/**
* This is a holder that holding a display surface.
*/
private SurfaceHolder mHolder = null;

private int sdk = 3;

/**
* This is a camera object using for connect/disconnect with the camera
* service,and so on.
*/
private Camera mCamera;

/**
* Perform inflation from XML and apply a class-specific base style. This
* constructor of View allows subclasses to use their own base style when
* they are inflating.
*
* @param context
* The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs
* The attributes of the XML tag that is inflating the view.
* @param defStyle
* The default style to apply to this view. If 0, no style will
* be applied (beyond what is included in the theme). This may
* either be an attribute resource, whose value will be retrieved
* from the current theme, or an explicit style resource.
*/
public CameraView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

/**
* Constructor that is called when inflating a view from XML. This is called
* when a view is being constructed from an XML file, supplying attributes
* that were specified in the XML file. This version uses a default style of
* 0, so the only attribute values applied are those in the Context's Theme
* and the given AttributeSet.
*
* @param context
* The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs
* The attributes of the XML tag that is inflating the view.
*/
public CameraView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

/**
* Simple constructor to use when creating a view from code.
*
* @param context
* The Context the view is running in, through which it can
* access the current theme, resources, etc.
*/
public CameraView(Context context) {
super(context);
init();
}

public void init() {
if (mHolder == null) {
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

sdk = getSDKInt();
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {

try {

// rotate camera 90 degree on portrait mode
if (getContext().getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {

if (sdk <= 4) {

// 1.5 & 1.6
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("orientation", "portrait");
mCamera.setParameters(parameters);
} else {
setDisplayOrientation(mCamera, 90);
}
}

} catch (Exception e) {
e.printStackTrace();
}

startPreview();
}

/**
* rotate camera with any degree, only available for SDK 5 and later
*
* @param camera
* @param angle
*/
private void setDisplayOrientation(Camera camera, int angle) {
Method downPolymorphic;

if (sdk <= 4)
return;

try {
if (sdk > 4 && sdk < 8) {

// parameters for pictures created by a Camera service.
Camera.Parameters parameters = mCamera.getParameters();

// 2.0, 2.1
downPolymorphic = parameters.getClass().getMethod(
"setRotation", new Class[] { int.class });
if (downPolymorphic != null)
downPolymorphic.invoke(parameters, new Object[] { angle });

// Sets the Parameters for pictures from this Camera
// service.
mCamera.setParameters(parameters);

} else {

downPolymorphic = camera.getClass().getMethod(
"setDisplayOrientation", new Class[] { int.class });
if (downPolymorphic != null)
downPolymorphic.invoke(camera, new Object[] { angle });
}
} catch (Exception e) {
}
}

@Override
public void surfaceCreated(SurfaceHolder holder) {

// get Camera object.
try {
mCamera = Camera.open();
mCamera.setPreviewDisplay(holder);

} catch (RuntimeException e) {
e.printStackTrace();
releaseCamera();
} catch (IOException e) {
e.printStackTrace();
releaseCamera();
}
}

public void stopPreview() {
if (mCamera != null) {
mCamera.stopPreview();
}
}

public void startPreview() {
if (mCamera != null) {
mCamera.startPreview();
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
releaseCamera();
}

private void releaseCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
}
mCamera = null;
System.gc();
}

private int getSDKInt() {
// this is safe so that we don't need to use SDKInt which is only
// available after 1.6
try {
return Integer.parseInt(Build.VERSION.SDK);
} catch (Exception e) {
return 3; // default to target 1.5 cupcake
}
}
}

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. android上一些方法的区别和用法的注意事项
  5. Android(安卓)--- Activity生命周期
  6. android实现字体闪烁动画的方法
  7. Android中dispatchDraw分析
  8. 锁屏界面
  9. 关于Android(安卓)Studio3.2新建项目Android(安卓)resource link

随机推荐

  1. mysql函数拼接查询concat函数的使用方法
  2. 解决MYSQL连接端口被占引入文件路径错误
  3. windows server 2008 64位MySQL5.6免安装
  4. 2017最新版windows安装mysql教程
  5. Mysql使用insert插入多条记录 批量新增数
  6. MySQL启动时InnoDB引擎被禁用了的解决方
  7. 解决Mysql收缩事务日志和日志文件过大无
  8. mysql的存储过程、游标 、事务实例详解
  9. MySQL5.7中 performance和sys schema中的
  10. 寻找sql注入的网站的方法(必看)