Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. profile = CamcorderProfile.get(cameraid, CamcorderProfile.QUALITY_LOW);
  2. profile.videoFrameHeight=360;
  3. profile.videoFrameWidth=640;
  4.  
  5. Camera.Parameters param = mCamera.getParameters();
  6. param.set( "cam_mode", 1 );
  7. // Enable video stabilization. Convenience methods not available in API
  8. // level <= 14
  9. String vstabSupported = param.get("video-stabilization-supported");
  10. if ("true".equals(vstabSupported)) {
  11. param.set("video-stabilization", "true");
  12. }
  13. List<Size> sizes = mCamera.getParameters() .getSupportedVideoSizes();
  14. mCamera.setParameters( param );
  15. mMediaRecorder = new MediaRecorder();
  16. // Step 1: Unlock and set camera to MediaRecorder
  17. mCamera.unlock();
  18. mMediaRecorder.setCamera(mCamera);
  19. mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
  20. // Step 2: Set sources
  21. mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
  22. mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
  23. String deviceMan = android.os.Build.MANUFACTURER;
  24. Toast.makeText(getApplicationContext(), deviceMan, Toast.LENGTH_SHORT).show();
  25. // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
  26. CamcorderProfile profile = CamcorderProfile.get(cameraid, CamcorderProfile.QUALITY_LOW);
  27. // if(!CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_HIGH)){Log.d("", "the camcorder profile instance is null");
  28. int currentapiVersion = android.os.Build.VERSION.SDK_INT;
  29. if (currentapiVersion >= android.os.Build.VERSION_CODES.FROYO){
  30. // Do something for froyo and above versions
  31. boolean tellbol=CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_HIGH);
  32. if(deviceMan.equals("samsung")){
  33. profile = CamcorderProfile.get(cameraid, CamcorderProfile.QUALITY_LOW);
  34. profile.videoFrameHeight=360;
  35. profile.videoFrameWidth=640;
  36. }
  37. else{
  38. profile = CamcorderProfile.get(cameraid, CamcorderProfile.QUALITY_HIGH);
  39. }
  40. mMediaRecorder.setProfile(profile);
  41. } else{
  42. // do something for phones running an SDK before froyo
  43. mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  44. mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  45. // mMediaRecorder.setVideoSize(720, 480);
  46. mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
  47. }
  48.  
  49. // Step 4: Set output file
  50. mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
  51. // Step 5: Set the preview output
  52. // Step 6: Prepare configured MediaRecorder
  53. try {
  54. mMediaRecorder.prepare();
  55. } catch (IllegalStateException e) {
  56. Log.d("Video", "IllegalStateException preparing MediaRecorder: " + e.getMessage());
  57. releaseMediaRecorder();
  58. return false;
  59. } catch (IOException e) {
  60. Log.d("Video", "IOException preparing MediaRecorder: " + e.getMessage());
  61. releaseMediaRecorder();
  62. return false;
  63. }
  64. return true;
  65.  
  66. Camera.Parameters p = myCamera.getParameters();
  67. List<Size> previewsizes = p.getSupportedPreviewSizes();
  68. List<Size> videosizes = p.getSupportedVideoSizes();
  69.  
  70. private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
  71. final double ASPECT_TOLERANCE = 0.2;
  72. double targetRatio = (double) w / h;
  73. if (sizes == null)
  74. return null;
  75.  
  76. Size optimalSize = null;
  77. double minDiff = Double.MAX_VALUE;
  78.  
  79. int targetHeight = h;
  80.  
  81. // Try to find an size match aspect ratio and size
  82. for (Size size : sizes) {
  83. Log.d("Camera", "Checking size " + size.width + "w " + size.height
  84. + "h");
  85. double ratio = (double) size.width / size.height;
  86. if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
  87. continue;
  88. if (Math.abs(size.height - targetHeight) < minDiff) {
  89. optimalSize = size;
  90. minDiff = Math.abs(size.height - targetHeight);
  91. }
  92. }
  93.  
  94. // Cannot find the one match the aspect ratio, ignore the
  95. // requirement
  96. if (optimalSize == null) {
  97. minDiff = Double.MAX_VALUE;
  98. for (Size size : sizes) {
  99. if (Math.abs(size.height - targetHeight) < minDiff) {
  100. optimalSize = size;
  101. minDiff = Math.abs(size.height - targetHeight);
  102. }
  103. }
  104. }
  105. return optimalSize;
  106. }
  107.  
  108. private int desiredwidth=640, desiredheight=360;
  109.  
  110. Size optimalPreviewSize = getOptimalPreviewSize(previewsizes, desiredwidth, desiredheight);
  111.  
  112. Size optimalVideoSize = getOptimalPreviewSize(videosizes, desiredwidth, desiredheight);
  113.  
  114. p.setPreviewSize(optimalPreviewSize.width, optimalPreviewSize.height);
  115.  
  116. mCamera.unlock();
  117. mMediaRecorder = new MediaRecorder();
  118. mMediaRecorder.setCamera(mCamera);
  119.  
  120. mMediaRecorder.setVideoSize(optimalVideoSize.width, optimalVideoSize.height);
  121. myCamera.setParameters(p);
  122.  
  123. private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
  124. final double ASPECT_TOLERANCE = 0.2;
  125. double targetRatio = (double) w / h;
  126. if (sizes == null)
  127. return null;
  128.  
  129. Size optimalSize = null;
  130. double minDiff = Double.MAX_VALUE;
  131.  
  132. int targetHeight = h;
  133.  
  134. if ( you want ratio as closed to what i asked for)
  135. { for (Size size : sizes) {
  136. Log.d("Camera", "Checking size " + size.width + "w " + size.height
  137. + "h");
  138. double ratio = (double) size.width / size.height;
  139. if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
  140. continue;
  141. if (Math.abs(size.height - targetHeight) < minDiff) {
  142. optimalSize = size;
  143. minDiff = Math.abs(size.height - targetHeight);
  144. }
  145. }
  146. }
  147.  
  148. if (you want height as closed to what i asked for) { //you can do other for width
  149. minDiff = Double.MAX_VALUE;
  150. for (Size size : sizes) {
  151. if (Math.abs(size.height - targetHeight) < minDiff) {
  152. optimalSize = size;
  153. minDiff = Math.abs(size.height - targetHeight);
  154. }
  155. }
  156. }
  157.  
  158. if (you want the bigest one) {
  159. minDiff = 0;
  160. for (Size size : sizes) {
  161. if ( size.height * size.width > minDiff ) {
  162. optimalSize = size;
  163. minDiff = size.height * size.width ;
  164. }
  165. }
  166. }
  167. return optimalSize;
  168. }
  169.  
  170. Camera.Parameters param = camera.getParameters();
  171.  
  172. param.setPreviewSize("your width"
  173. "your height");
  174. camera.setParameters(param);
  175.  
  176. The video resolutions supported by the device is
  177.  
  178. 176x144
  179. 640x480
  180. 1280x720
  181.  
  182. The preview screen supported resolution is
  183.  
  184. 1280x960
  185. 1280x720
  186. 640x480
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement