Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 如何压缩刚用React Native Expo拍摄的照片_React Native_Camera_Compression_Expo_Media Library - Fatal编程技术网

React native 如何压缩刚用React Native Expo拍摄的照片

React native 如何压缩刚用React Native Expo拍摄的照片,react-native,camera,compression,expo,media-library,React Native,Camera,Compression,Expo,Media Library,我正在使用expo和MediaLibrary的摄像头组件保存拍摄的照片。我的问题是,在Galery中使用MediaLibrary保存图像时,如何压缩该图像?我正在尝试压缩它,因为稍后我还会将该图像上载到Firebase存储。到目前为止,这是我现在没有压缩的代码: import React, { useState, useEffect} from 'react'; import { Text, View, TouchableOpacity } from 'react-native'; import

我正在使用expo和MediaLibrary的摄像头组件保存拍摄的照片。我的问题是,在Galery中使用MediaLibrary保存图像时,如何压缩该图像?我正在尝试压缩它,因为稍后我还会将该图像上载到Firebase存储。到目前为止,这是我现在没有压缩的代码:

import React, { useState, useEffect} from 'react';
import { Text, View, TouchableOpacity } from 'react-native';
import { Camera } from 'expo-camera';
import * as MediaLibrary from 'expo-media-library';
import { Dimensions } from 'react-native';
const {height, width} = Dimensions.get('window');

export default function App() {

  const [hasPermission, setHasPermission] = useState(null);
  const [cameraRef, setCameraRef] = useState(null)
  const [type, setType] = useState(Camera.Constants.Type.back);
  useEffect(() => {
    (async () => {
      const { status } = await Camera.requestPermissionsAsync();
      MediaLibrary.requestPermissionsAsync();
      setHasPermission(status === 'granted');
    })();
  }, []);

  if (hasPermission === null) {
    return <View />;
  }

  if (hasPermission === false) {
    return <Text>No access to camera</Text>;
  }

  return (
    <View style={{ flex: 1 }}>
      <Camera style={{ flex: 1 }} type={type} ref={ref => {
        setCameraRef(ref) ;
      }}>
        <View
          style={{
            flex: 1,
            backgroundColor: 'transparent',
            justifyContent: 'flex-end'
          }}>
          <TouchableOpacity style={{alignSelf: 'center', marginBottom: 20}} onPress={async() => {
            if(cameraRef){
              let photo = await cameraRef.takePictureAsync({ skipProcessing: true });
              MediaLibrary.saveToLibraryAsync(photo.uri);
            }
          }}>
            <View style={{ 
              borderWidth: 2,
              borderColor: 'white',
              height: 50,
              width:50,
              display: 'flex',
              justifyContent: 'center',
              alignItems: 'center',
              borderRadius: 25,
              }}
            >
              <View style={{
                borderWidth: 2,
                borderColor: 'white',
                height: 40,
                width:40,
                backgroundColor: 'white',
                borderRadius: 25}} >
              </View>
            </View>
          </TouchableOpacity>
        </View>
      </Camera>
    </View>
  );
}
ImageManipular随expo提供,您可以压缩、调整大小、旋转、裁剪等

签出

图像操纵器随expo附带,您可以压缩、调整大小、旋转、裁剪以及更多操作

签出

图像操纵器已移动到程序包。更新您的示例:

从“React”导入React,{useState,useffect}; 从“react native”导入{Text,View,TouchableOpacity}; 从“expo Camera”导入{Camera}; 从“世博媒体库”导入*作为媒体库; 从“react native”导入{Dimensions}; //新进口 从“expo图像操纵器”导入*作为图像操纵器; const{height,width}=Dimensions.get'window'; 导出默认功能应用程序{ //和以前一样的代码 回来 { setCameraRef; }}> { ifcameraRef{ let photo=wait cameraRef.takePictureAsync{skipping processing:true}; //这里有新代码 const manippresult=wait ImageManipulator.manipleAsync photo.uri, [{rotate:90},{flip:ImageManipulator.FlipType.Vertical}], {压缩:1,格式:ImageManipulator.SaveFormat.PNG} ; MediaLibrary.SaveToLibrary AsynchManipResult.uri; } }}> ; } .

图像操纵器已移动到程序包。更新您的示例:

从“React”导入React,{useState,useffect}; 从“react native”导入{Text,View,TouchableOpacity}; 从“expo Camera”导入{Camera}; 从“世博媒体库”导入*作为媒体库; 从“react native”导入{Dimensions}; //新进口 从“expo图像操纵器”导入*作为图像操纵器; const{height,width}=Dimensions.get'window'; 导出默认功能应用程序{ //和以前一样的代码 回来 { setCameraRef; }}> { ifcameraRef{ let photo=wait cameraRef.takePictureAsync{skipping processing:true}; //这里有新代码 const manippresult=wait ImageManipulator.manipleAsync photo.uri, [{rotate:90},{flip:ImageManipulator.FlipType.Vertical}], {压缩:1,格式:ImageManipulator.SaveFormat.PNG} ; MediaLibrary.SaveToLibrary AsynchManipResult.uri; } }}> ; }

import { ImageManipulator } from 'expo';

const manipResult = await ImageManipulator.manipulate(
    imageUri,
    [{ resize: { width: 640, height: 480 } }],
    { format: 'jpg' }
);