当前位置:   article > 正文

yolov5 ncnn android app 增加保存功能_yolov5存储照片

yolov5存储照片

参考项目地址    GitHub - nihui/ncnn-android-yolov5

 

布局文件main.xml 里面增加一个Button用来保存识别图片之后的结果图

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="#6B8E23">
  7. <LinearLayout
  8. android:orientation="horizontal"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content">
  11. <Button
  12. android:id="@+id/buttonImage"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="选图" />
  16. <Button
  17. android:id="@+id/buttonDetect"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="识别-cpu" />
  21. <Button
  22. android:id="@+id/buttonDetectGPU"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="识别-gpu" />
  26. <Button
  27. android:id="@+id/buttonSave"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="保存结果" />
  31. </LinearLayout>
  32. <ImageView
  33. android:id="@+id/imageView"
  34. android:layout_width="fill_parent"
  35. android:layout_height="fill_parent"
  36. android:layout_weight="1" />
  37. </LinearLayout>

AndroidManifest.xml 里面增加权限申请

  1. <!-- 往SDCard写入数据权限 -->
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  3. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" tools:ignore="ProtectedPermissions" />

MainActivity.java 里面重写public void onCreate(),在Button buttonDetectGPU代码块的后面增加一个Button类型按钮,注册监听器,并调用saveBitmap()方法。

  1. Button buttonSave = (Button) findViewById(R.id.buttonSave);
  2. buttonSave.setOnClickListener(new View.OnClickListener() {
  3. @Override
  4. public void onClick(View view) {
  5. if (yourSelectedImage == null)
  6. return;
  7. saveBitmap(bitmap);
  8. }
  9. });

增加saveBitmap()方法,需要传一个Bitmap类型的参数进去。

  1. public void saveBitmap(Bitmap bitmap) {
  2. new Thread(new Runnable() {
  3. @Override
  4. public void run() {
  5. try {
  6. // 保存图片到SD卡上
  7. File file = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + String.valueOf(System.currentTimeMillis()) + "SS.jpg");
  8. FileOutputStream stream = new FileOutputStream(file);
  9. // compress - 压缩的意思
  10. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
  11. //存储完成后需要清除相关的进程
  12. stream.flush();
  13. stream.close();
  14. Toast.makeText(MainActivity.this, "保存图片成功", Toast.LENGTH_LONG).show();
  15. } catch (Exception e) {
  16. Toast.makeText(MainActivity.this, "保存图片失败", Toast.LENGTH_LONG).show();
  17. e.printStackTrace();
  18. }
  19. }
  20. }).start();
  21. }

最后,不知道哪里的问题,还是没有实现保存功能。可能是传参数bitmap不对劲。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号