简体   繁体   English

使用按钮触发 React Native 中的相机模块?

[英]Trigger camera module in React Native by using a button?

I'm trying to figure out to trigger the camera module on iOS React Native app.我试图弄清楚在 iOS React Native 应用程序上触发相机模块。 I've been using a custom component to write the code for a button, and I would like that button to trigger the camera interface.我一直在使用自定义组件来编写按钮的代码,我希望该按钮能够触发相机界面。

But I'm having a hard time to understand what is needed to accomplish this.但我很难理解实现这一目标需要什么。

This is what I have so far:这是我到目前为止所拥有的:

import React from 'react';
import { View, StyleSheet, Text } from 'react-native';
import TakePhoto from './TakePhoto';
import CameraRoll from './CameraRoll';

const PhotoOptions = () => {

    const handleCameraPress = () => {
        console.log("Trigger camera");
    }

    return(
        <View style={styles.container}>
            <Text style={styles.label}>Välj en passande bild (valfritt)</Text>
            <View style={styles.columns}>
                <TakePhoto onPress={handleCameraPress} />
                <CameraRoll />
            </View>
        </View>
    )
}

const styles = StyleSheet.create({
    container: {
        borderWidth: 1,
        borderColor: 'red',
        marginBottom: 20,
        paddingBottom: 250
    },
    label: {
        marginBottom: 8
    },
    columns: {
        display: 'flex',
        flexDirection: 'row',
        // flex: 1
    }
});

export default PhotoOptions;

As I saw you are also trying to access to camera roll for picture and other stuff, You can use this lib for this.正如我所看到的,您还尝试访问相机胶卷以获取图片和其他内容,您可以为此使用此库。

https://github.com/react-native-community/react-native-cameraroll https://github.com/react-native-community/react-native-cameraroll

Usage:用法:

import CameraRoll from "@react-native-community/cameraroll";

CameraRoll.getAlbums(params);

CameraRoll.getPhotos(params);

_handleButtonPress = () => {
   CameraRoll.getPhotos({
       first: 20,
       assetType: 'Photos',
     })
     .then(r => {
       this.setState({ photos: r.edges });
     })
     .catch((err) => {
        //Error Loading Images
     });
   };
render() {
 return (
   <View>
     <Button title="Load Images" onPress={this._handleButtonPress} />
     <ScrollView>
       {this.state.photos.map((p, i) => {
       return (
         <Image
           key={i}
           style={{
             width: 300,
             height: 100,
           }}
           source={{ uri: p.node.image.uri }}
         />
       );
     })}
     </ScrollView>
   </View>
 );
}

If you wanted to trigger camera you can use this lib如果你想触发相机,你可以使用这个库

https://github.com/react-native-community/react-native-camera https://github.com/react-native-community/react-native-camera

Usage can found here.用法可以在这里找到。

https://github.com/react-native-community/react-native-camera/blob/master/docs/RNCamera.md https://github.com/react-native-community/react-native-camera/blob/master/docs/RNCamera.md

You also have an option to use this lib to pick some image from gallery or camera.您还可以选择使用此库从图库或相机中选择一些图像。

https://github.com/react-native-community/react-native-image-picker https://github.com/react-native-community/react-native-image-picker

Usage::用法::

import ImagePicker from 'react-native-image-picker';

// More info on all the options is below in the API Reference... just some common use cases shown here
const options = {
  title: 'Select Avatar',
  customButtons: [{ name: 'fb', title: 'Choose Photo from Facebook' }],
  storageOptions: {
    skipBackup: true,
    path: 'images',
  },
};

/**
 * The first arg is the options object for customization (it can also be null or omitted for default options),
 * The second arg is the callback which sends object: response (more info in the API Reference)
 */
ImagePicker.showImagePicker(options, (response) => {
  console.log('Response = ', response);

  if (response.didCancel) {
    console.log('User cancelled image picker');
  } else if (response.error) {
    console.log('ImagePicker Error: ', response.error);
  } else if (response.customButton) {
    console.log('User tapped custom button: ', response.customButton);
  } else {
    const source = { uri: response.uri };

    // You can also display the image using data:
    // const source = { uri: 'data:image/jpeg;base64,' + response.data };

    this.setState({
      avatarSource: source,
    });
  }
});


// Launch Camera:
ImagePicker.launchCamera(options, (response) => {
  // Same code as in above section!
});

// Open Image Library:
ImagePicker.launchImageLibrary(options, (response) => {
  // Same code as in above section!
});

you can use react-native-image-picker to trigger camera你可以使用react-native-image-picker来触发相机

install react-native-image-picker by通过安装 react-native-image-picker

yarn add react-native-image-picker

Example Code:示例代码:

import ImagePicker from 'react-native-image-picker';

// More info on all the options is below in the API Reference... just some common use cases shown here
const options = {
  title: 'Select Avatar',
  customButtons: [{ name: 'fb', title: 'Choose Photo from Facebook' }],
  storageOptions: {
    skipBackup: true,
    path: 'images',
  },
};

/**
 * The first arg is the options object for customization (it can also be null or omitted for default options),
 * The second arg is the callback which sends object: response (more info in the API Reference)
 */
ImagePicker.showImagePicker(options, (response) => {
  console.log('Response = ', response);

  if (response.didCancel) {
    console.log('User cancelled image picker');
  } else if (response.error) {
    console.log('ImagePicker Error: ', response.error);
  } else if (response.customButton) {
    console.log('User tapped custom button: ', response.customButton);
  } else {
    const source = { uri: response.uri };

    // You can also display the image using data:
    // const source = { uri: 'data:image/jpeg;base64,' + response.data };

    this.setState({
      avatarSource: source,
    });
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM