Flutter Camera Customization

Source: Internet
Author: User

The hardware-related parts of flutter have always been very painful. There are basically two kinds of scenarios, write it yourself, or wait out the relevant library.

In a recent project, the camera needs to be customized. Having experienced the development of the relevant modules, we know that this need is not simple, and it is the beginning of this cross-platform solution.

The demand is coming, how to do? Then you can only bite the bullet. Go to the pub first to find out, there is no library to use. Initially pick two libraries, one camera, and the other is image_picker.

Image_picker tried, basically pass, can only call the system camera or select album, Camera related parts, is certainly not able to use. The album part can be used.

The camera tries to run the next demo, feeling that the library can be used to directly encapsulate the preview as a flutter widget. We can easily make a variety of customization on the above.

The design diagram needs the camera full screen display, try to modify on the demo screen, tragedy appeared, the wind general stretching effect. Add a camera to adjust the page, after exiting the camera page, the demo background, switch to the front desk, Android side crash, tried n times, 100% of crash, I le a wipe, Google's official plug-in write so casual ~

And then, the point came, 本文主要是解决这两个问题,一个是全屏显示的问题,另一个则是crash问题 .

Take a photo on the Android side first:

Full-screen stretch problem on Android

For this kind of camera stretching problem, has done the camera customization related, will know is the preview resolution chooses the error to cause, knew this after, the modification is much simpler. Direct Pull Camer Plugin source code, Android implementation, relative or relatively simple, a file 700来 lines. Find a way to calculate the preview size.

    private void computeBestPreviewAndRecordingSize(        StreamConfigurationMap streamConfigurationMap, Size minPreviewSize, Size captureSize) {        ....    }

Considering the problem of future camera plug-in upgrade, create a new file directly for the best preview size calculation, and then replace it at the call.

Android front and back switch must present crash problem

Android front and back switch problem, view log found to be a resume after the crash, directly to the source code, found that the plug-in monitoring activity life cycle, in the resume when the open operation.

  @Override  public void onActivityResumed(Activity activity) {    if (requestingPermission) {      requestingPermission = false;      return;    }    if (activity == CameraPlugin.this.activity) {      if (camera != null) {        camera.open(null);      }    }  }

The point is, the key is that the plugin does not perform unregister operations, after exiting the camera page, calling the Dispose method, the camera is closed, and the cameradevice is set to null. All other life cycle callbacks call Cameradevice in the crash. Onactivityresumed call Camer.open will also crash. The root cause of these crash is that the callbacks are not unregister out. With this in view, it's easy to change the plugin's lifecycle back to unregister at Dispose. After the modification is finished, try the effect, and indeed there is no crash.

Something

Related code snippet I will not post out, about the full-screen preview of the size of the calculation, the Internet too much information, the previewsize calculation is correct. The second crash problem, add a unregisteractivitylifecyclecallbacks can. For the modification of the place, make a note, the subsequent upgrade plug-in when the good mess can be.

At present flutter third party is really poor, if only their own personal projects, or some biased in the data show project, you can try. However, for commercial projects, especially hardware dependence is relatively strong, it is recommended to look after a period of time.

Big something

Google's official camera plug-in, if only the use of their own, you can follow the above method to modify, if you want to use in commercial projects, or advised to think twice.

Google official Camera plugin Android part, with the Camera2 API, what is the problem of this API?

    1. Support starting from 21, that is to say, 10% of the share should be discarded;
    2. The above version is not the most pit, what is the most pit? Hardware manufacturers to CAMERA2 support degree, encountered several machines, CAMERA2 support very poor, these machines or nearly a year newer machine (Vivo x20a), get the maximum preview resolution is very low, the picture size is also not on.

Finally, rewrite the Android section, directly using the camera API, support version starting from 16, completely no problem, the hardware support is also very perfect. Google this hole buried a bit deep, the first consideration of the time factor, the result is spent on the plugin to modify the resolution, modify the crash, troubleshooting the special model of the time a lot of problems.

Reference
    1. Android device support for the new CAMERA2 API: Take Huawei M2 as an example

Flutter Camera Customization

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.