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