1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback, CameraCallback {
private static final String TAG = CameraSurfaceView.class.getSimpleName(); SurfaceHolder mSurfaceHolder; private Context mContext; private Handler mHandler;
private boolean hasSurface; private CameraManager mCameraManager; private int mRatioWidth = 0; private int mRatioHeight = 0; private int mSurfaceWidth; private int mSurfaceHeight;
public CameraSurfaceView(Context context) { super(context); init(context); }
public CameraSurfaceView(Context context, AttributeSet attrs) { super(context, attrs); init(context); }
public CameraSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); }
private void init(Context context) { mContext = context; mHandler = new Handler(context.getMainLooper()); mSurfaceHolder = getHolder(); mSurfaceHolder.setFormat(PixelFormat.TRANSPARENT); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mSurfaceHolder.addCallback(this); mCameraManager = new CameraManager(context); mCameraManager.setCameraCallback(this); }
public CameraManager getCameraManager() { return mCameraManager; }
@Override public void surfaceCreated(SurfaceHolder holder) { Logs.i(TAG, "surfaceCreated..." + hasSurface); if (!hasSurface && holder != null) { hasSurface = true; openCamera(); } }
@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Logs.i(TAG, "surfaceChanged [" + width + ", " + height + "]"); mSurfaceWidth = width; mSurfaceHeight = height; }
@Override public void surfaceDestroyed(SurfaceHolder holder) { Logs.v(TAG, "surfaceDestroyed."); closeCamera(); hasSurface = false; }
public SurfaceHolder getSurfaceHolder() { return mSurfaceHolder; }
public void onResume() { if (hasSurface) { openCamera(); } }
public void onPause() { closeCamera(); }
private void openCamera() { if (mSurfaceHolder == null) { Logs.e(TAG, "SurfaceHolder is null."); return; } if (mCameraManager.isOpen()) { Logs.w(TAG, "Camera is opened!"); return; } mCameraManager.openCamera(); }
private void closeCamera() { mCameraManager.releaseCamera(); }
private String getString(int resId) { return getResources().getString(resId); }
private void setAspectRatio(int width, int height) { if (width < 0 || height < 0) { throw new IllegalArgumentException("Size cannot be negative."); } mRatioWidth = width; mRatioHeight = height; requestLayout(); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); if (0 == mRatioWidth || 0 == mRatioHeight) { setMeasuredDimension(width, height); } else { if (width < height * mRatioWidth / mRatioHeight) { setMeasuredDimension(width, width * mRatioHeight / mRatioWidth); } else { setMeasuredDimension(height * mRatioWidth / mRatioHeight, height); } } }
@Override public void onOpen() { mCameraManager.startPreview(getSurfaceHolder()); }
@Override public void onOpenError(int error, String msg) { }
@Override public void onPreview(int previewWidth, int previewHeight) { if (mSurfaceWidth > mSurfaceHeight) { setAspectRatio(previewWidth, previewHeight); } else { setAspectRatio(previewHeight, previewWidth); } }
@Override public void onPreviewError(int error, String msg) { }
@Override public void onClose() { } }
|