Android 간단 한 손전등 구현 (개인 노트)

5241 단어
XML:



    

    

    
    


Java:
package com.example.flashlightactivite;

import android.Manifest;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;

public class Switch_Activity extends AppCompatActivity {

    ImageButton flashLightSwitch;
    boolean flag = false;
    SurfaceView surfaceView;
    Camera camera;
    Camera.Parameters camera_parameters; //    

//                     
//    Camera.Size camera_size;

    SurfaceHolder surfaceHolder;
//  SurfaceHolder     ,         Surface    。       SurfaceView   Surface      

//    *****      AndroidManifest.xml      :
//    

//      PERMISSION_STORAGE:    
    private static String [] PERMISSION_STORAGE = {
        Manifest.permission.CAMERA
    };

//              REQUEST_PERMISSION_CODE:      
    private static int REQUEST_PERMISSION_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_switch_ );

//                          
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
//           checkSelfPermission:       
            if (ActivityCompat.checkSelfPermission( this,Manifest.permission.CAMERA ) != PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions( this,PERMISSION_STORAGE,REQUEST_PERMISSION_CODE );
            }
        }

        flashLightSwitch = findViewById( R.id.imageButton );
        surfaceView = findViewById( R.id.surfaceView );
        camera = Camera.open();
        camera_parameters = camera.getParameters();
        surfaceHolder = surfaceView.getHolder();

        surfaceHolder.addCallback( new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try{
                    camera.setPreviewDisplay( surfaceHolder );
                }catch(Exception e){
//                         camera   
                    camera.release();
                }
                camera.startPreview();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        } );

//        ImageButton       
       flashLightSwitch.setOnClickListener( new View.OnClickListener() {
           @Override
           public void onClick(View v) {
                if ( !flag ) {
//                       flag   
                    flag = true;
//                          R. . 
                    flashLightSwitch.setImageResource( R.drawable.on );

//                       flash         :  
                    camera_parameters.setFlashMode( Camera.Parameters.FLASH_MODE_TORCH );
                    camera.setParameters( camera_parameters );
                }else {
                    flashLightSwitch.setImageResource( R.drawable.off );
                    flag = false;
//                       flash
                    camera_parameters.setFlashMode( Camera.Parameters.FLASH_MODE_OFF );
                    camera.setParameters( camera_parameters );
                }
           }
       } );

    }
}


/*
* FLASH_MODE_RED_EYE     ,                 。
        FLASH_MODE_TORCH    ,             。
        FLASH_MODE_AUTO    ,           。
        FLASH_MODE_OFF           
        FLASH_MODE_ON               
* */

AndroidManifest.xml :

    
    
    

좋은 웹페이지 즐겨찾기