Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 KB | None | 0 0
  1. class Preview extends ViewGroup implements SurfaceHolder.Callback {
  2. private final String TAG = "Preview";
  3. SurfaceView mSurfaceView;
  4. SurfaceHolder mHolder;
  5. Camera.Size mPreviewSize;
  6. List<Camera.Size> mSupportedPreviewSizes;
  7. Activity mAcivity;
  8. Camera mCamera;
  9. public int currentCameraId;
  10.  
  11. Preview(Context context, SurfaceView sv, Activity activity) {
  12. super(context);
  13. mSurfaceView = sv;
  14. mAcivity = activity;
  15. // addView(mSurfaceView);
  16. mHolder = mSurfaceView.getHolder();
  17. mHolder.addCallback(this);
  18. mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  19. }
  20.  
  21. public void setCamera(Camera camera) {
  22. mCamera = camera;
  23. if (mCamera != null) {
  24. mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
  25. requestLayout();
  26. // get Camera parameters
  27. Camera.Parameters params = mCamera.getParameters();
  28. List<String> focusModes = params.getSupportedFocusModes();
  29. if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
  30. // set the focus mode
  31. params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
  32. // set Camera parameters
  33. mCamera.setParameters(params);
  34. }
  35. }
  36. }
  37.  
  38. @Override
  39. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  40.  
  41. final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
  42. final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
  43. setMeasuredDimension(width, height);
  44. if (mSupportedPreviewSizes != null) {
  45. mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
  46. }
  47. }
  48.  
  49. @Override
  50. protected void onLayout(boolean changed, int l, int t, int r, int b) {
  51. if (changed && getChildCount() > 0) {
  52. final View child = getChildAt(0);
  53.  
  54. final int width = r - l;
  55. final int height = b - t;
  56.  
  57. int previewWidth = width;
  58. int previewHeight = height;
  59. if (mPreviewSize != null) {
  60. previewWidth = mPreviewSize.width;
  61. previewHeight = mPreviewSize.height;
  62. }
  63.  
  64. // Center the child SurfaceView within the parent.
  65. if (width * previewHeight > height * previewWidth) {
  66. final int scaledChildWidth = previewWidth * height / previewHeight;
  67. child.layout((width - scaledChildWidth) / 2, 0,
  68. (width + scaledChildWidth) / 2, height);
  69. } else {
  70. final int scaledChildHeight = previewHeight * width / previewWidth;
  71. child.layout(0, (height - scaledChildHeight) / 2,
  72. width, (height + scaledChildHeight) / 2);
  73. }
  74. }
  75. }
  76.  
  77. public void surfaceCreated(SurfaceHolder holder) {
  78.  
  79.  
  80. try {
  81. if (mCamera != null) {
  82. mCamera.setPreviewDisplay(holder);
  83. Camera.CameraInfo info = new Camera.CameraInfo();
  84. Camera.getCameraInfo(currentCameraId, info);
  85. if (info.canDisableShutterSound) {
  86. SharedPreferences prefs = mAcivity.getSharedPreferences("myprefs", Context.MODE_PRIVATE);
  87.  
  88. mCamera.enableShutterSound(prefs.getBoolean("shutter",true));
  89. }
  90. Camera.Parameters parameters = mCamera.getParameters();
  91. CameraActivity.setParams(parameters.getPreviewSize());
  92. }
  93. } catch (IOException exception) {
  94. Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
  95. }
  96.  
  97.  
  98. }
  99.  
  100. public void surfaceDestroyed(SurfaceHolder holder) {
  101.  
  102. if (mCamera != null) {
  103. mCamera.stopPreview();
  104. }
  105. }
  106.  
  107. private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
  108. final double ASPECT_TOLERANCE = 0.1;
  109. double targetRatio = (double) w / h;
  110. if (sizes == null) return null;
  111. Camera.Size optimalSize = null;
  112. double minDiff = Double.MAX_VALUE;
  113. int targetHeight = h;
  114.  
  115. for (Camera.Size size : sizes) {
  116. double ratio = (double) size.width / size.height;
  117. if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
  118. if (Math.abs(size.height - targetHeight) < minDiff) {
  119. optimalSize = size;
  120. minDiff = Math.abs(size.height - targetHeight);
  121. }
  122. }
  123.  
  124. if (optimalSize == null) {
  125. minDiff = Double.MAX_VALUE;
  126. for (Camera.Size size : sizes) {
  127. if (Math.abs(size.height - targetHeight) < minDiff) {
  128. optimalSize = size;
  129. minDiff = Math.abs(size.height - targetHeight);
  130. }
  131. }
  132. }
  133. return optimalSize;
  134. }
  135.  
  136. public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  137. if (mCamera != null) {
  138. Camera.Parameters parameters = mCamera.getParameters();
  139.  
  140. requestLayout();
  141.  
  142. List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
  143.  
  144.  
  145. Camera.Size previewSize =previewSizes.get(0);
  146.  
  147. parameters.setPreviewSize(previewSize.width, previewSize.height);
  148. mCamera.setParameters(parameters);
  149.  
  150. mCamera.setDisplayOrientation(getRoatationAngle(mAcivity, currentCameraId));
  151. mCamera.startPreview();
  152.  
  153. }
  154. }
  155.  
  156. public static int getRoatationAngle(Activity mContext, int cameraId) {
  157. android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
  158. android.hardware.Camera.getCameraInfo(cameraId, info);
  159. int rotation = mContext.getWindowManager().getDefaultDisplay().getRotation();
  160. int degrees = 0;
  161. switch (rotation) {
  162. case Surface.ROTATION_0:
  163. degrees = 0;
  164. break;
  165. case Surface.ROTATION_90:
  166. degrees = 90;
  167. break;
  168. case Surface.ROTATION_180:
  169. degrees = 180;
  170. break;
  171. case Surface.ROTATION_270:
  172. degrees = 270;
  173. break;
  174. }
  175. int result;
  176. if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
  177. result = (info.orientation + degrees) % 360;
  178. result = (360 - result) % 360; // compensate the mirror
  179. } else { // back-facing
  180. result = (info.orientation - degrees + 360) % 360;
  181. }
  182. return result;
  183. }
  184.  
  185. public void setCurrnatCamera(int id) {
  186. if (id != -1) {
  187. currentCameraId = id;
  188. } else {
  189. currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
  190. }
  191.  
  192. }
  193. }
  194.  
  195. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  196. android:id="@+id/FrameLayout01"
  197. android:layout_width="fill_parent"
  198. android:background="?attr/colorPrimary"
  199. android:layout_height="fill_parent">
  200.  
  201. <RelativeLayout
  202. android:id="@+id/rl1"
  203. android:layout_width="fill_parent"
  204. android:layout_height="fill_parent">
  205.  
  206.  
  207. <FrameLayout
  208. android:id="@+id/previewContainer"
  209. android:layout_width="fill_parent"
  210. android:layout_height="fill_parent"
  211. >
  212.  
  213. <SurfaceView
  214. android:id="@+id/SurfaceView01"
  215. android:layout_width="fill_parent"
  216.  
  217. android:layout_height="fill_parent"
  218. />
  219. </FrameLayout>
  220.  
  221.  
  222.  
  223. <RelativeLayout
  224. android:id="@+id/handle"
  225. android:layout_width="match_parent"
  226. android:layout_height="wrap_content"
  227. android:layout_alignParentBottom="true"
  228. android:layout_gravity="center_horizontal|bottom"
  229. android:background="?attr/colorPrimary"
  230. android:orientation="horizontal"
  231. android:padding="5dp"
  232. >
  233.  
  234. <ImageButton
  235. android:id="@+id/button"
  236. android:layout_width="80dp"
  237. android:layout_height="80dp"
  238. android:layout_centerInParent="true"
  239. android:scaleType="fitXY"
  240.  
  241. android:background="@drawable/photo"
  242. />
  243.  
  244. <ImageButton
  245. android:id="@+id/recam"
  246.  
  247. android:layout_width="50dp"
  248. android:layout_height="50dp"
  249. android:background="@drawable/change"
  250. android:layout_centerVertical="true"
  251. />
  252. </RelativeLayout>
  253. </RelativeLayout>
  254. </FrameLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement