日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何在ReactNative中實現(xiàn)類Instagram濾鏡效果?

譯者 | 崔皓

專業(yè)領(lǐng)域包括成都網(wǎng)站設(shè)計、成都做網(wǎng)站、商城網(wǎng)站定制開發(fā)、微信營銷、系統(tǒng)平臺開發(fā), 與其他網(wǎng)站設(shè)計及系統(tǒng)開發(fā)公司不同,創(chuàng)新互聯(lián)建站的整合解決方案結(jié)合了幫做網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,為客戶提供全網(wǎng)互聯(lián)網(wǎng)整合方案。

?審校 | 孫淑娟

開篇

本文通過分步驟的指南,說明如何在React Native中整合圖片編輯,并實現(xiàn)類Instagram濾鏡的效果。

在Instagram上,你可以很容易地使用濾鏡功能,并迅速得到想要的結(jié)果。濾鏡功能很好地修改照片,讓人們得到想要的效果。一些用戶想要在自定義的React Native中實現(xiàn)類似的濾鏡效果。也就是說將Instagram的濾鏡應(yīng)用到React Native 程序中。

為了滿足這個要求,本文編寫了一個分步驟的指南,說明如何在React Native中整合圖片編輯,實現(xiàn)類似Instagram濾鏡的功能。

我們的開發(fā)人員在對React Native中的各種過濾器庫進行廣泛研究之后,并沒有找到理想的實現(xiàn)效果。于是,他們想出了在React Native中構(gòu)建圖像濾鏡的特別指南。

就讓我們手捧指南,從這里出發(fā)吧!

前提條件

沒有特別的要求,只要確保React Native已經(jīng)安裝,并保證項目已經(jīng)創(chuàng)建。

雖然,安裝React Native和設(shè)置并非易事,由于本文主題在如何進行圖片編輯,因此不展開說React Native和設(shè)置,如果有需要可以訪問??React Native官網(wǎng)??,獲取更多信息。

安裝相關(guān)庫

在應(yīng)用程序中,需要三個主要功能;裁剪、過濾和下載。為了實現(xiàn)這些功能,我們的開發(fā)人員已經(jīng)選擇了三個最好的庫來支持React Native中的濾鏡功能。

1.圖像裁剪

圖像裁剪允許按照尺寸自由調(diào)整裁剪圖像。它是移動應(yīng)用開發(fā)不可獲缺的重要組成部分。我們可以通過??Crop Picker Library??來獲得裁剪圖片的功能。該庫還提供了視頻編輯功能。

2.圖像過濾器

我們使用 ??React Native Image Filter Kit?? 來處理應(yīng)用程序中的圖像過濾?;谠摴ぞ甙?,我們創(chuàng)建了一個特殊的代碼來生成20多個過濾器。

3.圖片下載

為了分享修改后的圖片,人們需要將其下載到手機上。這個功能可以從 ??React Native Cameraroll Library?? 庫中獲得,該庫可以幫助開發(fā)者將過濾后的圖片保存在iOS和Android的照片庫中。

核心功能開發(fā)指南

一旦上述庫安裝好之后,就可以開始核心功能的開發(fā)了。接下來,讓我們進入編碼部分,實現(xiàn)既定的里程碑。

如圖 1 所示,這里列出了文件夾結(jié)構(gòu),它可以幫助我們理解文件之間的關(guān)系以及需要實現(xiàn)的功能。

圖1:代碼文件結(jié)構(gòu)

第1步:調(diào)用手機相冊

創(chuàng)建文件夾名為 "ChooseImage"。接下來,添加 "index.jsx "文件。為了從手機圖庫中獲取圖片,在'index.jsx'文件中添加以下代碼。

import React, { useState } from 'react';

import {

Image,

Alert,

SafeAreaView,

StyleSheet,

Text,

TouchableOpacity,

View,

} from 'react-native';

import { launchImageLibrary } from 'react-native-image-picker';

import {

widthPercentageToDP as wp,

heightPercentageToDP as hp,

} from 'react-native-responsive-screen';

import Constants from '../../Constants/Constants';

import Button from '../../Components/Button';

import Loader from '../../Components/Loader';

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



