预览
代码
手机端效果
demoAndroidCodeCompiler.cs
demoAndroidCodeCompiler.Designer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Smobiler.Core;
using Smobiler.Core.Controls;
using Smobiler.Plugins;
namespace Smobiler.Tutorials.Plugins
{
partial class demoAndroidCodeCompiler : Smobiler.Core.Controls.MobileForm
{
public demoAndroidCodeCompiler() : base()
{
InitializeComponent();
}
private void button1_Press(object sender, EventArgs e)
{
//安卓震动功能,可先网上搜索头文件与代码
AndroidCodeProvider provider = new AndroidCodeProvider();
provider.Imports.Add("android.os.Vibrator");
provider.MethodBody = @"
Vibrator vib = (Vibrator) activity.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(200);
resultValue=""success"";";
//Run方法中provider参数填写代码,value参数对应provider参数中用户自行添加的变量value
this.androidCodeCompiler1.Run(provider, (obj, args) =>
{
if (args.isError == true)
MessageBox.Show(args.error);
else
Toast("Sucess");
});
}
private void title1_ImagePress(object sender, EventArgs e)
{
this.Close();
}
private void button2_Press(object sender, EventArgs e)
{
AndroidCodeProvider provider = new AndroidCodeProvider();
provider.Imports.Add("android.content.Intent;");
provider.Imports.Add("android.content.ComponentName;");
provider.MethodBody = @"
Intent intent = new Intent(Intent.ACTION_MAIN);
ComponentName cmp = new ComponentName(""com.tencent.mm"",""com.tencent.mm.ui.LauncherUI"");
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(cmp);
activity.startActivity(intent); ";
this.androidCodeCompiler1.Run(provider, (obj, args) =>
{
if (args.isError == true)
MessageBox.Show(args.error);
else
Toast("success");
});
}
private void btngetOcrRegister_Press(object sender, EventArgs e)
{
AndroidCodeProvider provider = new AndroidCodeProvider();
provider.Imports.Add("com.baidu.ocr.sdk.OCR");
provider.Imports.Add("com.baidu.ocr.sdk.OnResultListener");
provider.Imports.Add("com.baidu.ocr.sdk.exception.OCRError");
provider.Imports.Add("com.baidu.ocr.sdk.model.AccessToken");
provider.Imports.Add("com.baidu.ocr.sdk.model.GeneralBasicParams");
provider.Imports.Add("com.baidu.ocr.sdk.model.GeneralParams");
provider.Imports.Add("com.baidu.ocr.sdk.model.GeneralResult");
provider.Imports.Add("com.baidu.ocr.sdk.model.OcrRequestParams");
provider.Imports.Add("com.baidu.ocr.sdk.model.OcrResponseResult");
provider.Imports.Add("com.baidu.ocr.sdk.model.IDCardParams");
provider.Imports.Add("com.baidu.ocr.sdk.model.IDCardResult");
provider.Imports.Add("java.util.Timer");
provider.Imports.Add("java.util.TimerTask");
provider.Imports.Add("java.io.File");
provider.Imports.Add("com.baidu.ocr.ui.camera.CameraActivity");
provider.MethodBody = @"OCR.getInstance(context).initAccessTokenWithAkSk(new OnResultListener() {
@Override
public void onResult(AccessToken result) {
String token = result.getAccessToken();
callback(""{success: true,tocken:"" + token +""}"");
}
@Override
public void onError(OCRError error)
{
callback(""{success:false,errcode:"" + error.getErrorCode() + ""}"");
}
}, activity.getApplicationContext(), ""百度OCR API Key"", ""百度OCR Secret Key"");";
//上面代码中百度OCR API Key与Secret Key需要https://cloud.baidu.com注册申请
this.androidCodeCompiler1.Run(provider, (obj, args) =>
{
if (args.isError == true)
MessageBox.Show(args.error);
else
Toast("Sucess");
});
}
private void btngetOcrParams_Press(object sender, EventArgs e)
{
AndroidCodeProvider provider = new AndroidCodeProvider();
provider.Imports.Add("com.baidu.ocr.sdk.OCR");
provider.Imports.Add("com.baidu.ocr.sdk.OnResultListener");
provider.Imports.Add("com.baidu.ocr.sdk.exception.OCRError");
provider.Imports.Add("com.baidu.ocr.sdk.model.AccessToken");
provider.Imports.Add("com.baidu.ocr.sdk.model.GeneralBasicParams");
provider.Imports.Add("com.baidu.ocr.sdk.model.GeneralParams");
provider.Imports.Add("com.baidu.ocr.sdk.model.GeneralResult");
provider.Imports.Add("com.baidu.ocr.sdk.model.OcrRequestParams");
provider.Imports.Add("com.baidu.ocr.sdk.model.OcrResponseResult");
provider.Imports.Add("com.baidu.ocr.sdk.model.IDCardParams");
provider.Imports.Add("com.baidu.ocr.sdk.model.IDCardResult");
provider.Imports.Add("java.io.File");
provider.Imports.Add("com.baidu.ocr.ui.camera.CameraActivity");
provider.MethodBody = @"Intent intent = new Intent(activity, CameraActivity.class);
intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,new File(activity.getFilesDir(),""pic.jpg"").getAbsolutePath());intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_GENERAL);activity.startActivityForResult(intent, 456);";
provider.MethodResult = @"String contentType = data.getStringExtra(CameraActivity.KEY_CONTENT_TYPE);
File filePath = new File(activity.getFilesDir(),""pic.jpg"");
if (requestCode == 456)
{
if (resultCode == Activity.RESULT_OK && filePath.exists() == true)
{
// 通用文字识别参数设置
GeneralBasicParams param = new GeneralBasicParams();
param.setDetectDirection(true);
param.setImageFile(new File(activity.getFilesDir(),""pic.jpg""));
// 调用通用文字识别服务
OCR.getInstance(activity.getApplication().getApplicationContext()).recognizeGeneralBasic(param, new OnResultListener() {
@Override
public void onResult(GeneralResult result) {
// 调用成功,返回GeneralResult对象
callback(""{success: true,result:"" + result.getJsonRes() + ""}"");
}
@Override
public void onError(OCRError error)
{
// 调用失败,返回OCRError对象
callback(""{success:false,errcode:"" + error.getErrorCode() + ""}"");
}
});
}
else
{
callback(""{\""success\"":false,\""error\"":\""未获取到返回内容\""}"");
}
}
";
this.androidCodeCompiler1.Run(provider, (obj, args) =>
{
if (args.isError == true)
MessageBox.Show(args.error);
else
Toast("Sucess");
});
}
private void btnIDCardParams_Press(object sender, EventArgs e)
{
AndroidCodeProvider provider = new AndroidCodeProvider();
provider.Imports.Add("com.baidu.ocr.sdk.OCR");
provider.Imports.Add("com.baidu.ocr.sdk.OnResultListener");
provider.Imports.Add("com.baidu.ocr.sdk.exception.OCRError");
provider.Imports.Add("com.baidu.ocr.sdk.model.AccessToken");
provider.Imports.Add("com.baidu.ocr.sdk.model.GeneralBasicParams");
provider.Imports.Add("com.baidu.ocr.sdk.model.GeneralParams");
provider.Imports.Add("com.baidu.ocr.sdk.model.GeneralResult");
provider.Imports.Add("com.baidu.ocr.sdk.model.OcrRequestParams");
provider.Imports.Add("com.baidu.ocr.sdk.model.OcrResponseResult");
provider.Imports.Add("com.baidu.ocr.sdk.model.IDCardParams");
provider.Imports.Add("com.baidu.ocr.sdk.model.IDCardResult");
provider.Imports.Add("java.io.File");
provider.Imports.Add("com.baidu.ocr.ui.camera.CameraActivity");
provider.MethodBody = @"Intent intent = new Intent(activity, CameraActivity.class);
intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,new File(activity.getFilesDir(),""pic.jpg"").getAbsolutePath());intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_GENERAL);activity.startActivityForResult(intent, 456);";
provider.MethodResult = @"String contentType = data.getStringExtra(CameraActivity.KEY_CONTENT_TYPE);
File filePath = new File(activity.getFilesDir(),""pic.jpg"");
if (requestCode == 456)
{
if (resultCode == Activity.RESULT_OK && filePath.exists() == true)
{
// 身份证识别参数设置
IDCardParams param = new IDCardParams();
param.setImageFile(new File(activity.getFilesDir(),""pic.jpg""));
// 调用身份证识别服务
OCR.getInstance(activity.getApplication().getApplicationContext()).recognizeIDCard(param, new OnResultListener() {
@Override
public void onResult(IDCardResult result) {
// 调用成功,返回IDCardResult对象
callback(""{ success: true,result: "" + result.getJsonRes() + ""}"");
}
@Override
public void onError(OCRError error)
{
// 调用失败,返回OCRError对象
callback(""{success:false,errcode:"" + error.getErrorCode() + ""}"");
}
});
}
else
{
callback(""{\""success\"":false,\""error\"":\""未获取到返回内容\""}"");
}
}
";
this.androidCodeCompiler1.Run(provider, (obj, args) =>
{
if (args.isError == true)
MessageBox.Show(args.error);
else
Toast("Sucess");
});
}
private void androidCodeCompiler1_AndroidCodeCompilerResult(object sender, AndroidCodeCompilerEventArgs e)
{
Toast(e.Data);
}
}
}
using Smobiler.Core;
namespace Smobiler.Tutorials.Plugins
{
partial class demoAndroidCodeCompiler : Smobiler.Core.Controls.MobileForm
{
#region "SmobilerForm Designer generated code "
//SmobilerForm overrides dispose to clean up the component list.
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
//NOTE: The following procedure is required by the SmobilerForm Designer
//It can be modified using the SmobilerForm Designer.
//Do not modify it using the code editor.
[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
this.label1 = new Smobiler.Core.Controls.Label();
this.button1 = new Smobiler.Core.Controls.Button();
this.androidCodeCompiler1 = new Smobiler.Plugins.AndroidCodeCompiler();
this.label2 = new Smobiler.Core.Controls.Label();
this.title1 = new Smobiler.Core.Controls.Title();
this.button2 = new Smobiler.Core.Controls.Button();
this.label3 = new Smobiler.Core.Controls.Label();
this.btngetOcrRegister = new Smobiler.Core.Controls.Button();
this.btngetOcrParams = new Smobiler.Core.Controls.Button();
this.btnIDCardParams = new Smobiler.Core.Controls.Button();
//
// label1
//
this.label1.FontSize = 15F;
this.label1.Location = new System.Drawing.Point(18, 96);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(268, 203);
this.label1.Text = "安卓震动功能,可先网上搜索头文件与代码.\r\n添加androidCodeCompiler组件\r\n可使用方法Compile(预编译)\r\nRun(编译并执行)\r\n\r\n";
//
// button1
//
this.button1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(67)))), ((int)(((byte)(81)))));
this.button1.Location = new System.Drawing.Point(18, 310);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(100, 30);
this.button1.Text = "震动";
this.button1.Press += new System.EventHandler(this.button1_Press);
//
// androidCodeCompiler1
//
this.androidCodeCompiler1.Name = "androidCodeCompiler1";
this.androidCodeCompiler1.AndroidCodeCompilerResult += new Smobiler.Plugins.AndroidCodeCompilerEventHandler(this.androidCodeCompiler1_AndroidCodeCompilerResult);
//
// label2
//
this.label2.FontSize = 15F;
this.label2.Location = new System.Drawing.Point(18, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(268, 35);
this.label2.Text = "此插件仅支持android";
//
// title1
//
this.title1.ImageType = Smobiler.Core.Controls.ImageEx.ImageStyle.FontIcon;
this.title1.Name = "title1";
this.title1.ResourceID = "angle-left";
this.title1.Size = new System.Drawing.Size(300, 30);
this.title1.Text = "AndroidCodeCompiler";
this.title1.ImagePress += new System.EventHandler(this.title1_ImagePress);
//
// button2
//
this.button2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(185)))), ((int)(((byte)(229)))), ((int)(((byte)(100)))));
this.button2.Location = new System.Drawing.Point(186, 310);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(100, 30);
this.button2.Text = "打开微信";
this.button2.Press += new System.EventHandler(this.button2_Press);
//
// label3
//
this.label3.FontSize = 15F;
this.label3.Location = new System.Drawing.Point(18, 346);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(268, 43);
this.label3.Text = "百度OCR:需要注册百度OCR https://cloud.baidu.com/";
//
// btngetOcrRegister
//
this.btngetOcrRegister.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(76)))), ((int)(((byte)(176)))), ((int)(((byte)(250)))));
this.btngetOcrRegister.Location = new System.Drawing.Point(18, 396);
this.btngetOcrRegister.Name = "btngetOcrRegister";
this.btngetOcrRegister.Size = new System.Drawing.Size(100, 30);
this.btngetOcrRegister.Text = "注册";
this.btngetOcrRegister.Press += new System.EventHandler(this.btngetOcrRegister_Press);
//
// btngetOcrParams
//
this.btngetOcrParams.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(254)))), ((int)(((byte)(192)))), ((int)(((byte)(80)))));
this.btngetOcrParams.Location = new System.Drawing.Point(186, 396);
this.btngetOcrParams.Name = "btngetOcrParams";
this.btngetOcrParams.Size = new System.Drawing.Size(100, 30);
this.btngetOcrParams.Text = "文字识别";
this.btngetOcrParams.Press += new System.EventHandler(this.btngetOcrParams_Press);
//
// btnIDCardParams
//
this.btnIDCardParams.Location = new System.Drawing.Point(102, 443);
this.btnIDCardParams.Name = "btnIDCardParams";
this.btnIDCardParams.Size = new System.Drawing.Size(100, 30);
this.btnIDCardParams.Text = "身份证识别";
this.btnIDCardParams.Press += new System.EventHandler(this.btnIDCardParams_Press);
//
// demoAndroidCodeCompiler
//
this.Components.AddRange(new Smobiler.Core.Controls.MobileComponent[] {
this.androidCodeCompiler1});
this.Controls.AddRange(new Smobiler.Core.Controls.MobileControl[] {
this.label1,
this.button1,
this.label2,
this.title1,
this.button2,
this.label3,
this.btngetOcrRegister,
this.btngetOcrParams,
this.btnIDCardParams});
this.Name = "demoAndroidCodeCompiler";
}
#endregion
private Smobiler.Core.Controls.Label label1;
private Smobiler.Core.Controls.Button button1;
private Smobiler.Plugins.AndroidCodeCompiler androidCodeCompiler1;
private Smobiler.Core.Controls.Label label2;
private Core.Controls.Title title1;
private Core.Controls.Button button2;
private Core.Controls.Label label3;
private Core.Controls.Button btngetOcrRegister;
private Core.Controls.Button btngetOcrParams;
private Core.Controls.Button btnIDCardParams;
}
}