Processing of distorted display problems on Surfaceview after capturing images on Android

Source: Internet
Author: User

We often use Surfaceview to write custom cameras in Android, but sometimes the image will be distorted, and we will be very depressed to ask why this is why? In fact, the most fundamental reason is that the size of Surfaceview and previewsize is not the same ratio.

The so-called previewsize is the size of the frame data at the time of the preview, Surfaceview is used to preview the camera's view, which is the size of screen fullscreen. Another important size is picturesize, which is the size of the picture after the photo, that is, the image of the saved result.

So solving just the problem is to call the camera's Takepicture method before you set the camera's picturesize and previewsize two parameters, respectively, the method is called Setpicturesize and setpreviewsize.

Then the actual picturesize and previewsize can be obtained by the following steps:

The first is to get a range of sizes that the phone can fit

list<size> previewsizes = params.getsupportedpreviewsizes (); List<Size> picturesizes = params.getsupportedpicturesizes ();  

And then get the size of your own screen

 Public Static float getscreenrate (Context context) {       = getscreenmetrics (context);        float H = p.y;        float W = p.x;        return (w/H);  


Finally, according to their own size, from the appropriate size to take some of their own specifications of the size

 PublicSize getproppreviewsize (list<camera.size> List,floatThintminWidth)       {Collections.sort (list, sizecomparator); inti = 0;  for(Size s:list) {if((s.width >= minWidth) &&equalrate (s, th)) {log.i (TAG,"Previewsize:w =" + S.width + ", h =" +s.height);  Break; } I++; }      if(i = =list.size ()) {i= 0;//If you don't find it, choose the smallest size.    }      returnList.get (i); }  


After getting previewsize, the same get picturesize

 PublicSize getproppicturesize (list<camera.size> List,floatThintminWidth)      {Collections.sort (list, sizecomparator); inti = 0;  for(Size s:list) {if((s.width >= minWidth) &&equalrate (s, th)) {log.i (TAG,"Picturesize:w =" + S.width + ", h =" +s.height);  Break; } I++; }      if(i = =list.size ()) {i= 0;//If you don't find it, choose the smallest size.    }      returnList.get (i); }  


Set Dimensions

Mparams = mcamera.getparameters (); // Set Picturesize   Size picturesize = getproppicturesize (mparams.getsupportedpicturesizes (), previewrate, 1280x720//  Set PreviewsizeSize previewsize = getproppreviewsize (mparams.getsupportedpreviewsizes (), previewrate, 1280x720);  


The problem with the distorted display on the Surfaceview after Android raised camera captures the image is basically solved!

Processing of distorted display problems on Surfaceview after capturing images on Android

Related Article

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.