const CreatePost = ({ navigation }) => {



const [thumbnail, setThumbnail] = useState({});

const [loaderVisible, setLoaderVisible] = useState(false);



const onChooseImage = async (selectionType) => {





const options = {

cameraType: 'back',

mediaType: selectionType,

includeBase64: true,

};

const result = await launchImageLibrary(options);

if (!result.didCancel && result.assets) {

if (selectionType === 'photo') {

const photoData = {

uri: result.assets[0].uri,

type: result.assets[0].type,

name: result.assets[0].fileName,

};

setThumbnail(photoData);

}

}

if (result.errorMessage) console.log('error');

};



const handleNextStepClick = async () => {

if (!thumbnail.length) {

setLoaderVisible(false);

if (!Object.keys(thumbnail).length) {

Alert.alert('Please add thumbnail image');

return;

} else {

return ImagePicker.openCropper({

includeBase64: true,

path: thumbnail,

cropping: false,

freeStyleCropEnabled: true,

compressImageQuality: 0.8,

showCropFrame: true,

mediaType: 'photo',

}).then(image => {

navigation.navigate('FilterScreen', { imageData: image });

})

}

}

};



return (


style={styles.safeView}>


style={styles.imageView}>

{Object.keys(thumbnail).length ? (

<>


style={styles.insideView}>


source={{ uri: thumbnail?.uri }}

style={styles.thumbImage}

resizeMode={'contain'}

/>




style={styles.editView}>


activeOpacity={0.6}

onPress={() => onChooseImage('photo')}

style={{

...styles.addLessonBtnContainer,

marginEnd: 7,

}}>


source={require('../../Assests/icon_edit.png')}

resizeMode="contain"

style={styles.editImage}

/>




activeOpacity={0.6}

onPress={() => setThumbnail({})}

style={styles.addLessonBtnContainer}>


source={require('../../Assests/delete.png')}

resizeMode="contain"

style={styles.editImage}

/>







) : (

<>




onPress={() => onChooseImage('photo')}

activeOpacity={0.7}>




source={require('../../Assests/Pick.png')}

style={styles.galleryImg}

resizeMode="contain"

/>








style={styles.postTextView}>



{Constants.create_post_story}







)}






title={Constants.next}

onclick={handleNextStepClick}

style={styles.button_next}

/>







);

};



const styles = StyleSheet.create({

button_next: {

textTransform: 'uppercase',

fontSize: wp('5%'),

color: 'white',

marginHorizontal: wp('7%')

},

editView: {

justifyContent: 'flex-end',

alignItems: 'center',

marginTop: wp('5%'),

alignSelf: 'flex-end',

display: 'flex',

flexDirection: 'row',

},

imageView: {

paddingHorizontal: wp('5%'),

paddingVertical: wp('10%'),

backgroundColor: '#FFFFFF',

marginTop: wp('5%'),

width: wp('100%'),

},

insideView: {

width: '100%',

justifyContent: 'center',

alignItems: 'center',

},

thumbImage: {

width: wp('100%'),

height: wp('80%'),

},

editImage: {

width: wp('4.5%'),

height: wp('4.5%'),

tintColor: '#FFFFFF',

},

galleryView: {

height: wp('20%'),

width: wp('20%'),

backgroundColor: '#FF701F',

borderRadius: 40,

justifyContent: 'center',

alignItems: 'center'

},

galleryImg: {

height: wp('7%'),

width: wp('7%'),

tintColor: 'white'

},

postTextView: {

marginTop: wp('5%'),

},

safeView: {

flex: 1,

backgroundColor: '#fff',

},

buttonView: {

marginTop: wp('7%'),

marginBottom: wp('3%')

},

pickContainer: {

borderWidth: 1,

borderColor: '#DFDFDF',

marginTop: hp('10%'),

justifyContent: 'center',

alignItems: 'center',

borderStyle: 'dashed',

width: '100%',

paddingVertical: wp('7%'),

},

addLessonBtnContainer: {

backgroundColor: '#FF701F',

borderRadius: 4,

paddingHorizontal: wp('3%'),

paddingVertical: wp('2%'),

},

introText: {

textTransform: 'uppercase',

textAlign: 'center',

textAlignVertical: 'center',

color: '#1F1F1F',

fontSize: wp('5%'),

},

});



