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; } } }
时间: 2024-10-05 22:16:27