Android calls camera error setparameters failed deep parsing

Source: Internet
Author: User
Tags deprecated

1. Camera

Camera is a device supported within the Android framework that allows you to take photos and shoot videos, so there are always problems with camera development, such as the following:

E/androidruntime (1542): Java.lang.RuntimeException:setParameters failed
E/androidruntime (1542): at android.hardware.Camera.native_setParameters (native Method)
E/androidruntime (1542): at Android.hardware.Camera.setParameters (camera.java:914)
This error has occurred. According to the error, we can know that the Setparameters method of Android is wrong.

2, how to solve it?

We know that the camera parameters also has a lot of parameters set, which is wrong? Very many people do not know why the Internet started to find, can not find the start of various push, a number of parameters set in the past, in fact, the most effective way is the layer to find the reason. OK, let's open the Android code to find the camera class. Then find the setparameters method.

Private native final void Native_setparameters (String params);    /**     * Changes the settings for this Camera service.     *     * @param params the Parameters to use for this Camera service     * @throws runtimeexception If any parameter is INV Alid or not supported.     * @see #getParameters ()     */public    void Setparameters (Parameters params) {        native_setparameters ( Params.flatten ());    }

From the code in this section of code. What information can we get, setparameters method is to call the method of Jni method Native_setparameters, actually see here is not much, because again to look at the Jni method is very troublesome. After all, our daily development use is mostly Java code. We were able to find that the transmission came in parameters, called theThe flatten method of parameters.

We look for flatten code for viewing.

