Analysis of three sizes and three variants in Android Camera development (a tool class that contributes to the adaptation of Picturesize and Previewsize)

Source: Internet
Author: User

Go to (http://blog.csdn.net/yanzi1225627/article/details/17652643)

Often listen to people ask camera development, all kinds of deformation problems, today there is time to comb the summary under this.

Three sizes:

1, the size of the Surfaceview

Surfaceview is used to preview the camera, which is the size of screen when it is fullscreen.

2, the size of the Picturesize

This is the picturesize size after the photograph.

3, the size of the Previewsize

This is the size of the frame data when previewing.

Three variants:

1, the preview screen object length and width distortion. The reason is that the surfaceview and previewsize have inconsistent length-to-width ratios, which can cause distortion on the preview screen. Because the camera will scale the preview frame data to Surfaceview size when it is displayed, the object will deform when the ratio is inconsistent. As for this scaling algorithm, there may be differences between phones.

2, photo after the object deformation.

3, click on the photo moment, the preview screen will suddenly deform and then return to the normal preview.

2, 32 kinds of deformation are due to the previewsize and picturesize of the length and width ratio inconsistency. In a word, in order to ensure that the picture is not true, to ensure that the preview Surfaceview, Previewsize, picturesize of the length and width ratio to be. If Surfaceview is full screen, get the screen length and width to get ratio.

For this I have written a tool class, get the appropriate picturesize and Previewsize, the name is: Mycampara.java.

[Java]View Plaincopyprint?
  1. Package org.yanzi.util;
  2. Import java.util.Collections;
  3. Import Java.util.Comparator;
  4. Import java.util.List;
  5. Import Android.hardware.Camera;
  6. Import android.hardware.Camera.Size;
  7. Import Android.util.Log;
  8. Public class Mycampara {
  9. private static final String tag = "Yan";
  10. private Camerasizecomparator sizecomparator = new Camerasizecomparator ();
  11. private static Mycampara Mycampara = null;
  12. Private Mycampara () {
  13. }
  14. public static Mycampara getinstance () {
  15. if (Mycampara = = null) {
  16. Mycampara = new Mycampara ();
  17. return Mycampara;
  18. }
  19. else{
  20. return Mycampara;
  21. }
  22. }
  23. Public Size getpreviewsize (list<camera.size> List, int th) {
  24. Collections.sort (list, sizecomparator);
  25. int i = 0;
  26. For (Size s:list) {
  27. if ((s.width > Th) && equalrate (S, 1.33f)) {
  28. LOG.I (Tag, "final set Preview size: w =" + S.width + "h =" + S.height);
  29. Break ;
  30. }
  31. i++;
  32. }
  33. return List.get (i);
  34. }
  35. Public Size getpicturesize (list<camera.size> List, int th) {
  36. Collections.sort (list, sizecomparator);
  37. int i = 0;
  38. For (Size s:list) {
  39. if ((s.width > Th) && equalrate (S, 1.33f)) {
  40. LOG.I (Tag, "Final set image size: W =" + S.width + "h =" + S.height);
  41. Break ;
  42. }
  43. i++;
  44. }
  45. return List.get (i);
  46. }
  47. Public Boolean equalrate (Size s, float rate) {
  48. float R = (float) (s.width)/(float) (s.height);
  49. if (Math.Abs (r-rate) <= 0.2)
  50. {
  51. return true;
  52. }
  53. else{
  54. return false;
  55. }
  56. }
  57. public class Camerasizecomparator implements comparator<camera.size>{
  58. //sorted in ascending order
  59. public int compare (size lhs, size rhs) {
  60. //TODO auto-generated method stub
  61. if (lhs.width = = rhs.width) {
  62. return 0;
  63. }
  64. Else if (Lhs.width > Rhs.width) {
  65. return 1;
  66. }
  67. else{
  68. return-1;
  69. }
  70. }
  71. }
  72. }


The above tool class comments are as follows:

1, is a singleton mode class. Invocation Example:

[Java]View Plaincopyprint?
    1. Size PictureS = Mycampara.getinstance (). Getpicturesize (picturesizes, 800);
    2. Myparam.setpicturesize (Pictures.width, pictures.height);


2, the program first to preview the size of the list in ascending order, because the actual found that some mobile phones provide ascending or descending.

3, the length-to-width ratio of the size is guaranteed by equalrate (size S, float rate). In general, this ratio is 1.333/1.7777, which is generally said to be 4:3 and 16:9 ratios.

4, in the Getpreviewsize () function to increase the judgment if ((s.width > Th) && equalrate (S, 1.33f)), in addition to the guarantee ratio, but also to ensure that the user needs to set the minimum size width. This is to be changed as needed.

If you think the author is serious in writing a blog, please vote for me, thank you.

Analysis of three sizes and three variants in Android Camera development (a tool class that contributes to the adaptation of Picturesize and Previewsize)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.