In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "Android how to customize the camera Camera to achieve manual focus", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "Android how to customize the camera Camera to achieve manual focus" this article.
Catalogue
The method of manual focusing is analyzed to realize that after the user clicks on the screen, the focus area and the metering area are set.
I. the method of manual focusing
Manual focus is mainly configured through two Camera methods:
SetFocusAreas sets the focusing area setMeteringAreas sets the photometric area
They need to pass in a Camera.Area collection, as shown in the figure Camera.Area:
/ * Create an area with specified rectangle and weight. * * @ param rect the bounds of the area. * @ param weight the weight of the area. * / public Area (Rect rect, int weight) {this.rect = rect; this.weight = weight;}
The first parameter is the area of focus and metering, ranging from [- 1000 Area] to [1000], and the second parameter is the weight, which ranges from 0 to 1000. When multiple Area are passed in, the weight determines the priority of focusing or metering. If only one area is focused at a time, the second parameter can be passed directly to 1000, as is the case in most developers.
When it comes to the scope of the first parameter, please take a look at the following figure, which will be clearer:
We can see that, different from the resolution of the mobile phone screen, the mapping area of Area to the screen is from-1000 momentum 1000 in the upper left corner to 1000 in the lower right corner, and the center point is 0Magin0. The coordinates obtained after we click on the screen will eventually need to be transformed into the coordinates of the mapping region, which is the most important part of manual focus. After understanding the configuration of these two necessary parameters, we can begin to achieve manual focus.
Second, after the user clicks on the screen, set the focus area and the metering area to obtain the coordinate values of the click preview screen
When the user clicks on the screen, it is actually the area of the preview screen that is clicked. Everyone must know the photo function, but this is not explained much, so we can monitor the View directly through the setOnTouchListener method.
SurfaceView.setOnTouchListener (new View.OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {Log.e ("MainActivity", "X coordinate:" + event.getX () + ", Y coordinate:" + event.getY ()); return false;}}))
Through MotionEvent, we can get the coordinate values relative to View when the user clicks on the screen.
Convert the View coordinate values to the coordinate values of the Area mapping area
As I said before, the Area mapping region is from [- 1000maxim 1000] to [1000Mae 1000], so through the following coordinate conversion formula, we can get the coordinate values of the mapped region when we click on the preview screen.
SurfaceView.setOnTouchListener (new View.OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {int areaX = (int) (event.getX () / surfaceView.getWidth () * 2000)-1000; / / obtain the X coordinate of the mapping region int areaY = (int) (event.getY () / surfaceView.getWidth () * 2000)-1000; / / obtain the Y coordinate return false;}} of the mapping region)
After we get the coordinates of the mapping area, we need to set a focus range, which is flexible. I'll create a rectangular area with a length and width of 200.
SurfaceView.setOnTouchListener (new View.OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {int areaX = (int) (event.getX () / surfaceView.getWidth () * 2000)-1000; / / get the X coordinate of the mapped region int areaY = (int) (event.getY () / surfaceView.getWidth () * 2000)-1000; / / obtain the Y coordinates of the mapped region / / create the Rect region Rect focusArea = new Rect () FocusArea.left = Math.max (x-100,-1000); / / take maximum or minimum values to avoid range overflow screen coordinates focusArea.top = Math.max (y-100,- 1000); focusArea.right = Math.min (x + 100,1000); focusArea.bottom = Math.min (y + 100,100,100); return false;}})
Set focus and photometry
After completing this step, manual focus has been achieved, as shown in the following code:
SurfaceView.setOnTouchListener (new View.OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {int areaX = (int) (event.getX () / surfaceView.getWidth () * 2000)-1000; / / get the X coordinate of the mapped region int areaY = (int) (event.getY () / surfaceView.getWidth () * 2000)-1000; / / obtain the Y coordinates of the mapped region / / create the Rect region Rect focusArea = new Rect () FocusArea.left = Math.max (x-100,-1000); / / take maximum or minimum values to avoid range overflow screen coordinates focusArea.top = Math.max (y-100,-1000); focusArea.right = Math.min (x + 100,100); focusArea.bottom = Math.min (y + 100,1000); / / create Camera.Area Camera.Area cameraArea = new Camera.Area (focusArea, 1000); List meteringAreas = new ArrayList (); List focusAreas = new ArrayList () If (mParameters.getMaxNumMeteringAreas () > 0) {meteringAreas.add (cameraArea); focusAreas.add (cameraArea);} mParameters.setFocusMode (Camera.Parameters.FOCUS_MODE_AUTO); / / set focus mode mParameters.setFocusAreas (focusAreas); / / set focus area mParameters.setMeteringAreas (meteringAreas); / / set light metering area try {mCamera.cancelAutoFocus (); / / defocus mCamera.setParameters (mParameters) before each focus / / set camera parameters mCamera.autoFocus (mAutoFocusCallback); / / enable focus} catch (Exception e) {} return false;}})
The relevant comments are in the code, manual focus is actually very simple, calculate the coordinates of the Area mapping area, and set the focus and metering area for the camera.
The above is all the contents of the article "how to customize the camera Camera to achieve manual focus by Android". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.