先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7
深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新HarmonyOS鸿蒙全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
如果你需要这些资料,可以添加V获取:vip204888 (备注鸿蒙)
正文
public void surfaceDestroyed(SurfaceOps callbackSurfaceOps) {
}
}
打开相机
创建surface后触发surfaceCreated回调,执行打开相机的操作。打开相机并添加相片接收的监听,代码示例如下:
private void openCamera() {
CameraKit cameraKit = CameraKit.getInstance(getApplicationContext());
String[] cameraLists = cameraKit.getCameraIds();
String cameraId = cameraLists.length > 1 && isCameraRear ? cameraLists[1] : cameraLists[0];
CameraStateCallbackImpl cameraStateCallback = new CameraStateCallbackImpl();
cameraKit.createCamera(cameraId, cameraStateCallback, creamEventHandler);
}
/**
- CameraStateCallbackImpl 相机状态回调
*/
class CameraStateCallbackImpl extends CameraStateCallback {
CameraStateCallbackImpl() {
}
@Override
public void onCreated(Camera camera) {
// 获取预览
previewSurface = surfaceProvider.getSurfaceOps().get().getSurface();
if (previewSurface == null) {
LogUtils.error(TAG, “create camera filed, preview surface is null”);
return;
}
// Wait until the preview surface is created.
try {
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException exception) {
LogUtils.warn(TAG, “Waiting to be interrupted”);
}
CameraConfig.Builder cameraConfigBuilder = camera.getCameraConfigBuilder();
// 配置预览
cameraConfigBuilder.addSurface(previewSurface);
camera.configure(cameraConfigBuilder.build());
cameraDevice = camera;
enableImageGroup();
}
@Override
public void onConfigured(Camera camera) {
FrameConfig.Builder framePreviewConfigBuilder
= camera.getFrameConfigBuilder(Camera.FrameConfigType.FRAME_CONFIG_PREVIEW);
framePreviewConfigBuilder.addSurface(previewSurface);
// 开启循环捕捉
camera.triggerLoopingCapture(framePreviewConfigBuilder.build());
}
private void enableImageGroup() {
if (!exitImage.isEnabled()) {
exitImage.setEnabled(true);
switchCameraImage.setEnabled(true);
}
}
}
切换前后置摄像头
点击切换摄像头图标后,执行切换前后置摄像头操作,代码示例如下:
private void switchClicked() {
isCameraRear = !isCameraRear;
openCamera();
}
8. 效果展示
人脸识别FA(MainAbilitySlice)完成了检验设备是否支持人脸识别,人脸识别,人脸识别结果显示,成功后跳转到打开相机的FA(OpenCameraSlice);相机FA实现了相机的打开,拍照,相片存储,摄像头切换的功能。具体效果图如下:
人脸识别初始页面:
人脸识别结果显示:
相机页面:
9. 完整代码示例
编写布局与样式
1.base/graphic/background_ability_main.xml
2.base/graphic/button_element.xml
<?xml version="1.0" encoding="utf-8"?>
3.base/layout/ability_main.xml
<?xml version="1.0" encoding="utf-8"?>
4.base/layout/ability_open_camera.xml
<?xml version="1.0" encoding="utf-8"?>
功能逻辑代码
1.com/huawei/cookbook/slice/MainAbilitySlice.java
package com.huawei.cookbook.slice;
import com.huawei.cookbook.MainAbility;
import com.huawei.cookbook.OpenCamera;
import com.huawei.cookbook.ResourceTable;
import com.huawei.cookbook.util.FaceAuthResult;
import com.huawei.cookbook.util.LogUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.biometrics.authentication.BiometricAuthentication;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import java.util.concurrent