/**         * Creates a single string with all of the parameters set in         * This parameters object.         * <p>the {@link #unflatten (String)} method does the reverse.</p>         *         * @return A String with all values From this Parameters object, in         *         semi-colon delimited key-value pairs         *        /public String flatten () {            StringBuilder flattened = new StringBuilder ();            For (String K:mmap.keyset ()) {                flattened.append (k);                Flattened.append ("=");                Flattened.append (Mmap.get (k));                Flattened.append (";");            }            Chop off the extra semicolon at the end            Flattened.deletecharat (Flattened.length ()-1);            return flattened.tostring ();        }

What information can we get from this code? When we can see the data provided, the data is obtained from the mmap. OK, next, let's take a look at mmap there are several ways to assign a value to it.

/** * Takes A flattened string of parameters and adds each one to * this parameters object. * <p>the {@link #flatten ()} method does the reverse.</p> * * @param flattened a String of para meters (Key-value paired) that * is semi-colon delimited */public void Unflatten (String flattened)            {mmap.clear ();            StringTokenizer tokenizer = new StringTokenizer (flattened, ";");                while (Tokenizer.hasmoreelements ()) {String kv = Tokenizer.nexttoken ();                int pos = kv.indexof (' = ');                if (pos = =-1) {continue;                } String k = kv.substring (0, POS);                String v = kv.substring (pos + 1);            Mmap.put (k, v);         }}/** * sets a String parameter. * * @param key The key name for the parameter * @param value the String value of the parameter */public void Set (string key, String value) {if (key.indexof (' = ')! =-1 | | k                Ey.indexof (';')! =-1) {LOG.E (TAG, "key \" "+ key +" \ "contains invalid character (= or;)");            Return } if (value.indexof (' = ')! =-1 | | Value.indexof (';')! =-1) {LOG.E (TAG, "value \" "+ Value +" \                "contains invalid character (= or;)");            Return        } mmap.put (key, value);         }/** * Sets an integer parameter.        * * @param key The key name for the parameter * @param value the int value of the parameter */        public void Set (String key, int value) {mmap.put (Key, integer.tostring (value)); } private void Set (String key, list<area> areas) {if (areas = = null) {Set (Key, "(            0,0,0,0,0) "); } else {StringBuilder buffer = new StringbuildER ();                    for (int i = 0; i < areas.size (), i++) {Area area = Areas.get (i);                    Rect rect = area.rect;                    Buffer.append (' (');                    Buffer.append (Rect.left);                    Buffer.append (', ');                    Buffer.append (Rect.top);                    Buffer.append (', ');                    Buffer.append (Rect.right);                    Buffer.append (', ');                    Buffer.append (Rect.bottom);                    Buffer.append (', ');                    Buffer.append (Area.weight);                    Buffer.append (') ');                if (i! = Areas.size ()-1) buffer.append (', ');            } set (Key, buffer.tostring ());         }}/** * Returns The value of a String parameter.        * * @param key The key name for the parameter * @return the String value of the parameter * *           public string get (string key) { return Mmap.get (key); }/** * Sets the dimensions for preview pictures.  If the preview has already * started, applications should stop the preview first before changing * preview         Size. * * The sides of width and height is based on camera orientation. That's, the preview size is the size before it's rotated by display * orientation. So applications need to consider the display orientation * while setting preview size. For example, suppose the camera supports * both 480x320 and 320x480 preview sizes. The application wants a 3:2 * preview ratio. If The display orientation is set to 0 or in, preview * size should be set to 480x320. If The display orientation is set to * + or, preview size should be set to 320x480.         The display * Orientation should also is considered while setting picture size and * thumbnail size. * * @param width The widTh of the pictures, in pixels * @param height the height of the pictures, in pixels * @see #setDisplayOrie ntation (int) * @see #getCameraInfo (int, camerainfo) * @see #setPictureSize (int, int) * @see #setJp egthumbnailsize (int, int) */public void setpreviewsize (int width, int height) {String v = Inte            Ger.tostring (width) + "x" + integer.tostring (height);        Set (Key_preview_size, V); }

/** * <p>sets The dimensions for EXIF thumbnail in Jpeg picture.         IF * Applications set both width and height to 0, EXIF would not contain * thumbnail.</p> * * <p>applications need to consider the display orientation.  See {@link * #setPreviewSize (Int,int)} for reference.</p> * * @param width the width of the thumbnail, in pixels * @param height the height of the thumbnail, in pixels * @see #setPreviewSize (int,in T) */public void setjpegthumbnailsize (int width, int height) {Set (Key_jpeg_thumbnail_width, WI            DTH);        Set (Key_jpeg_thumbnail_height, HEIGHT);         }/** * Sets the quality of the EXIF thumbnail in Jpeg picture. * * @param quality The JPEG quality of the EXIF thumbnail.         The range is 1 * to + being the best. */public void setjpegthumbnailquality (int qualiTy) {Set (key_jpeg_thumbnail_quality, quality);         }/** * Sets Jpeg quality of captured picture. * * @param quality The JPEG quality of captured picture.         The range is 1 * to + being the best.        */public void setjpegquality (int quality) {Set (key_jpeg_quality, quality); }/** * Sets the rate at which preview frames is received. This is the * target frame rate.         The actual frame rate depends on the driver. * * @param fps the frame rate (frames per second) * @deprecated replaced by {@link #setPreviewFpsRange (int , int)} */@Deprecated public void setpreviewframerate (int fps) {set (Key_preview_frame_ra        TE, FPS); }/** * Sets the maximum and maximum preview fps. This controls the rate of * Preview frames received in {@link previewcallback}. The minimum and         * Maximum preview fps must is one of the elements from {@link * #getSupportedPreviewFpsRange}.         * * @param min The minimum preview fps (scaled by 1000).         * @param max the maximum preview fps (scaled by 1000).         * @throws runtimeexception If FPS range is invalid.        * @see #setPreviewCallbackWithBuffer (camera.previewcallback) * @see #getSupportedPreviewFpsRange () */        public void Setpreviewfpsrange (int min, int max) {set (Key_preview_fps_range, "" + min + "," + Max ");         }/** * Sets the image format for preview pictures.         * <p>if This is never called, the default format would be * {@link android.graphics.imageformat#nv21}, which * Uses the NV21 encoding format.</p> * * @param pixel_format the desired preview picture for         MAT, defined * by one of the {@link Android.graphics.ImageFormat} constants. * (e.g, &Lt;var>imageformat.nv21</var> (default), * <var>imageformat.rgb_565</var&gt         ;, or * <var>ImageFormat.JPEG</var>) * @see Android.graphics.ImageFormat */public void Setpreviewformat (int pixel_format) {String s = Cameraformatforpixelformat (pixel_            format); if (s = = null) {throw new IllegalArgumentException ("Invalid pixel_format=" + pixel            _format);        } set (Key_preview_format, s); }/** * <p>sets the dimensions for pictures.</p> * * <p>applic Ations need to consider the display orientation. See {@link * #setPreviewSize (Int,int)} for reference.</p> * * @param width the width for pi         Ctures, in pixels * @param height the height for pictures, in pixels * @see #setPreviewSize (int,int)    *     */public void setpicturesize (int width, int height) {String v = integer.tostring (width) + "x" + I            nteger.tostring (height);        Set (Key_picture_size, V);         }/** * Sets the image format for pictures. * * @param pixel_format the desired picture format * (<var>imageformat.nv21</ Var>, * &LT;VAR&GT;IMAGEFORMAT.RGB_565&LT;/VAR&GT;, or * < var>imageformat.jpeg</var>) * @see Android.graphics.ImageFormat */public void Setpicturef            ormat (int pixel_format) {String s = cameraformatforpixelformat (Pixel_format); if (s = = null) {throw new IllegalArgumentException ("Invalid pixel_format=" + pixel            _format);        } set (Key_picture_format, s); }


OK, the wrong place. We've been positioned in a couple of places. In the code you write, check to see if you have called these methods. Android source code or gaze is clearer, look at the method in English, see if there are errors in the number of references.

At that time I was using setpicturesize error, according to the method of explanation, I explained briefly. Why it went wrong. Since Parameters.setpicturesize (320, 480) (set resolution) has a wrong number of parameters, it is assumed that the resolution is not clear enough to drop the sentence. The execution will be OK again.

Note: Finally found a reason, feel very easy, in the actual development. Sometimes a little problem. Let a person busy an afternoon is also normal drop.




Android calls camera error setparameters failed deep parsing

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.