一个简单的闪光灯demo操作

不用问我为什么这么用,这个框架也没这么用过,是在网上找的一些资料加上自己看框架总结的一些东西,只作为参考学习所用

导入#import <AVFoundation/AVFoundation.h>框架
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *bt=[UIButton buttonWithType:UIButtonTypeCustom];
    bt.backgroundColor=[UIColor blueColor];
    [bt setTitle:@"闪光灯" forState:0];
    [bt sizeToFit];
    bt.center=self.view.center;
    
    [bt addTarget:self action:@selector(btEvent:)
 forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:bt];
}
-(void)btEvent:(UIButton *)sender{
    //使用定时器
    sosTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self
                                                   selector:@selector(open)
                                                   userInfo:nil
                                                    repeats:YES];
    [sosTimer fire];
}

-(void)open{
    
        AVCaptureSession * session = [[AVCaptureSession alloc]init];
        AVCaptureDeviceInput *input =[AVCaptureDeviceInput deviceInputWithDevice:device
                                                                             error:nil];
        [session addInput:input];
        AVCaptureVideoDataOutput * output = [[AVCaptureVideoDataOutput alloc]init];
        [session addOutput:output];
        [session beginConfiguration];
        [device lockForConfiguration:nil];
        [device setTorchMode:AVCaptureTorchModeOn];
        
        [device unlockForConfiguration];
        [session commitConfiguration];
        [session startRunning];

}


参考资料:

http://blog.163.com/chester_lp/blog/static/139794082012119112834437/

http://stackoverflow.com/questions/3204008/how-do-i-use-avcaptureflashmode/3204078#3204078

我可以给你一个Android使用Camera2Demo的示例代码:import android.Manifest; import android.app.Activity; import android.content.Context; import android.content.pm.PackageManager; import android.graphics.ImageFormat; import android.graphics.SurfaceTexture; import android.hardware.camera2.CameraAccessException; import android.hardware.camera2.CameraCaptureSession; import android.hardware.camera2.CameraCharacteristics; import android.hardware.camera2.CameraDevice; import android.hardware.camera2.CameraManager; import android.hardware.camera2.CaptureRequest; import android.hardware.camera2.params.StreamConfigurationMap; import android.media.ImageReader; import android.os.Build; import android.os.Handler; import android.os.HandlerThread; import android.support.annotation.NonNull; import android.support.annotation.RequiresApi; import android.support.v4.app.ActivityCompat; import android.util.Size; import android.util.SparseIntArray; import android.view.Surface; import android.view.TextureView; import android.widget.Toast;@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public class Camera2Demo { private TextureView textureView; private Context context; private Size previewSize; private Handler backgroundHandler; private HandlerThread backgroundThread; private ImageReader imageReader; private CaptureRequest.Builder previewRequestBuilder; private CameraDevice cameraDevice; private CameraCaptureSession captureSession; private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); static { ORIENTATIONS.append(Surface.ROTATION_0, 0); ORIENTATIONS.append(Surface.ROTATION_90, 90); ORIENTATIONS.append(Surface.ROTATION_180, 180); ORIENTATIONS.append(Surface.ROTATION_270, 270); } public Camera2Demo(TextureView textureView, Context context) { this.textureView = textureView; this.context = context; } public void openCamera() { CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE); try { String cameraID = cameraManager.getCameraIdList()[0]; CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraID); StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); previewSize = map.getOutputSizes(SurfaceTexture.class)[0]; // 权限检查 if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { return; } cameraManager.openCamera(cameraID, stateCallBack, backgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } private CameraDevice.StateCallback stateCallBack = new CameraDevice.StateCallback() { @Override public void onOpened(@NonNull CameraDevice camera) { cameraDevice = camera; startPreview(); } @Override public void onDisconnected(@NonNull CameraDevice camera) { camera.close(); cameraDevice = null; } @Override public void onError(@NonNull CameraDevice camera, int error) { Toast.makeText(context, "摄像头开启失败", Toast.LENGTH_SHORT).show(); } }; private void startPreview() { SurfaceTexture surfaceTexture = textureView.getSurfaceTexture(); surfaceTexture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight()); Surface previewSurface = new Surface(surfaceTexture); try { previewRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); previewRequestBuilder.addTarget(previewSurface); cameraDevice.createCaptureSession(Arrays.asList(previewSurface, imageReader.getSurface()), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession session) { captureSession = session; try { captureSession.setRepeatingRequest(previewRequestBuilder.build(), null, backgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(@NonNull CameraCaptureSession session) { } }, backgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值