android控制手机强制手机横竖方向,使用android IOIO和安卓手机制作视频遥控小车(控制灯的开关、实时视频传输、方向控制)...
android IOIO是通過安卓手機的USB接口控制的電路板,通過android
IOIO可以不需要太多的硬件知識,通過安卓手機轉接的IOIO板控制外圍設備,配件有紙盒、L298N電機驅動模塊、舊手機一部(系統必須在安卓2.3以上(與IOIO的固件有關)),舊手機電池兩塊,LED燈一組,USB線一根、減速電機帶輪子
兩個、萬向輪一個、杜邦線6根左右就夠了
先搭好硬件環境,然后進行安卓代碼的編寫,以下是代碼:
視頻傳輸部分參考了網上的代碼?
??public
class SocketCameraActivity extends IOIOActivity implements
SurfaceHolder.Callback,
Camera.PreviewCallback{
private SurfaceView
mSurfaceview = null; // SurfaceView對象:(視圖組件)視頻顯示
private
SurfaceHolder mSurfaceHolder = null; //
SurfaceHolder對象:(抽象接口)SurfaceView支持類
private Camera
mCamera = null; // Camera對象,相機預覽
DigitalOutput
left,right,light;
ServerSocket
ser_socket;
Socket
cli_socket;
private String
pUsername="XZY";
private String
serverUrl="192.168.1.100";
private int
serverPort=8888;
private int
VideoPreRate=1;
private int
tempPreRate=0;
private int
VideoQuality=85;
private float
VideoWidthRatio=1;
private float
VideoHeightRatio=1;
private int
VideoWidth=320;
private int
VideoHeight=240;
private int
VideoFormatIndex=0;
private boolean
startSendVideo=false;
private boolean
connectedServer=false;
private Button
myBtn01, myBtn02;
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//禁止屏幕休眠
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mSurfaceview = (SurfaceView)
findViewById(R.id.camera_preview);
myBtn01=(Button)findViewById(R.id.button1);
myBtn02=(Button)findViewById(R.id.button2);
ServerThread st=new ServerThread();
st.start();
//開始連接主機按鈕
myBtn01.setOnClickListener(new
OnClickListener(){
public
void onClick(View v) {
//Common.SetGPSConnected(LoginActivity.this, false);
if(connectedServer){//停止連接主機,同時斷開傳輸
startSendVideo=false;
connectedServer=false;
myBtn02.setEnabled(false);
myBtn01.setText("開始連接");
myBtn02.setText("開始傳輸");
//斷開連接
Thread th = new
MySendCommondThread("PHONEDISCONNECT|"+pUsername+"|");
th.start();
}
else//連接主機
{
//啟用線程發送命令PHONECONNECT
Thread th = new
MySendCommondThread("PHONECONNECT|"+pUsername+"|");
th.start();
connectedServer=true;
myBtn02.setEnabled(true);
myBtn01.setText("停止連接");
}
}});
myBtn02.setEnabled(false);
myBtn02.setOnClickListener(new
OnClickListener(){
public
void onClick(View v) {
if(startSendVideo)//停止傳輸視頻
{
startSendVideo=false;
myBtn02.setText("開始傳輸");
}
else{ // 開始傳輸視頻
startSendVideo=true;
myBtn02.setText("停止傳輸");
}
}});
}
@Override
public void
onStart()//重新啟動的時候
{
mSurfaceHolder = mSurfaceview.getHolder(); //
綁定SurfaceView,取得SurfaceHolder對象
mSurfaceHolder.addCallback(this); //
SurfaceHolder加入回調接口
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//
設置顯示器類型,setType必須設置
//讀取配置文件
SharedPreferences preParas =
PreferenceManager.getDefaultSharedPreferences(SocketCameraActivity.this);
pUsername=preParas.getString("Username",
"XZY");
serverUrl=preParas.getString("ServerUrl",
"172.17.95.1");
String tempStr=preParas.getString("ServerPort",
"8888");
serverPort=Integer.parseInt(tempStr);
tempStr=preParas.getString("VideoPreRate",
"1");
VideoPreRate=Integer.parseInt(tempStr);
tempStr=preParas.getString("VideoQuality",
"85");
VideoQuality=Integer.parseInt(tempStr);
tempStr=preParas.getString("VideoWidthRatio",
"100");
VideoWidthRatio=Integer.parseInt(tempStr);
tempStr=preParas.getString("VideoHeightRatio",
"100");
VideoHeightRatio=Integer.parseInt(tempStr);
VideoWidthRatio=VideoWidthRatio/100f;
VideoHeightRatio=VideoHeightRatio/100f;
super.onStart();
}
@Override
protected void
onResume() {
super.onResume();
InitCamera();
}
private void
InitCamera(){
try{
mCamera =
Camera.open();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void
onPause() {
super.onPause();
try{
if
(mCamera != null) {
mCamera.setPreviewCallback(null); // !!這個必須在前,不然退出出錯
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void
surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
if (mCamera == null) {
return;
}
mCamera.stopPreview();
mCamera.setPreviewCallback(this);
mCamera.setDisplayOrientation(90);
//設置橫行錄制
//獲取攝像頭參數
Camera.Parameters parameters =
mCamera.getParameters();
Size size =
parameters.getPreviewSize();
VideoWidth=size.width;
VideoHeight=size.height;
VideoFormatIndex=parameters.getPreviewFormat();
mCamera.startPreview();
}
@Override
public void
surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try {
if
(mCamera != null) {
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void
surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
if (null != mCamera) {
mCamera.setPreviewCallback(null); // !!這個必須在前,不然退出出錯
mCamera.stopPreview();
mCamera.release();
mCamera =
null;
}
}
@Override
public void
onPreviewFrame(byte[] data, Camera camera) {
// TODO Auto-generated method stub
//如果沒有指令傳輸視頻,就先不傳
if(!startSendVideo)
return;
if(tempPreRate
tempPreRate++;
return;
}
tempPreRate=0;
try {
if(data!=null)
{
Log.d("YuvImage","success");
YuvImage image = new
YuvImage(data,VideoFormatIndex, VideoWidth,
VideoHeight,null);
if(image!=null)
{
ByteArrayOutputStream outstream = new
ByteArrayOutputStream();
//在此設置圖片的尺寸和質量
image.compressToJpeg(new Rect(0, 0,
(int)(VideoWidthRatio*VideoWidth),
(int)(VideoHeightRatio*VideoHeight)), VideoQuality,
outstream);
outstream.flush();
//啟用線程將圖像數據發送出去
Thread th = new
MySendFileThread(outstream,pUsername,serverUrl,serverPort);
th.start();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean
onCreateOptionsMenu(Menu menu)
{
menu.add(0,0,0,"系統設置");
menu.add(0,1,1,"關于程序");
menu.add(0,2,2,"退出程序");
return
super.onCreateOptionsMenu(menu);
}
public boolean
onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);//獲取菜單
switch(item.getItemId())//菜單序號
{
case
0:
//系統設置
{
Intent intent=new
Intent(this,SettingActivity.class);
startActivity(intent);
}
break;
case
1://關于程序
{
new
AlertDialog.Builder(this)
.setTitle("關于本程序")
.setMessage("遙控小車程序")
.setPositiveButton
(
"我知道了",
new
DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int
which)
{
}
}
)
.show();
}
break;
case
2://退出程序
{
//殺掉線程強制退出
android.os.Process.killProcess(android.os.Process.myPid());
}
break;
}
return true;
}
class
MySendCommondThread extends Thread{
private String commond;
public MySendCommondThread(String
commond){
this.commond=commond;
}
public void run(){
//實例化Socket
try
{
Socket socket=new
Socket(serverUrl,serverPort);
PrintWriter out = new
PrintWriter(socket.getOutputStream());
out.println(commond);
out.flush();
} catch
(UnknownHostException e) {
} catch
(IOException e) {
}
}
}
class
MySendFileThread extends Thread{
private String username;
private String ipname;
private int port;
private byte byteBuffer[] = new
byte[1024];
private OutputStream outsocket;
private ByteArrayOutputStream
myoutputstream;
public MySendFileThread(ByteArrayOutputStream
myoutputstream,String username,String ipname,int port){
this.myoutputstream = myoutputstream;
this.username=username;
this.ipname = ipname;
this.port=port;
try
{
myoutputstream.close();
} catch
(IOException e) {
e.printStackTrace();
}
}
public void run() {
try{
//將圖像數據通過Socket發送出去
Socket tempSocket = new
Socket(ipname, port);
outsocket =
tempSocket.getOutputStream();
//寫入頭部數據信息
String
msg=java.net.URLEncoder.encode("PHONEVIDEO|"+username+"|","utf-8");
byte[] buffer=
msg.getBytes();
outsocket.write(buffer);
ByteArrayInputStream
inputstream = new
ByteArrayInputStream(myoutputstream.toByteArray());
int amount;
while ((amount =
inputstream.read(byteBuffer)) != -1) {
outsocket.write(byteBuffer, 0,
amount);
}
myoutputstream.flush();
myoutputstream.close();
tempSocket.close();
Log.d("MySendFileThread","success");
} catch
(IOException e) {
e.printStackTrace();
}
}
}
class Looper extends
BaseIOIOLooper {
@Override
protected void setup() throws
ConnectionLostException {
left=ioio_.openDigitalOutput(1);
right=ioio_.openDigitalOutput(2);
light=ioio_.openDigitalOutput(3);
left.write(true);
right.write(true);
light.write(true);
}
@Override
public void loop() throws
ConnectionLostException {
}
}
@Override
protected IOIOLooper
createIOIOLooper() {
return new Looper();
}
public void
go()
{
try {
left.write(false);
right.write(false);
} catch (ConnectionLostException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
public void
nstop()
{
try {
left.write(true);
right.write(true);
} catch (ConnectionLostException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
public void
goleft()
{
try {
left.write(false);
right.write(true);
} catch (ConnectionLostException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
public void
goright()
{
try {
left.write(true);
right.write(false);
} catch (ConnectionLostException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
public void
lightOn()
{
try {
light.write(false);
} catch (ConnectionLostException e) {
e.printStackTrace();
}
}
public void
lightOff()
{
try {
light.write(true);
} catch (ConnectionLostException e) {
e.printStackTrace();
}
}
class ServerThread
extends Thread
{
public void run()
{
try
{
ser_socket=new
ServerSocket(8000);
} catch
(IOException e) {
e.printStackTrace();
}
while
(true)
{
try {
cli_socket = ser_socket.accept();
Log.e("接受連接", "accept");
BufferedReader in = new BufferedReader(new
InputStreamReader(cli_socket.getInputStream()));
String str ="";
String temp;
while((temp=in.readLine())!=null)
{
str=str+temp;
Log.e("收到數據",temp);
if(temp.equals("lightOn"))
{
Log.d("收到","lightOn");
lightOn();
}
if(temp.equals("lightOff"))
{
lightOff();
}
if(temp.equals("go"))
{
go();
}
if(temp.equals("nstop"))
{
nstop();
}
if(temp.equals("goleft"))
{
goleft();
}
if(temp.equals("goright"))
{
goright();
}
}
}
catch (Exception
e)
{
e.printStackTrace();
}
finally
{
try
{
cli_socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}
}
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的android控制手机强制手机横竖方向,使用android IOIO和安卓手机制作视频遥控小车(控制灯的开关、实时视频传输、方向控制)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ++实现 ipv6数据报_IPV6报文格
- 下一篇: oracle to mysql demo