赞
踩
package com.myharmonyos.camerademo.slice; import com.myharmonyos.camerademo.ResourceTable; import com.myharmonyos.camerademo.utils.L; import com.myharmonyos.camerademo.utils.ToastUtils; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Button; import ohos.agp.components.Component; import ohos.agp.components.surfaceprovider.SurfaceProvider; import ohos.agp.graphics.Surface; import ohos.agp.graphics.SurfaceOps; import ohos.app.Environment; import ohos.eventhandler.EventHandler; import ohos.eventhandler.EventRunner; import ohos.media.camera.CameraKit; import ohos.media.camera.device.*; import ohos.media.image.Image; import ohos.media.image.ImageReceiver; import ohos.media.image.common.ImageFormat; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class CameraAbilitySlice extends AbilitySlice { private String TAG = "TEST"; private SurfaceProvider surfaceProvider; private boolean isFrontCamera; // 前后摄像头标识 private EventHandler eventHandler = null; private Camera mycamera; private Surface surface = null; private ImageReceiver imageReceiver = null; // 图片接收 private String imagePath = null; // 拍照后路径 @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_camera); eventHandler = new EventHandler(EventRunner.create()); surfaceProvider = (SurfaceProvider) findComponentById(ResourceTable.Id_surface_preview); surfaceProvider.pinToZTop(true); surfaceProvider.getSurfaceOps().get().addCallback(new SurfaceOps.Callback() { @Override public void surfaceCreated(SurfaceOps surfaceOps) { // 将相机中捕获的数据在屏幕中绘制 surface = surfaceOps.getSurface(); createCamera(); } @Override public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) { } @Override public void surfaceDestroyed(SurfaceOps surfaceOps) { } }); // 点击拍照按钮,保存图片 Button btnOpen = (Button) findComponentById(ResourceTable.Id_btn_open); btnOpen.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { FrameConfig.Builder frameConfig = mycamera.getFrameConfigBuilder(Camera.FrameConfigType.FRAME_CONFIG_PICTURE); frameConfig.addSurface(surface); if(imageReceiver != null){ frameConfig.addSurface(imageReceiver.getRecevingSurface()); } // frameConfig.setImageRotation(90); mycamera.triggerLoopingCapture(frameConfig.build()); L.i(TAG, "## imagePath : "+ imagePath); // 因模拟器不支持拍照回调保存图片,文件路径为空(需要使用真机查看保存图片文件地址) String msg = (imagePath == null || "".equals(imagePath) ? "拍照成功":imagePath); ToastUtils.showMsg(msg); } }); } private void createCamera(){ // 创建图片接收器 /* * 特别注意: 模拟器尚不支持 imageReceiver 回调方法 setImageArrivalListener */ imageReceiver = ImageReceiver.create(1440,1080, ImageFormat.JPEG, 9); imageReceiver.setImageArrivalListener(new ImageReceiver.IImageArrivalListener(){ @Override public void onImageArrival(ImageReceiver imageReceiver) { // 保存数据 // 获得/sdcard/Android/data/$(packagename)/files 目录; imagePath = getExternalFilesDir(Environment.DIRECTORY_PICTURES) +"/test"+System.currentTimeMillis()+".jpg"; File saveFile = new File(imagePath); // 创建图片文件 // 读取一张图片数据 Image image = imageReceiver.readNextImage(); Image.Component component = image.getComponent(ImageFormat.ComponentType.JPEG); byte[] bytes = new byte[component.remaining()]; component.read(bytes); FileOutputStream output = null; try { output = new FileOutputStream(saveFile); output.write(bytes); // 写图像数据 output.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (output != null) { try { output.close(); // 关闭流 } catch (IOException e) { e.printStackTrace(); } } image.release(); } } }); CameraKit cameraKit = CameraKit.getInstance(getContext()); String[] cameraIds = cameraKit.getCameraIds(); String cameraId = ""; for (String logicalCameraId : cameraIds) { int faceType = cameraKit.getCameraInfo(logicalCameraId).getFacingType(); switch (faceType) { case CameraInfo.FacingType.CAMERA_FACING_FRONT: if (isFrontCamera) { cameraId = logicalCameraId; } break; case CameraInfo.FacingType.CAMERA_FACING_BACK://后置摄像头 if (!isFrontCamera) { cameraId = logicalCameraId; } break; case CameraInfo.FacingType.CAMERA_FACING_OTHERS: default: break; } } // 创建相机 cameraKit.createCamera(cameraId, new CameraStateCallback() { @Override public void onCreated(Camera camera) { // super.onCreated(camera); surface = surfaceProvider.getSurfaceOps().get().getSurface(); mycamera = camera; CameraConfig.Builder builder = camera.getCameraConfigBuilder(); if(null == builder){ return; } builder.addSurface(surface); if(null != imageReceiver){ builder.addSurface(imageReceiver.getRecevingSurface()); } camera.configure(builder.build()); } @Override public void onConfigured(Camera camera) { super.onConfigured(camera); FrameConfig.Builder builder = camera.getFrameConfigBuilder(Camera.FrameConfigType.FRAME_CONFIG_PREVIEW); builder.addSurface(surface); camera.triggerLoopingCapture(builder.build()); } }, eventHandler); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }
完整示例还未审核通过,审核后会把地址贴到这里
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。