android.hardware.camera2这个包提供了相机设备和安卓设备连接的接口,他的存在弃用了原有的Camera相机。
和原有的旧API中的camera不同的是,camera2大大的提升了拍照的速度。并且值得一提的是,使用原来的camera相机拍完的照片仅仅是一个纯纯的图片文件,也就时说不包含图片的Exif信息,那么什么是图片的Exif信息呢,简单来说就是:
正如你看到的,就是这些东西,或许你觉得这东西没什么必要,但就是这些信息曾卡住我加了一个周末的班才解决!!回想都是泪啊!
不过言归正传,这篇博客的主题是使用新的camera2开发自定义相机。
经过这两天的研究,自定义相机差不多需要5个步骤:
①创建一个TextureView用来显示相机的预览
②得到CameraManager对象通过相机硬件ID打开相机设备(打开成功会得到一个CameraDevice对象)
③用相机设备(CameraDevice对象)创建会话,通过会话发送相机预览的请求进行预览(中间可以加上种种的参数什么的,你要求相机设备怎么做他就怎么做)
⑤拍照也是一样发送请求,然后通过ImageReader保存返回的图片数据。
明确了自定义相机的步骤,接下来就好办了,敲代码。。。
首先我们使用一个Fragment来显示自定义相机的界面,
fragment XML代码:
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.miser.ceamera2demo.Camera2Fragment">
<TextureView
android:id="@+id/tv_textview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ImageView
android:id="@+id/iv_Thumbnail"
android:layout_width="60dp"
android:layout_height="80dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="44dp"
android:background="#F1F2aa" />
<Button
android:id="@+id/btn_takepic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="拍照" />
RelativeLayout>
LinearLayout>
简单我们在XML中定义了一个TextureView一个ImageView一个Button,TextureView用来显示相机的预览,ImageView用来显示一个拍照完成的缩略图,Button当然就是用来点击拍照的啦。
Fragment Java代码
public class Camera2Fragment extends Fragment {
private static final String TAG = "Camera2Fragment";
private static final int SETIMAGE = 1;
TextureView mTextureView;
ImageView mThumbnail;
Button mButton;
Handler mHandler;
Handler mUIHandler;
ImageReader mImageReader;
CaptureRequest.Builder mPreViewBuidler;
CameraCaptureSession mCameraSession;
CameraCharacteristics mCameraCharacteristics;
Ringtone ringtone;
//相机会话的监听器,通过他得到mCameraSession对象,这个对象可以用来发送预览和拍照请求
private CameraCaptureSession.StateCallback mSessionStateCallBack = new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession cameraCaptureSession) {
try {
mCameraSession = cameraCaptureSession;
cameraCaptureSession.setRepeatingRequest(mPreViewBuidler.build(), null, mHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
}
};
//打开相机时候的监听器,通过他可以得到相机实例,这个实例可以创建请求建造者
private CameraDevice.StateCallback cameraOpenCallBack = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice cameraDevice) {
Log.d(TAG, "相机已经打开");
try {
mPreViewBuidler = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
SurfaceTexture texture = mTextureView.getSurfaceTexture();
texture.setDefaultBufferSize(mPreViewSize.getWidth(), mPreViewSize.getHeight());
Surface surface = new Surface(texture);
mPreViewBuidler.addTarget(surface);
cameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()), mSessionStateCallBack, mHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onDisconnected(CameraDevice cameraDevice) {
Log.d(TAG, "相机连接断开");
}
@Override
public void onError(CameraDevice cameraDevice, int i) {
Log.d(TAG, "相机打开失败");
}
};
private ImageReader.OnImageAvailableListener onImageAvaiableListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader imageReader) {
mHandler.post(new ImageSaver(imageReader.acquireNextImage()));
}
};
private Size mPreViewSize;
//预览图显示控件的监听器,可以监听这个surface的状态
private TextureView.SurfaceTextureListener mSurfacetextlistener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
HandlerThread thread = new HandlerThread("Ceamera3");
thread.start();
mHandler = new Handler(thread.getLooper());
CameraManager manager = (CameraManager) getActivity().getSystemService(Context.CAMERA_SERVICE);
String cameraid = CameraCharacteristics.LENS_FACING_FRONT + "";
try {
mCameraCharacteristics = manager.getCameraCharacteristics(cameraid);
StreamConfigurationMap map = mCameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
Size largest = Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), new CompareSizeByArea());
mPreViewSize = map.getOutputSizes(SurfaceTexture.class)[0];
mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), ImageFormat.JPEG, 5);
mImageReader.setOnImageAvailableListener(onImageAvaiableListener, mHandler);
manager.openCamera(cameraid, cameraOpenCallBack, mHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
};
private View.OnClickListener picOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
shootSound();
Log.d(TAG, "正在拍照");
CaptureRequest.Builder builder = mCameraSession.getDevice().createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
builder.addTarget(mImageReader.getSurface());
builder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_AUTO);
builder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CameraMetadata.CONTROL_AF_TRIGGER_START);
builder.set(CaptureRequest.JPEG_ORIENTATION, 90);
mCameraSession.capture(builder.build(), null, mHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_camera2, null);
findview(v);
mUIHandler = new Handler(new InnerCallBack());
//初始化拍照的声音
ringtone = RingtoneManager.getRingtone(getActivity(), Uri.parse("file:///system/media/audio/ui/camera_click.ogg"));
AudioAttributes.Builder attr = new AudioAttributes.Builder();
attr.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION);
ringtone.setAudioAttributes(attr.build());
//初始化相机布局
mTextureView.setSurfaceTextureListener(mSurfacetextlistener);
//设置点击拍照的监听
mButton.setOnClickListener(picOnClickListener);
return v;
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (mCameraSession != null) {
mCameraSession.getDevice().close();
mCameraSession.close();
}
}
private void findview(View v) {
mTextureView = (TextureView) v.findViewById(R.id.tv_textview);
mButton = (Button) v.findViewById(R.id.btn_takepic);
mThumbnail = (ImageView) v.findViewById(R.id.iv_Thumbnail);
mThumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "别戳了,那个页面还没写", Toast.LENGTH_SHORT).show();
}
});
}
/**
* 播放系统的拍照的声音
*/
public void shootSound() {
ringtone.stop();
ringtone.play();
}
private class ImageSaver implements Runnable {
Image reader;
public ImageSaver(Image reader) {
this.reader = reader;
}
@Override
public void run() {
Log.d(TAG, "正在保存图片");
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsoluteFile();
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, System.currentTimeMillis() + ".jpg");
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
ByteBuffer buffer = reader.getPlanes()[0].getBuffer();
byte[] buff = new byte[buffer.remaining()];
buffer.get(buff);
BitmapFactory.Options ontain = new BitmapFactory.Options();
ontain.inSampleSize = 50;
Bitmap bm = BitmapFactory.decodeByteArray(buff, 0, buff.length, ontain);
Message.obtain(mUIHandler, SETIMAGE, bm).sendToTarget();
outputStream.write(buff);
Log.d(TAG, "保存图片完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
private class InnerCallBack implements Handler.Callback {
@Override
public boolean handleMessage(Message message) {
switch (message.what) {
case SETIMAGE:
Bitmap bm = (Bitmap) message.obj;
mThumbnail.setImageBitmap(bm);
break;
}
return false;
}
}
}
其中我们使用到了4个非常重要的监听器:
mSurfacetextlistener:
设置在TextureView上的监听器当我们的TextureView已经准被好可以往上绘制东西的时候(也就时触发onSurfaceTextureAvailable事件),我们就可以进行初始化CameraManager、openCamera等等的一些操作了。
cameraOpenCallBack:
打开相机时候的监听器,如果打开成功,也就是会触发onOpened的时候,我们就可以得到cameraDevice对象了,cameraDevice对象有什么用呢,他可以进行一步非常重要的操作,createCaptureSession,创建一个与你的相机设备通信的一个会话,有了它你才可以进行后面的预览,拍照,设置相机参数的操作。
mSessionStateCallBack:
相机会话的监听器,当onConfigured时,他会得到一个CameraCaptureSession对象,这个对象就是用来发送预览和拍照请求的啦。
onImageAvaiableListener:是你点击拍照 mCameraSession.capture(…)的时候要用到的,他会返回你本次拍照的图片数据,你可以通过文件流把他输出成JPEG格式的图片文件。
最后在你的自定义相机使用完的时候要记得及时释放资源
if (mCameraSession != null) {
mCameraSession.getDevice().close();
mCameraSession.close();
}
因为相机设备是对你手机上面的程序来说是个公共的硬件资源。不释放下次就无法打开相机了,因为它还是再被占用着。
最后别忘了加上权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.CAMERA">uses-permission>
好,大功告成!
Demo地址http://download.csdn.net/detail/qq_27512671/9477936
2016年12月9日11:00:22 补充
这个项目会在我的Github上更新,欢迎pull request:
https://github.com/miqt/camera2