Android Custom Camera Camera a black screen solution _android

Source: Internet
Author: User
Tags abs

This article illustrates the solution to the black screen of the Android custom camera camera. Share to everyone for your reference, specific as follows:

For some phones, like HTC, when customizing camera, call Camera.parameters's parameters.setpreviewsize (width, height) method when the width and height are odd, There will be a black screen phenomenon, the solution can refer to the SDK provided by the Apidemos in the camera example:

List<size> sizes = parameters.getsupportedpreviewsizes ();
Size optimalsize = getoptimalpreviewsize (sizes, W, h);
Parameters.setpreviewsize (Optimalsize.width, optimalsize.height);

At the same time, setting the Parameters.setpicturesize (Width,height) attribute on the HTC phone also results in a black screen and no problem on the Samsung phone. And if you set the Setpreviewsize properties of the wide-height error, the photos will also be distorted and other bugs, so when the adaptation problem, the best way is not to set the Previewsize and Picturesize properties .

Getoptimalpreviewsize method

 private Size getoptimalpreviewsize (list<size> sizes, int w, int h) {Final double
 Aspect_tolerance = 0.05;
 Double targetratio = (double) w/h;
 if (sizes = null) return null;
 Size optimalsize = null;
 Double Mindiff = Double.max_value;
 int targetheight = h; Try to find a size match aspect ratio and size for (size size:sizes) {Double ratio = (double) size.width/size.h
 eight;
 if (Math.Abs (ratio-targetratio) > Aspect_tolerance) continue;
 if (Math.Abs (size.height-targetheight) < Mindiff) {optimalsize = size;
 Mindiff = Math.Abs (size.height-targetheight); }//Cannot find the one match the aspect ratio, ignore the requirement if (optimalsize = null) {Mindiff = Double.
 Max_value;
  for (size size:sizes) {if (Math.Abs (size.height-targetheight) < Mindiff) {optimalsize = size;
 Mindiff = Math.Abs (size.height-targetheight);
}} return optimalsize; }

For more information on Android-related content readers can view the site: "The Android photo and photo processing skills summary", "Android Development introduction and Advanced Course", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", " Android Basic Components Usage Summary, Android View tips Summary, Android layout layout tips and Android Control usage summary

I hope this article will help you with the Android program.

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.