这个代码是一个跨平台的手电筒程序

CloudLED是透过跨平抬筐架CloudBox开发的

如何在iPhone控制LED

如何在Android控制LED

这个范例示范了如何将实践抽离,面对抽象的方式

到我的资源可下载源码

/* *  CBLed.h *  CloudBox Cross-Platform Framework Project * *  Created by Cloud on 2012/1/14. *  Copyright 2012 Cloud Hsu. All rights reserved. * */class CBLedBase;class CBLed{private:bool m_isOn;CBLedBase* m_led;public:CBLed();~CBLed();bool getOn() { return m_isOn;}void switchLed();void turnOnLed();void turnOffLed();};

/* *  CBLed.cpp *  CloudBox Cross-Platform Framework Project * *  Created by Cloud on 2012/1/14. *  Copyright 2012 Cloud Hsu. All rights reserved. * */#include "CBLed.h"#ifdef __CBIOS__#include "CBLediOS.h"#else#include "../Android/CBLedAndroid.h"#endifCBLed::CBLed(){m_isOn = false;#ifdef __CBIOS__m_led = new CBLediOS();#elsem_led = new CBLedAndroid();#endif}CBLed::~CBLed(){delete m_led;}void CBLed::switchLed(){if(m_isOn){turnOffLed();}else{turnOnLed();}}void CBLed::turnOnLed(){if(!m_isOn){m_led->turnOnLed();m_isOn = true;}}void CBLed::turnOffLed(){if(m_isOn){m_led->turnOffLed();m_isOn = false;}}

/* *  CBLediOS.h *  CloudBox Cross-Platform Framework Project * *  Created by Cloud on 2012/1/14. *  Copyright 2012 Cloud Hsu. All rights reserved. * */#include "CBLedBase.h"class CBLediOS : public CBLedBase{public:CBLediOS();~CBLediOS();void turnOnLed();void turnOffLed();};

/* *  CBLediOS.mm *  CloudBox Cross-Platform Framework Project * *  Created by Cloud on 2012/1/14. *  Copyright 2012 Cloud Hsu. All rights reserved. * */#include "CBLediOS.h"#import <AVFoundation/AVFoundation.h>CBLediOS::CBLediOS(){}CBLediOS::~CBLediOS(){}void CBLediOS::turnOnLed(){#ifdef TARGET_IPHONE_SIMULATOR#endif#ifdef TARGET_OS_IPHONEAVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];    if ([device hasTorch]) {        [device lockForConfiguration:nil];        [device setTorchMode: AVCaptureTorchModeOn];        [device unlockForConfiguration];    }#endif}void CBLediOS::turnOffLed(){#ifdef TARGET_OS_IPHONEAVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];    if ([device hasTorch]) {        [device lockForConfiguration:nil];        [device setTorchMode: AVCaptureTorchModeOff];        [device unlockForConfiguration];    }#endif}

/* *  CloudLed.java *  CloudBox Cross-Platform Framework Project * *  Created by Cloud Hsu on 2012/1/17. *  Copyright 2012 Cloud Hsu. All rights reserved. * */package com.clouddevelop.cloudbox;import android.hardware.Camera;import android.util.Log;public class CloudLed {Camera m_Camera;public CloudLed(){}public void turnOn(){try{Log.i("cloudbox-app", "CloudLed.turnOn()");m_Camera = Camera.open();Camera.Parameters mParameters;mParameters = m_Camera.getParameters();mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);m_Camera.setParameters(mParameters);}catch(Exception ex){ex.printStackTrace();}}public void turnOff(){try{Log.i("cloudbox-app", "CloudLed.turnOff()");Camera.Parameters mParameters;mParameters = m_Camera.getParameters();mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);m_Camera.setParameters(mParameters);m_Camera.release();}catch(Exception ex){ex.printStackTrace();}}}

/* *  CBLedAndroid.h *  CloudBox Cross-Platform Framework Project * *  Created by Cloud Hsu on 2012/1/17. *  Copyright 2012 Cloud Hsu. All rights reserved. * */#include <jni.h>#include "../Extends/CBLedBase.h"#include "CBJNI.h"class CBLedAndroid : public CBLedBase, public CBJNI{private:jmethodID m_turnOn;jmethodID m_turnOff;public:CBLedAndroid();~CBLedAndroid();void turnOnLed();void turnOffLed();};

/* *  CBLedAndroid.cpp *  CloudBox Cross-Platform Framework Project * *  Created by Cloud Hsu on 2012/1/17. *  Copyright 2012 Cloud Hsu. All rights reserved. * */#include "CBLedAndroid.h"#include "def.h"#include "../CBLibrary.h"CBLedAndroid::CBLedAndroid(){initial("com/clouddevelop/cloudbox/CloudLed");m_turnOn = g_env->GetMethodID(m_mainClass, "turnOn", "()V");m_turnOff = g_env->GetMethodID(m_mainClass, "turnOff", "()V");}CBLedAndroid::~CBLedAndroid(){}void CBLedAndroid::turnOnLed(){g_env->CallVoidMethod(m_mainObject, m_turnOn);}void CBLedAndroid::turnOffLed(){g_env->CallVoidMethod(m_mainObject, m_turnOff);}



更多相关文章

  1. Android(安卓)中 控制 wifi
  2. Android(安卓)App驱动Arduino通过蓝牙控制交流调光器
  3. Android播放音频之按钮控制
  4. android 带手电筒的扫一扫(1 可以自动打开手电筒,2 可以自动对焦,增
  5. 国外极客打造Android遥控玩具坦克 可网上控制驾驶
  6. android——沙漏计时器
  7. [Android(安卓)Pro] 控制硬加速 hardwareAccelerated 在3.0才有
  8. Android(安卓)控制线程的暂停和恢复
  9. android之远程控制电脑播放ppt

随机推荐

  1. Android中音视频合成的几种方案详析
  2. Android(安卓)中自定义View的应用(二)
  3. Android(安卓)Mediaplayer 三种从资源文
  4. Android控件之SlidingDrawer(滑动式抽屉)详
  5. Android开发基础之AlertDialog的单选对话
  6. Activity有几点你可能不知道的
  7. Android中有关Handler的使用(一)
  8. Android(安卓)蓝牙开发实例--蓝牙聊天程
  9. Android培训---支持不同的平台版本
  10. 国内的几款比较著名的android系统