CloudBox范例代码:CloudLED

长平狐 发布于 2012/10/08 15:17
阅读 115
收藏 0

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

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"
#endif

CBLed::CBLed()
{
	m_isOn = false;
#ifdef __CBIOS__
	m_led = new CBLediOS();
#else
	m_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_IPHONE
	AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch]) {
        [device lockForConfiguration:nil];
        [device setTorchMode: AVCaptureTorchModeOn];
        [device unlockForConfiguration];
    }
#endif
}
void CBLediOS::turnOffLed()
{
#ifdef TARGET_OS_IPHONE
	AVCaptureDevice *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);
}




原文链接:http://blog.csdn.net/cloudhsu/article/details/7471905
加载中
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部