Android calls camera error setParameters failed deep Parsing

Source: Internet
Author: User
Tags deprecated

Android calls camera error setParameters failed deep Parsing
1. Camera

Camera is supported in the Android framework and allows you to take photos and videos. Therefore, you will always encounter some problems when using 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)
When this error occurs, we can see that the setParameters method of android has an error.

2. What should we do?

We know that there are also many parameter settings in the parameters of camera. Which of the following is an error? Many people do not know how to search for the Internet, and start various guesses if they cannot find it. In fact, the most effective way is to find the cause at the underlying layer. OK. Let's open the android code and find the camera class. 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 invalid or not supported.     * @see #getParameters()     */    public void setParameters(Parameters params) {        native_setParameters(params.flatten());    }

In this Code, what information can we get? The setParameters method calls the native_setParameters method of the jni method. In fact, there is not much difference here, it is very troublesome to check the jni method again. After all, most of our daily development uses java code. We can find that the Parameters parameter is transmitted and the flatten method of Parameters is called. Let's look for the flatten code.

 /**         * Creates a single string with all the parameters set in         * this Parameters object.         * 

The {@link #unflatten(String)} method does the reverse.

* * @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. We can see that the data is obtained from mMap when the data is provided. OK. Next, let's take a look at how many mMap Methods assign values to mMap.

/**         * Takes a flattened string of parameters and adds each one to         * this Parameters object.         * 

The {@link #flatten()} method does the reverse.

* * @param flattened a String of parameters (key-value paired) that * are 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 || key.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 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 are based on camera orientation. That * is, the preview size is the size before it is 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 180, preview * size should be set to 480x320. If the display orientation is set to * 90 or 270, preview size should be set to 320x480. The display * orientation should also be 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 #setDisplayOrientation(int) * @see #getCameraInfo(int, CameraInfo) * @see #setPictureSize(int, int) * @see #setJpegThumbnailSize(int, int) */ public void setPreviewSize(int width, int height) { String v = Integer.toString(width) + "x" + Integer.toString(height); set(KEY_PREVIEW_SIZE, v); }

/**         * 

Sets the dimensions for EXIF thumbnail in Jpeg picture. If * applications set both width and height to 0, EXIF will not contain * thumbnail.

* *

Applications need to consider the display orientation. See {@link * #setPreviewSize(int,int)} for reference.

* * @param width the width of the thumbnail, in pixels * @param height the height of the thumbnail, in pixels * @see #setPreviewSize(int,int) */ public void setJpegThumbnailSize(int width, int height) { set(KEY_JPEG_THUMBNAIL_WIDTH, width); 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 100, with 100 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 100, with 100 being the best. */ public void setJpegQuality(int quality) { set(KEY_JPEG_QUALITY, quality); } /** * Sets the rate at which preview frames are 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_RATE, 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 be 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. *

If this is never called, the default format will be * {@link android.graphics.ImageFormat#NV21}, which * uses the NV21 encoding format.

* * @param pixel_format the desired preview picture format, defined * by one of the {@link android.graphics.ImageFormat} constants. * (E.g., ImageFormat.NV21 (default), * ImageFormat.RGB_565, or * ImageFormat.JPEG) * @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); } /** *

Sets the dimensions for pictures.

* *

Applications need to consider the display orientation. See {@link * #setPreviewSize(int,int)} for reference.

* * @param width the width for pictures, 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" + Integer.toString(height); set(KEY_PICTURE_SIZE, v); } /** * Sets the image format for pictures. * * @param pixel_format the desired picture format * (ImageFormat.NV21, * ImageFormat.RGB_565, or * ImageFormat.JPEG) * @see android.graphics.ImageFormat */ public void setPictureFormat(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 error is located in the following places. In your own code, check whether these methods have been called ~~~. The android source code is still clearly annotated. Let's look at the English description of the method to see if the parameter has encountered an error.

At that time, I encountered an error when using setPictureSize. According to the method instructions, I briefly explained why the error occurred. Because the parameter parameters. setPictureSize (320,480) (sets resolution) is incorrect, if you do not know the resolution, you can drop this sentence and then run it.

Note: I finally found the cause, which is very simple. In actual development, sometimes a small problem makes people busy one afternoon.




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.