Inscription – sword tianya, start from your drip accumulation, and will keep improving.

github? The test source is here Baidu synchronization
CSDN Netease Cloud classroom tutorial The Denver nuggets
zhihu Series of articles on Flutter The headline synchronization

In the actual project development, several camera plug-ins officially recommended by Google always failed to meet the needs, and what is more uncomfortable is that using the system camera to take photos or selecting photos on some huawei phones of advanced version series will cause the application to blink back.

Xiaobian also analyzed the reason: The photos taken by these mobile phones were too large. At the moment when the mobile phone camera was finished, the mobile phone system had not finished processing the photos, and then the application read the photos, resulting in an abnormal crash of the system.

To the 0.0.1 version of this plug-in, the camera application plug-in has not repaired its compatibility, to small series of character, that is to recreate a wheel, so this plug-in was born.

Version 0.0.3 also supports iOS (2020-09-12)

Android iOS dual platform custom camera, photo album selection function iOS platform bottom popbox selection (Android is not supported yet)

Here is what the custom camera looks like by default:


1 Adding a Dependency

Flutter_custom_camera_pugin (flutter_custom_CamerA_pugin) (flutter_custom_CamerA_pugin) (Flutter_custom_CamerA_pugin)

Add dependencies through the pub repository as follows: See here for the latest version

  dependencies:
	 flutter_custom_camera_pugin: ^0.01.
Copy the code

To add a dependency, click github to view github. The code is as follows:

dependencies:
	shake_animation_widget:
	      git:
	        url: https://github.com/zhaolongs/FlutterCustomCameraPugin.git
	        ref: master
Copy the code

Then load the dependency as follows:

flutter pub get
Copy the code

Then guide the package where it is used, with the following code:

import 'package:flutter_custom_camera_pugin/flutter_custom_camera_pugin.dart';
Copy the code

2 Enable the custom camera to take photos

You can use the FlutterCustomCameraPugin openCamera method to open the custom camera page. The optional cameraConfigOptions parameter configures whether the buttons on the custom camera page are displayed, as shown in Listing 2-1:

  /// Listing 2-1
  /// Turn on the camera
  void openCamera(a) async {
    CameraConfigOptions options = new CameraConfigOptions();

    /// Whether to display the album switch by default
    options.isShowSelectCamera = true;

    /// Default custom album whether to display before and after camera switch
    options.isShowPhotoAlbum = true;

    /// Default custom album whether to display flash button
    options.isShowFlashButtonCamera = true;

    /// set up the custom camera
    // return the result of taking a photo
    CameraResultInfo resultInfo =
        await FlutterCustomCameraPugin.openCamera(cameraConfigOptions: options);

    if (resultInfo.code == 200) {
      imageFile = new File(resultInfo.data["lImageUrl"]);
    }else if (resultInfo.code == 201) {
     ///201 is to cancel the photo as clicked on the close button
     /// Or the back button on an Android phone
    }
    setState(() {});
  }

Copy the code

CameraConfigOptions is used to configure camera parameters, as shown below:

/// Set parameters for the album configuration
class CameraConfigOptions {

  / / / 0.0.1 version
  /// Whether to display the album switch by default
  bool isShowPhotoAlbum = true;

  /// Default custom album whether to display before and after camera switch
  bool isShowSelectCamera = true;

  /// Default custom album whether to display flash button
  bool isShowFlashButtonCamera = true;

}
Copy the code

CameraResultInfo is closed for photo or album selection, and will be called back when the photo is successfully taken, the photo is cancelled, the photo is successfully selected, and the photo is failed.

class CameraResultInfo {
  /// Message identifier
  int code;
  /// callback message
  String message =' ';
  /// Callback data
  dynamic data ;
  /// The method name of the callback
  String method =' ';
}
Copy the code

3 Open the album and select a photo

  /// Open the album
  void openPhotoAlbum(a) async {
    /// The album selection returns the result
    // select success and cancel both callback
    CameraResultInfo resultInfo =await FlutterCustomCameraPugin.openPhotoAlbum();
    if (resultInfo.code == 200) {
      imageFile = new File(resultInfo.data["lImageUrl"]); }}Copy the code

4 Open a dialog box selection

 void openSystemAlert(a) async {
   /// The album selection returns the result
   // select success and cancel both callback
   CameraResultInfo resultInfo =await FlutterCustomCameraPugin.openSystemAlert();
   
   if (resultInfo.code == 200) {
     imageFile = new File(resultInfo.data["lImageUrl"]);
   }

   setState(() {});
 }
Copy the code