export default CreatePost;

輸出

一旦完成上述代碼,并將其添加到之后,你就可以看到如圖2所示內(nèi)容。

圖2:調(diào)用手機相冊

第2步:圖像裁剪和調(diào)整大小

在第一步中,我們已經(jīng)在NEXT "按鈕上添加了一段代碼。因此,當(dāng)你點擊“NEXT”對照片進行裁剪時,就會打開對應(yīng)的用戶界面。用戶可以調(diào)整圖片的大小,也可以旋轉(zhuǎn)它,總之可以對其進行編輯。

選擇器提供各種圖像比例供用戶選取。一旦用戶點擊了選項按鈕,就會出現(xiàn)一個動作表視圖?,F(xiàn)在,用戶可以選擇一個預(yù)定義的比例對照片進行裁剪了。

第3步:創(chuàng)建圖像過濾器

現(xiàn)在,選擇和裁剪功能已經(jīng)準(zhǔn)備好了。接下來,是時候添加圖像過濾功能了。現(xiàn)在讓我們創(chuàng)建一個新的文件夾,并命名為Filter Image"。再次,在新文件夾下面創(chuàng)建一個文件

import React, { useRef, useState, useEffect } from 'react';

import {

widthPercentageToDP as wp,

heightPercentageToDP as hp,

} from 'react-native-responsive-screen';

import {

FlatList,

Image,

SafeAreaView,

StyleSheet,

Text,

TouchableOpacity,

ImageBackground,

View,

} from 'react-native';

import { FILTERS } from '../../Helpers/Filters';

import Button from '../../Components/Button';

import Constants from '../../Constants/Constants';

import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';



const FilterScreen = ({ navigation, route }) => {



const [selectedFilterIndex, setIndex] = useState(0);

const [image, SetImage] = useState('')

const [thumbnail, setThumbnail] = useState({});



useEffect(() => {

getImageFromNavigation()

})



const getImageFromNavigation = () => {

if (route?.params?.imageData) {

setThumbnail(route?.params?.imageData)

}

}



const onExtractImage = ({ nativeEvent }) => {

SetImage(nativeEvent.uri)

extractedUri.current = nativeEvent.uri;

};



const onSelectFilter = selectedIndex => {

setIndex(selectedIndex);

};



const extractedUri = useRef(thumbnail?.path);



const handleNextStepClick = async () => {

if (selectedFilterIndex === 0) {

navigation.navigate('ViewImage', { imageString: thumbnail })

} else {

console.log('goinfFromHere');

navigation.navigate('ViewImage', { imageString: image })

}

};





const renderFilterComponent = ({ item, index }) => {

const FilterComponent = item.filterComponent;

const image = (


style={styles.filterSelector}

source={{ uri: thumbnail?.path }}

defaultSource={require('../../Assests/Pick.png')}

/>

);

return (

onSelectFilter(index)}>

{item.title}





);

};



const SelectedFilterComponent = FILTERS[selectedFilterIndex].filterComponent;



return (

<>


style={styles.safeView}>


source={require('../../Assests/image_background.png')}

style={styles.container}>




contentContainerStyle={styles.keyboardContainer}

resetScrollToCoords={{ x: 0, y: 0 }}>



{selectedFilterIndex === 0 ? (


style={styles.default_Img}

source={{ uri: thumbnail?.path }}

resizeMode='contain'

/>

) : Object.keys(thumbnail).length && (


onExtractImage={onExtractImage}

extractImageEnabled={true}

image={


style={styles.default_Img}

source={{ uri: thumbnail?.path }}

resizeMode='contain'

/>

}

/>

)}


data={FILTERS}

keyExtractor={item => item.title}

showsHorizontalScrollIndicator={false}

horizontal={true}

renderItem={renderFilterComponent}

/>




title={Constants.next}

onclick={handleNextStepClick}

style={{ textTransform: 'uppercase' }}

/>











);

};

