phonegap(cordova) 自己定义插件代码篇(四)----读取本地图片
有时候确实知道本地图片地址,要获取到base64
/**
* 获取本地图片,包括路径和压缩后的 base64
*/ (function (cordova) {
var define = cordova.define; define("cordova/plugin/localImage", function (require, exports, module) {
var argscheck = require('cordova/argscheck'),
exec = require('cordova/exec');
exports.getImage = function (localFileUrl, successCB, failCB) { exec(successCB, failCB, "LocalImage", "getImage", [localFileUrl]); }; });
cordova.addConstructor(function () {
if (!window.plugins) {
window.plugins = {};
}
console.log("将插件注入localImage...");
window.plugins.localImage = cordova.require("cordova/plugin/localImage");
console.log("localImage注入结果:" + typeof (window.plugins.localImage)); });
})(cordova);
android
public class LocalImagePlugin extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {
if ("getImage".equals(action)) {
String localFileUrl = args.getString(0).replace("file:///", "/");
// Log.i("our", localFileUrl);
String file = "{\"path\":\"" + localFileUrl + "\",\"data\":\""
+ bitmapToString(localFileUrl) + "\"}";
// Log.i("our", file);
callbackContext.success(file);
return true;
} else {
return false;
}
}
public static String bitmapToString(String filePath) {
Bitmap bm = getSmallBitmap(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 50, baos);
byte[] b = baos.toByteArray();
return Base64.encode(b);
}
// 依据路径获得图片并压缩。返回bitmap用于显示
public static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 480, 800);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
// 计算图片的缩放值
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
}
iOS
#import <UIKit/UIKit.h>
#import <Cordova/CDV.h> @interface CDVLocalImage: CDVPlugin @property (nonatomic,copy) NSString*callbackID;
//Instance Method
-(void) getImage:(CDVInvokedUrlCommand*)command ; @end
#import "CDVLocalImage.h"
#import "TencentOpenAPI/QQApiInterface.h"
#import <TencentOpenAPI/TencentOAuth.h>
@implementation CDVLocalImage
@synthesize callbackID;
-(void)getImage:(CDVInvokedUrlCommand *)command {
//url
NSString* localImageUrl = [command.arguments objectAtIndex:0]; localImageUrl =[localImageUrl stringByReplacingOccurrencesOfString:@"file://" withString:@""]; NSData *mydata=UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:localImageUrl], 0.5);
NSString *pictureDataString=[mydata base64Encoding]; NSString *file =[NSString stringWithFormat:@"{\"path\":\"%@\",\"data\":\"%@\"}", localImageUrl,pictureDataString]; // NSLog(@"selected >>>>%@",file); CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:file];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@end
phonegap(cordova) 自己定义插件代码篇(四)----读取本地图片的更多相关文章
- phonegap(cordova) 自己定义插件代码篇(六)----android ,iOS 微信支付工具整合
还是那句话,在使用插件代码篇的时候,请先了解插件机制(如整合原生插件先阅读原生插件文档.非常重要.非常重要!非常重要!),如未了解,请先阅读入门篇.这里就专贴关键代码 必须先把官方sdk 依照要求一步 ...
- phonegap(cordova) 自己定义插件代码篇(三)----支付宝支付工具整合
建议读者,先阅读官方文档,知晓其支付流程之后再来使用此代码,比方客户须要做什么,服务端须要做什么(非常重要!非常重要! 非常重要!),由于这几个篇幅都是纯代码篇,由于阅读前面的入门篇之后看这些应该毫无 ...
- phonegap(cordova) 自己定义插件代码篇(五)----android ,iOS 集成微信登陆
统一登陆还是非常有必要的,安全,放心.代码 /*cordov 微信自己定义插件*/ (function (cordova) { var define = cordova.define; define( ...
- OpenCV基础篇之读取显示图片
程序及分析 /* * FileName : read.cpp * Author : xiahouzuoxin @163.com * Version : v1.0 * Date : Tue 13 May ...
- tinymce4.x 上传本地图片(自己写个插件)
tinymce是一款挺不错的html文本编辑器.但是添加图片是直接添加链接,不能直接选择本地图片. 下面我写了一个插件用于直接上传本地图片. 在tinymce的plugins目录下新建一个upload ...
- Android移动APP开发笔记——Cordova(PhoneGap)通过CordovaPlugin插件调用 Activity 实例
引言 Cordova(PhoneGap)采用的是HTML5+JavaScript混合模式来开发移动手机APP,因此当页面需要获取手机内部某些信息时(例如:联系人信息,坐标定位,短信等),程序就需要调用 ...
- Cordova应用的JavaScript代码和自定义插件代码的调试
我之前写过三篇Cordova相关的技术文章.当我们使用Cordova将自己开发的前端应用打包安装到手机上后,可能会遇到需要调试Cordova应用的时候. 本文就介绍Cordova应用的调试步骤. 如果 ...
- Android Cordova 插件开发之编写自己定义插件
前言 本文适合Android+web的复合型人才,由于cordova本身就是混合开发,所以在Android开发的基础上,还要懂web相关技术(HTML+CSS+JS).可是也有例外,比方我.仅仅需负责 ...
- Cordova - 与iOS原生代码交互2(使用Swift开发Cordova的自定义插件)
在前一篇文章中我介绍了如何通过 js 与原生代码进行交互(Cordova - 与iOS原生代码交互1(通过JS调用Swift方法)),当时是直接对Cordova生成的iOS工程项目进行编辑操作的(添加 ...
随机推荐
- El表达式日期处理
第1步:引入指令 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt " %&g ...
- POJ 1111(数字很吉利嘛) 简单BFS
Image Perimeters Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8594 Accepted: 5145 Desc ...
- POJ 1523 Tarjan求割点
SPF Description Consider the two networks shown below. Assuming that data moves around these network ...
- C - New Year Candles
Problem description Vasily the Programmer loves romance, so this year he decided to illuminate his r ...
- xhtml1-strict.dtd
<!-- Extensible HTML version 1.0 Strict DTD This is the same as HTML 4 Strict except for changes ...
- CSS多列布局(实例)
前言 一列布局 二列布局 三列布局 1 一列布局 一列布局: HTML部分 <!DOCTYPE html> <html> <head> <meta chars ...
- Django中关于MySQL的bug总结
bug one: You are trying to add a non-nullable field 'height' to person without a default; we can't d ...
- 【Linux】ubuntu中怪异的vi编辑器
由于前几天一场windows系统的比特币勒索病毒,我下狠心装了Linux的ubuntu版本.可是今天在使用命令行中的vi编辑器时出现了怪异的现象:backspace不能删除,编辑模式回车随机出现字母. ...
- hibernate_08_关联映射_一对多
hibernate的映射关系 一对多.多对一.一对一.多对多. 常用的是一对多和多对一. 在数据库中可以通过添加主外键的关联,表现一对多的关系:在hibernate中通过在一方持有多方的集合实现,即在 ...
- ASP.NET刷新页面的六种方法
第一: private void Button1_Click( object sender, System.EventArgs e ) { Response.Redirect( Requ ...