android 手電筒的實現

來源:互聯網
上載者:User

標籤:des   android   http   io   os   ar   使用   java   for   

android手機用閃光燈做成手電筒的應用非常多,可是有的不能用。

後來發現是除了把 camera device的 flashmode設定成torch外還要開啟預覽:

以下是代碼:


MainActivity.java

package com.android.flashlight;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.widget.ImageView;public class MainActivity extends Activity{private FlashlightSurface mSurface;private ImageView mImageView;private boolean isFlashlightOn = false;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Contants.LogI("MainActivity: onCreate()");mSurface = (FlashlightSurface) findViewById(R.id.surfaceview);mImageView = (ImageView) findViewById(R.id.image);}@Overridepublic boolean onTouchEvent(MotionEvent event) {if(MotionEvent.ACTION_UP == event.getAction()){Contants.LogI("MainActivity: onTouchEvent() : ACTION_UP");if(isFlashlightOn){mSurface.setFlashlightSwitch(false);isFlashlightOn = false;mImageView.setImageResource(R.drawable.flashlight_off);}else{mSurface.setFlashlightSwitch(true);isFlashlightOn = true;mImageView.setImageResource(R.drawable.flashlight_on);}}return super.onTouchEvent(event);}}

FlashlightSurface.java

package com.android.flashlight;import android.content.Context;import android.graphics.PixelFormat;import android.hardware.Camera;import android.util.AttributeSet;import android.view.SurfaceHolder;import android.view.SurfaceView;public class FlashlightSurface extends SurfaceView implements SurfaceHolder.Callback{private SurfaceHolder mHolder;private Camera mCameraDevices;private Camera.Parameters mParameters;public FlashlightSurface(Context context, AttributeSet attrs) {super(context, attrs);Contants.LogI("FlashlightSurface");mHolder = this.getHolder();mHolder.addCallback(this);mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {Contants.LogI("surfaceChanged");mParameters = mCameraDevices.getParameters();if(mParameters != null)mParameters.setPictureFormat(PixelFormat.JPEG);mParameters.setPreviewSize(320, 480);mParameters.setPictureSize(320, 480);mCameraDevices.setParameters(mParameters);mCameraDevices.startPreview();}@Overridepublic void surfaceCreated(SurfaceHolder holder) {Contants.LogI("surfaceCreated");try {mCameraDevices = Camera.open();mCameraDevices.setPreviewDisplay(mHolder);} catch (Exception e) {if(mCameraDevices != null)mCameraDevices.release();mCameraDevices = null;}}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {Contants.LogI("surfaceDestroyed");if(mCameraDevices == null) return;mCameraDevices.stopPreview();mCameraDevices.release();mCameraDevices = null;}/** * 設定手電筒的開關狀態 * @param on : true則開啟,false則關閉 */public void setFlashlightSwitch(boolean on){if(mCameraDevices == null) return;if(mParameters == null){mParameters = mCameraDevices.getParameters();}if(on){mParameters.setFlashMode(Contants.FLASH_MODE_TORCH);}else{mParameters.setFlashMode(Contants.FLASH_MODE_OFF);}Contants.LogI("setFlashlightSwitch-----------------" + on);mCameraDevices.setParameters(mParameters);}}

布局檔案main.xml

<?xml version="1.0" encoding="utf-8"?>   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent" >    <com.android.flashlight.FlashlightSurface        android:id="@+id/surfaceview"        android:layout_width="fill_parent"      android:layout_height="fill_parent"        ></com.android.flashlight.FlashlightSurface>    <ImageView         android:id="@+id/image"        android:layout_width="fill_parent"      android:layout_height="fill_parent"    android:src="@drawable/flashlight_off"    android:background="#FFFFFFFF"        />    </RelativeLayout>

顯然這裡開啟了一個預覽,可是被圖片蓋上了,看不見而已。不然閃光燈不亮。


這裡有個Demo:http://download.csdn.net/detail/liu_zhen_wei/4801779

包含widget表單小組件的使用方法和手電筒的功能實現,安裝後,加入表單小組件(手電筒)到案頭,然後點擊小組件後,開啟手電筒的介面

點擊介面,實現 開啟/關閉 手電筒。

android 手電筒的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.