Android Camera開發手電筒功能

NO IMAGE
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

這是一個簡單的運用Android Camera開發手電筒功能,AndroidManifest.xml檔案的入口是startapp,這個檔案沒上傳上來,大家可以自己寫。

flashlight.java


package com.android.app;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast; 
public class Main extends Activity {
private boolean isopent = false;
private Camera camera; 
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
View view = View.inflate(this, R.layout.main, null);
setContentView(view);
TextView img_but = (TextView) findViewById(R.id.main_img);
img_but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!isopent) {
Toast.makeText(getApplicationContext(), "您已經開啟了手電筒", 0)
.show();
camera = Camera.open();
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview(); // 開始亮燈
isopent = true;
} else {
Toast.makeText(getApplicationContext(), "關閉了手電筒",
Toast.LENGTH_SHORT).show();
camera.stopPreview(); // 關掉亮燈
camera.release(); // 關掉照相機
isopent = false;
}
}
});
}
}

佈局檔案程式碼


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"" rel="nofollow" target="_blank">http://schemas.android.com/apk/res/android"</a>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@ id/main_img"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_body">
</TextView>
</LinearLayout>

AndroidManifest.xml檔案


<manifest xmlns:android="<a href="http://schemas.android.com/apk/res/android"" rel="nofollow" target="_blank">http://schemas.android.com/apk/res/android"</a>
package="com.android.app"
android:versionCode="1"
android:versionName="1.0" > 
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".AppStart" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main" >
</activity>
</application>
<!-- 攝像頭、手電筒 -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
</manifest>

相關文章

Android 開發 最新文章