<span style="font-family: Arial, Helvetica, sans-serif;"> 前几天遇到一个问题是开发做了一个功能 却无法在全部手机上实现打开相机都能打开的情况 查了下相机支持的频率 发现有些相机支持115种 比如小米2s</span>
而有些手机相机只支持几种 鉴于这个情况 我们就要考虑下这里面该如何做
下面新建一个相机工程解决下这个问题
main_actvity的xml代码
<span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:onClick="openCamera"
android:layout_marginBottom="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:textColor="#ffffff"
android:textSize="18sp"
android:text="点击开启照相功能"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout></span>
效果图
下面加载下这个布局
package com.akira.camerademo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>protected void onCreate(Bundle savedInstanceState) {
<span style="white-space:pre"> </span>super.onCreate(savedInstanceState);
<span style="white-space:pre"> </span>setContentView(R.layout.activity_main);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>public void openCamera(View view){
<span style="white-space:pre"> </span>startActivity(new Intent(MainActivity.this,CanAct.class));
<span style="white-space:pre"> </span> finish();<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
}
这里偷了个懒 直接掉onclick 实际开发中千万别这么干
下面另外一个照相机的布局
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/mSurface"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button
android:id="@+id/bt_take"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="照相"
/>
</LinearLayout></span>
这里也是莫名的简单 调用weight去适配当前比例
接下来去加载布局 调用相机
相机代码
package com.akira.camerademo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class CanAct extends Activity {
private Camera mCamera;
private SurfaceHolder.Callback mCallback;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.can);
SurfaceView mSurfaceView = (SurfaceView) findViewById(R.id.mSurface);
SurfaceHolder holder = mSurfaceView.getHolder();
holder.addCallback(mCallback);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Button take = (Button) findViewById(R.id.bt_take);
take.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
takePic();
}
});
mCallback = new Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera=null;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Camera.Parameters mParams = mCamera.getParameters();
List<Camera.Size> supportedPreviewSizes = mParams.getSupportedPreviewSizes();
Camera.Size mSize = supportedPreviewSizes.get(0);
mParams.setPictureSize(mSize.width, mSize.height);
mParams.setPictureFormat(PixelFormat.JPEG);
//支持的相机频率 方法过时
List<Integer> supportedPreviewFrameRates = mParams.getSupportedPreviewFrameRates();
mParams.setPreviewFrameRate(supportedPreviewFrameRates.get(0));
mCamera.setParameters(mParams);
mCamera.startPreview();
}
};
}
Camera.PictureCallback mPictureCallback =new PictureCallback() {
/**
* 照相回调
*/
@Override
public void onPictureTaken(byte[] data, Camera camera) {
new SavePictureTask().execute(data);
camera.startPreview();
}
};
/**
* 照相
*/
private void takePic() {
mCamera.stopPreview();
mCamera.takePicture(null, null, mPictureCallback);
}
class SavePictureTask extends AsyncTask<byte[], String, String>{
@Override
protected String doInBackground(byte[]... params) {
File pic = new File(Environment.getExternalStorageDirectory(),"pic.jpg");
if(pic.exists())
pic.delete();
try {
FileOutputStream fos = new FileOutputStream(pic.getPath());
fos.write(params[0]);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
}
但是 这里出了点问题
总之这里的思路是
List<Integer> supportedPreviewFrameRates = mParams.getSupportedPreviewFrameRates();
得到频率 然后去设置频率
项目中要求频率是25 但如果真实频率没有25 那么就找最近的
但是 这里出了错希望大牛可以帮我解决一下 项目中是没问题的