首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么世博相机和vue-本地的相机没有捕捉到图像?

为什么世博相机和vue-本地的相机没有捕捉到图像?
EN

Stack Overflow用户
提问于 2021-02-22 20:26:58
回答 2查看 128关注 0票数 1

我正在努力学习vue-母语,并创建一个实践应用程序,使用相机和捕捉图像。在文档中,他们使用一些ref为其分配相机,但我不知道在vue-本地传递ref的确切方法。我做了很少的家庭作业,在git上发现了这个问题,但也没有帮助。

参考文献: https://github.com/GeekyAnts/vue-native-core/issues/88

医生(vue-本地): https://vue-native.io/docs/device-apis.html#Camera

Doc (展览馆): https://docs.expo.io/versions/latest/sdk/camera/#takepictureasync

代码片段是:

代码语言:javascript
复制
<template>
  <view class="container">
    <camera 
      class="camera" 
      :type=type
      ref="camera"  
    >
      <view class="camera-view-1">
          <view class="camera-view-2">
            <view class="camera-view-3">
              <touchable-opacity 
                class="camera-touchable-opacity"
                :on-press="_takePicture"
              >
                <text style="color: #FF0000">Capture</text>
              </touchable-opacity>
            </view>
          </view>
        </view>    
    </camera>
    <view>
      <touchable-opacity
        :on-press="_changeCamera">
        <text class="change-cam"> Change Camera </text>
      </touchable-opacity>
      <button title="Go to home screen" @press="goToHomeScreen"></button>    
    </view>
  </view>
</template>

<script>
import * as Permissions from 'expo-permissions';
import { Camera } from 'expo-camera';        
export default {
  data: function() {
    return {
      hasCameraPermission: false,
      type: Camera.Constants.Type.back,
      img: '',
    };
  },
  props: {
    navigation: { type: Object }
  },
  methods: {
    goToHomeScreen() {
      this.navigation.navigate("Home");
    },
    _changeCamera: function() {
        this.type = (this.type === Camera.Constants.Type.back ? Camera.Constants.Type.front : Camera.Constants.Type.back);
    },
    _takePicture: async function() {
      if(!camera) return;
      const photo = await camera.takePictureAsync();
    }
  },
  mounted: function() {
    Permissions.askAsync(Permissions.CAMERA)
      .then(status => {
        hasCameraPermission = status.status == "granted" ? true : false;
      }).catch((err)=>{
          console.log(err);
      });
  },
  components: { Camera },
};
</script>

<style>
.container, .camera {
  flex: 1;
}

.change-cam {
  color: #FF0000;
  text-align: center;
}
</style>

当我点击捕获按钮时,我得到一个没有定义camera的错误。我想我没有正确地使用ref。有人能建议在vue-native中使用它的正确方法吗?谢谢

EN

回答 2

Stack Overflow用户

发布于 2021-09-22 06:27:44

代码语言:javascript
复制
#In which code open camera and take picture# 
#Note: npm/yarn install expo-permissions#
#npm/yarn install expo-camera#    
    <template>
      <view class="container">
        <camera
          class="container"
          :type="this.type"
          ref="this.camera"
          v-if="!!myPreviewImg.uri === false"
        />
        <touchable-opacity
          class="camera-touchable-opacity"
          :on-press="capturePic"
          :disabled="!readyCapture"
          v-if="!!myPreviewImg.uri === false"
        >
          <text :style="{ color: 'white', fontSize: 20, backgroundColor: 'blue' }"
            >Take Picture</text
          >
        </touchable-opacity>
    
        <View v-if="!!myPreviewImg.uri === true">
          <Image style="width: 100%; height: 95%" :source="myPreviewImg"> </Image>
          <touchable-opacity class="camera-touchable-opacity" :on-press="close">
            <text :style="{ color: 'white', fontSize: 20, backgroundColor: 'blue' }"
              >Close</text
            >
          </touchable-opacity>
        </View>
      </view>
    </template>
    <script>
    import * as Permissions from "expo-permissions";
    import { Camera } from "expo-camera";
    
    export default {
      data: function () {
        return {
          hasCameraPermission: false,
          type: Camera.Constants.Type.back,
          isPreview: false,
          ref: null,
          cameraRef: "",
          readyCapture: false,
          myPreviewImg: {
            uri: false,
          },
        };
      },
      mounted: function () {
        this.cameraPic();
      },
      methods: {
        capturePic: async function () {
          let data = await this.$refs["this.camera"].takePictureAsync();
          console.log("photo", data);
          this.myPreviewImg = {
            uri: data.uri,
          };
          this.readyCapture = true;
        },
        cameraPic: async function () {
          let { status } = await Camera.requestPermissionsAsync();
          if (status === "granted") {
            console.log("Permission to access location was denied");
            this.hasCameraPermission = status == "granted" ? true : false;
            console.log(this.hasCameraPermission);
            this.readyCapture = true;
            return;
          }
        },
        close: function () {
          this.myPreviewImg = {
            uri: false,
          };
        },
      },
      components: { Camera },
    };
    </script>
    <style>
    .container {
      flex: 1;
    }
    .text-color-primary {
      color: blue;
    }
    </style>
票数 0
EN

Stack Overflow用户

发布于 2021-10-06 21:02:31

您可以在您的方法中将相机称为this.$refs.camera,因为您在代码中将元素上的引用定义为ref=" camera“。

捕获函数将是this.$refs.camera.takePictureAsync()

在你的代码中

代码语言:javascript
复制
_takePicture: async function() {
      if(!camera) return;
      const photo = await this.$refs.camera.takePictureAsync();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66322934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档