Flutter图像处理插件momo_vn的使用
Flutter图像处理插件momo_vn的使用
1. Android set up
在AndroidManifest.xml
文件中请求Internet权限。
<uses-permission android:name="android.permission.INTERNET" />
2. iOS set up
在Info.plist
文件中更新如下:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string></string>
<key>CFBundleURLSchemes</key>
<array>
<string>partnerSchemeId</string>
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>momo</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
定义回调URL在AppDelegate.swift
文件中:
import momo_vn
override func application(_ application: UIApplication, openOpen url: URL, sourceApplication: String?, annotation: Any) -> Bool {
MoMoPayment.handleOpenUrl(url: url, sourceApp: sourceApplication!)
return true
}
override func application(_ app: UIApplication, openOpen url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
MoMoPayment.handleOpenUrl(url: url, sourceApp: "")
return true
}
更多设置 可以参考:https://developers.momo.vn/#/
Usage
Import
import 'package:momo_vn/momo_vn.dart';
Init
void initState() {
super.initState();
_momoPay = MomoVn();
_momoPay.on(MomoVn.EVENT_PAYMENT_SUCCESS, _handlePaymentSuccess);
_momoPay.on(MomoVn.EVENT_PAYMENT_ERROR, _handlePaymentError);
initPlatformState();
}
Create data Object
MomoPaymentInfo options = MomoPaymentInfo(
merchantname: "Tên đối tác",
merchantcode: 'Mã đối tác',
partnerCode: 'Mã đối tác',
appScheme: "1221222",
amount: 6000000000,
orderId: '12321312',
orderLabel: 'Label để hiển thị Mã giao dịch',
merchantnamelabel: "Tiêu đề tên cửa hàng",
fee: 0,
description: 'Mô tả chi tiết',
username: 'Định danh user (id/email/...)',
partner: 'merchant',
extra: "{\"key1\":\"value1\",\"key2\":\"value2\"}",
isTestMode: true
);
Open MoMo application
try {
_momoPay.open(options);
} catch (e) {
debugPrint(e);
}
Get result
void _setState() {
_payment_status = 'Đã chuyển thanh toán';
if (_momoPaymentResult.isSuccess) {
_payment_status += "\nTình trạng: Thành công.";
_payment_status += "\nSố điện thoại: " + _momoPaymentResult.phonenumber;
_payment_status += "\nExtra: " + _momoPaymentResult.extra;
_payment_status += "\nToken: " + _momoPaymentResult.token;
} else {
_payment_status += "\nTình trạng: Thất bại.";
_payment_status += "\nExtra: " + _momoPaymentResult.extra;
_payment_status += "\nMã lỗi: " + _momoPaymentResult.status.toString();
}
}
void _handlePaymentSuccess(PaymentResponse response) {
setState(() {
_momoPaymentResult = response;
_setState();
});
Fluttertoast.showToast(msg: "THành công: " + response.phonenumber, timeInSecForIos: 4);
}
void _handlePaymentError(PaymentResponse response) {
setState(() {
_momoPaymentResult = response;
_setState();
});
Fluttertoast.showToast(msg: "Thất bại: " + response.message.toString(), timeInSecForIos: 4);
}
示例代码
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:momo_vn/momo_vn.dart';
import 'package:fluttertoast/fluttertoast.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late MomoVn _momoPay;
late PaymentResponse _momoPaymentResult;
// ignore: non_constant_identifier_names
late String _paymentStatus;
[@override](/user/override)
void initState() {
super.initState();
_momoPay = MomoVn();
_momoPay.on(MomoVn.EVENT_PAYMENT_SUCCESS, _handlePaymentSuccess);
_momoPay.on(MomoVn.EVENT_PAYMENT_ERROR, _handlePaymentError);
_paymentStatus = "";
initPlatformState();
}
Future<void> initPlatformState() async {
if (!mounted) return;
setState(() {});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('THành toán qua ứng dụng Momovn'),
),
body: Center(
child: Column(
children: [
Column(
children: [
FlatButton(
color: Colors.blue,
textColor: Colors.white,
disabledColor: Colors.grey,
disabledTextColor: Colors.black,
padding: EdgeInsets.all(8.0),
splashColor: Colors.blueAccent,
child: Text('Demo payment with momovn'),
onPressed: () async {
MomoPaymentInfo options = MomoPaymentInfo(
merchantName: "TTN",
appScheme: "MOxx",
merchantCode: 'MOxx',
partnerCode: 'Mxx',
amount: 60000,
orderId: '12321312',
orderLabel: 'Gói combo',
merchantNameLabel: "HLGD",
fee: 10,
description: 'Thanh toán combo',
username: '01234567890',
partner: 'merchant',
extra: "{\"key1\":\"value1\",\"key2\":\"value2\"}",
isTestMode: true
);
try {
_momoPay.open(options);
} catch (e {
debugPrint(e.toString());
}
},
),
],
),
Text(_paymentStatus.isEmpty ? "Chưa thanh toán" : _paymentStatus)
],
),
),
),
);
}
void _setState() {
_paymentStatus = 'Đã chuyển thanh toán';
if (_momoPaymentResult.isSuccess == true) {
_paymentStatus += "\nTình trạng: Thành công.";
_paymentStatus += "\nSố điện thoại: " + _momoPaymentResult.phoneNumber.toString();
_paymentStatus += "\nExtra: " + _momoPaymentResult.extra!;
_paymentStatus += "\nToken: " + _momoPaymentResult.token.toString();
} else {
_paymentStatus += "\nTình trạng: Thất bại.";
_paymentStatus += "\nExtra: " + _momoPaymentResult.extra.toString();
_paymentStatus += "\nMã lỗi: " + _momoPaymentResult.status.toString();
}
}
void _handlePaymentSuccess(PaymentResponse response) {
setState(() {
_momoPaymentResult = response;
_setState();
});
Fluttertoast.showToast(msg: "thành công: " + response.phoneNumber.toString(), toastLength: Toast.LENGTH_SHORT);
}
void _handlePaymentError(PaymentResponse response) {
setState(() {
_momoPaymentResult = response;
_setState();
});
Fluttertoast.showToast(msg: "thất bại: " + response.message.toString(), toastLength: Toast.LENGTH_SHORT);
}
}
更多关于Flutter图像处理插件momo_vn的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter图像处理插件momo_vn的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用momo_vn
图像处理插件的示例代码。momo_vn
是一个强大的图像处理库,可以应用于各种图像编辑任务。假设你已经有一个Flutter项目,并且已经添加了momo_vn
依赖。
首先,确保在pubspec.yaml
文件中添加momo_vn
依赖:
dependencies:
flutter:
sdk: flutter
momo_vn: ^latest_version # 替换为实际最新版本号
然后运行flutter pub get
来安装依赖。
接下来,是一个完整的示例代码,展示如何使用momo_vn
进行基本的图像处理:
import 'package:flutter/material.dart';
import 'package:momo_vn/momo_vn.dart';
import 'package:image_picker/image_picker.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: ImageProcessingScreen(),
);
}
}
class ImageProcessingScreen extends StatefulWidget {
@override
_ImageProcessingScreenState createState() => _ImageProcessingScreenState();
}
class _ImageProcessingScreenState extends State<ImageProcessingScreen> {
final ImagePicker _picker = ImagePicker();
File? _imageFile;
Uint8List? _processedImage;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Image Processing with momo_vn'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_imageFile == null
? Text('No image selected.')
: Image.memory(_processedImage! ?? _imageFile!.readAsBytesSync()),
SizedBox(height: 20),
ElevatedButton(
onPressed: _pickImage,
child: Text('Pick Image'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _processImage,
child: Text('Process Image'),
isEnabled: _imageFile != null,
),
],
),
),
);
}
Future<void> _pickImage() async {
final pickedFile = await _picker.pickImage(source: ImageSource.camera);
setState(() {
if (pickedFile != null) {
_imageFile = File(pickedFile.path);
// Reset processed image
_processedImage = null;
}
});
}
Future<void> _processImage() async {
if (_imageFile == null) return;
// Initialize the editor
final editor = await MoMoEditor.init();
// Load the image
final sourceImage = await editor.image(fromFile: _imageFile!);
// Create a mutable image
final mutableImage = await sourceImage.mutableCopy();
// Apply some filters (for example, grayscale)
final grayImage = await mutableImage.filter(
name: 'grayscale',
params: <String, dynamic>{},
);
// Save the processed image to Uint8List
final processedImageBytes = await grayImage.data();
setState(() {
_processedImage = processedImageBytes;
});
}
}
在这个示例中,我们做了以下几件事:
- 使用
ImagePicker
从设备中选择或拍摄图像。 - 使用
momo_vn
插件初始化图像处理编辑器。 - 加载图像并创建一个可变的图像副本。
- 应用灰度滤镜作为示例处理。
- 将处理后的图像数据保存为
Uint8List
并显示在屏幕上。
请注意,momo_vn
支持多种图像处理功能,包括滤镜、变换、绘制等。你可以查阅momo_vn
的官方文档以了解更多高级用法和参数配置。