const styles = StyleSheet.create({

default_Img: {

flex: 1,

width: wp('100%'),

height: hp('50%'),

alignSelf: 'center',

alignContent: 'center'

},

keyboardContainer: {

width: wp('90%'),

},

buttonView: {

marginTop: wp('7%'),

marginBottom: wp('3%')

},

safeView: {

flex: 1,

backgroundColor: '#fff',

},

filterSelector: {

width: 100,

height: 100,

margin: 5,

},

filterTitle: {

marginTop: 70,

fontSize: 12,

textAlign: 'center',

},

container: {

flex: 1,

justifyContent: 'center',

backgroundColor: '#FFFFFF',

alignItems: 'center',

},

});

export default FilterScreen;

輸出

完成上述代碼之后,如圖3所示,應(yīng)用程序的所有過濾器都是可見的了,用戶可以選擇任意一個濾鏡對圖片進行處理了。

圖 3:圖片過濾器應(yīng)用

第4步:圖像保存和下載

完成上述功能之后,接著就需要編寫保存/下載圖片的功能,否則應(yīng)用是玩不轉(zhuǎn)的。由于我們在上一步過濾器的基礎(chǔ)上添加下載功能的代碼。

和前面兩個步驟一樣,創(chuàng)建一個名為自定義圖片。

import React, { useState, useEffect } from 'react';

import {

View,

StyleSheet,

Image,

Platform,

PermissionsAndroid

} from 'react-native';

import { CameraRoll } from "@react-native-camera-roll/camera-roll";

import {

widthPercentageToDP as wp,

heightPercentageToDP as hp,

} from 'react-native-responsive-screen';

import Constants from '../../Constants/Constants';

import Button from '../../Components/Button';



const ViewImage = ({ route }) => {

const [thumbnail, setThumbnail] = useState({});

const [photos, setPhotos] = useState('');



useEffect(() => {

getImageFromNavigation()

})



const getImageFromNavigation = () => {

if (route?.params?.imageString) {

console.log('params-->', route?.params?.imageString);

setThumbnail(route?.params?.imageString)

setPhotos(route?.params?.imageString)

}

}

async function hasAndroidPermission() {

const permission = PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE;



const hasPermission = await PermissionsAndroid.check(permission);

if (hasPermission) {

return true;

}



const status = await PermissionsAndroid.request(permission);

return status === 'granted';

}



async function savePicture() {

if (Platform.OS === "android" && !(await hasAndroidPermission())) {

return;

}

CameraRoll.save(photos, { type: 'photo' })

};

return (




source={{ uri: photos !== '' ? photos : thumbnail?.path }}

style={styles.imgView}

/>




title={Constants.image_download}

onclick={savePicture}

style={{ textTransform: 'uppercase' }}

/>





);

};



const styles = StyleSheet.create({

imgView: {

width: wp('100%'),

height: hp('30%'),

resizeMode: 'contain'

},

container: {

flex: 1,

alignItems: 'center',

backgroundColor: '#F5FCFF',

paddingTop: 30,

marginVertical: hp('25%')

},

buttonView: {

marginTop: wp('7%'),

marginBottom: wp('3%'),

width: wp('80%'),

},

});



export default ViewImage;

輸出結(jié)果如圖4 所示。

圖4:圖片下載

總結(jié)

根據(jù)上述指南,你已經(jīng)完成了代碼的編寫。這里對整個指南稍做總結(jié),在保證React Native安裝和配置的前提下,分別安裝圖像裁剪、圖像過濾和圖片下載的相關(guān)庫。然后,根據(jù)四步實現(xiàn)濾鏡功能的開發(fā),包括:調(diào)用手機相冊、圖像裁剪和調(diào)整圖片大小、創(chuàng)建圖像過濾器以及保存和下載圖像。

譯者介紹

崔皓,社區(qū)編輯,資深架構(gòu)師,擁有18年的軟件開發(fā)和架構(gòu)經(jīng)驗,10年分布式架構(gòu)經(jīng)驗。

原文標(biāo)題:??A Guide to Implement Instagram-Like Filters in React Native??,作者:Kiran Beladiya


文章標(biāo)題:如何在ReactNative中實現(xiàn)類Instagram濾鏡效果?
標(biāo)題URL:http://www.5511xx.com/article/ccdpsji.html