将 android 代码转换为 iOS,用于将位图图像输入从本机 iOS 插件发送到 phone-gap
Conversion of android code to iOS for sending bitmap image input to phone-gap from native iOS plugin
我尝试了很多方法将图像输入发送到 phone-gap 从本地 iOS plugin.But 最终遇到了一个问题..请任何人告诉我我在哪里实施错误?
这是我的 phonegap 插件方法代码:
myPlugin.convertToFile("profileBase64Callback", citizen.profileImage, function(){ }, function(){ });//citizen.profileImage is of type base64 data
使用从原生 iOS 到 phonegap
的响应图像
function profileBase64Callback(imageURI){
console.log("profileImage path :" + imageURI);
}
android版本原生插件文件:
if(action.equals("convertToFile")){
try{
JSONObject json = args.getJSONObject(0);
callbackFunction = (String) json.get("callback");
String base64 = json.getString("base64");
Log.d(TAG, "base64 content"+base64);
byte[] decodedString = Base64.decode(base64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
String filePath = convertBitmapToFile(decodedByte);
//callbackContext.success(requestId);
sendData(filePath);
}catch(Exception e){
Log.d(TAG + ": error in start: ", e.getMessage(), e);
}
}
//convertBitmapToFile 在android
中的实现
private String convertBitmapToFile(Bitmap photo)
{
File mypath = null;
try {
File dir;
File storage = null;
// = Environment.getExternalStorageDirectory();
if (Environment.getExternalStorageState() == null) {
storage = Environment.getDataDirectory();
}else if(Environment.getExternalStorageState() != null){
storage = Environment.getExternalStorageDirectory();
}
dir = new File(storage.getAbsolutePath() + “/AppName/profilePicture");
if (!dir.exists())
{
dir.mkdirs();
File file = new File(dir, ".nomedia");
if(!file.exists())
file.createNewFile();
}
String uniqueId = "AppName"+Utility.getCurrentDate()+"_"+Utility.getCurrentTime()+"_"+Math.random();
String current = uniqueId + ".jpg";
mypath = new File(dir, current);
FileOutputStream mFileOutStream = new FileOutputStream(mypath);
photo.compress(Bitmap.CompressFormat.JPEG, 10, mFileOutStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(CompressFormat.JPEG, 0, stream);
mFileOutStream.flush();
mFileOutStream.close();
return mypath.getAbsolutePath();
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
我正在通过这种方法获取图像,但是当使用控制台以 javascript 方法打印时,profilepicture 处于未定义状态。任何人都可以告诉我上面从 android [=40 的转换=] 或者我应该使用哪种方法将图像转换为解码数组,bitmapimage
这解决了我的问题:
Base64File.m
- (void)convertToFile:(CDVInvokedUrlCommand *)command{
NSLog(@"--convertToFile Plugin");
NSString *methodname1;
NSDictionary* options = [[NSDictionary alloc]init];
if ([command.arguments count] > 0) {
options = [command argumentAtIndex:0];
methodname1 =[options objectForKey:@"callback"];
}
//Now data is decoded. You can convert them to UIImage
NSData* data = [[NSData alloc] initWithBase64EncodedString:methodname1 options:0];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/Test.png"]; // identity the home directory and file name
[data writeToFile:jpgPath atomically:YES];
NSString *appndStr = [@"file://" stringByAppendingString:jpgPath];
NSLog(@"----appndStr--%@",appndStr);
NSDictionary* jsonDict = @{
@"imagURI": appndStr,
};
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:nil];
//Generate the JSON String
NSString* jsonString1 = [[NSString alloc] initWithBytes:[jsonData bytes]
length:[jsonData length]
encoding:NSUTF8StringEncoding];
NSString *echo = jsonString1;
dispatch_async(dispatch_get_main_queue(), ^{
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"OK"];
[pluginResult setKeepCallbackAsBool:true];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
//Send data to Javascript callback Method
NSString * jsCallBack = [NSString stringWithFormat:@"%@(%@);", @"profileBase64Callback", echo];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
});
}
Profile.js
function profileBase64Callback(imageURI){
console.log("profileImage path :" + imageURI.imagURI);
}
控制台输出:
profile图片路径:file:///Users/samDesk/Library/Developer/CoreSimulator/Devices/C8427646-AB64-4063-A7BE-57547DDA656E/data/Applications/DDD4B4DR-2803-4615-9EC9-5840EAAD6709/tmp/Test.png
我尝试了很多方法将图像输入发送到 phone-gap 从本地 iOS plugin.But 最终遇到了一个问题..请任何人告诉我我在哪里实施错误?
这是我的 phonegap 插件方法代码:
myPlugin.convertToFile("profileBase64Callback", citizen.profileImage, function(){ }, function(){ });//citizen.profileImage is of type base64 data
使用从原生 iOS 到 phonegap
的响应图像 function profileBase64Callback(imageURI){
console.log("profileImage path :" + imageURI);
}
android版本原生插件文件:
if(action.equals("convertToFile")){
try{
JSONObject json = args.getJSONObject(0);
callbackFunction = (String) json.get("callback");
String base64 = json.getString("base64");
Log.d(TAG, "base64 content"+base64);
byte[] decodedString = Base64.decode(base64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
String filePath = convertBitmapToFile(decodedByte);
//callbackContext.success(requestId);
sendData(filePath);
}catch(Exception e){
Log.d(TAG + ": error in start: ", e.getMessage(), e);
}
}
//convertBitmapToFile 在android
中的实现 private String convertBitmapToFile(Bitmap photo)
{
File mypath = null;
try {
File dir;
File storage = null;
// = Environment.getExternalStorageDirectory();
if (Environment.getExternalStorageState() == null) {
storage = Environment.getDataDirectory();
}else if(Environment.getExternalStorageState() != null){
storage = Environment.getExternalStorageDirectory();
}
dir = new File(storage.getAbsolutePath() + “/AppName/profilePicture");
if (!dir.exists())
{
dir.mkdirs();
File file = new File(dir, ".nomedia");
if(!file.exists())
file.createNewFile();
}
String uniqueId = "AppName"+Utility.getCurrentDate()+"_"+Utility.getCurrentTime()+"_"+Math.random();
String current = uniqueId + ".jpg";
mypath = new File(dir, current);
FileOutputStream mFileOutStream = new FileOutputStream(mypath);
photo.compress(Bitmap.CompressFormat.JPEG, 10, mFileOutStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(CompressFormat.JPEG, 0, stream);
mFileOutStream.flush();
mFileOutStream.close();
return mypath.getAbsolutePath();
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
我正在通过这种方法获取图像,但是当使用控制台以 javascript 方法打印时,profilepicture 处于未定义状态。任何人都可以告诉我上面从 android [=40 的转换=] 或者我应该使用哪种方法将图像转换为解码数组,bitmapimage
这解决了我的问题:
Base64File.m
- (void)convertToFile:(CDVInvokedUrlCommand *)command{
NSLog(@"--convertToFile Plugin");
NSString *methodname1;
NSDictionary* options = [[NSDictionary alloc]init];
if ([command.arguments count] > 0) {
options = [command argumentAtIndex:0];
methodname1 =[options objectForKey:@"callback"];
}
//Now data is decoded. You can convert them to UIImage
NSData* data = [[NSData alloc] initWithBase64EncodedString:methodname1 options:0];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/Test.png"]; // identity the home directory and file name
[data writeToFile:jpgPath atomically:YES];
NSString *appndStr = [@"file://" stringByAppendingString:jpgPath];
NSLog(@"----appndStr--%@",appndStr);
NSDictionary* jsonDict = @{
@"imagURI": appndStr,
};
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:nil];
//Generate the JSON String
NSString* jsonString1 = [[NSString alloc] initWithBytes:[jsonData bytes]
length:[jsonData length]
encoding:NSUTF8StringEncoding];
NSString *echo = jsonString1;
dispatch_async(dispatch_get_main_queue(), ^{
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"OK"];
[pluginResult setKeepCallbackAsBool:true];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
//Send data to Javascript callback Method
NSString * jsCallBack = [NSString stringWithFormat:@"%@(%@);", @"profileBase64Callback", echo];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
});
}
Profile.js
function profileBase64Callback(imageURI){
console.log("profileImage path :" + imageURI.imagURI);
}
控制台输出:
profile图片路径:file:///Users/samDesk/Library/Developer/CoreSimulator/Devices/C8427646-AB64-4063-A7BE-57547DDA656E/data/Applications/DDD4B4DR-2803-4615-9EC9-5840EAAD6709/tmp/Test.png