import {
Button,
StyleSheet,
Text,
View,
Image,
TextInput,
} from
"react-native"
;
import * as ImagePicker from
"expo-image-picker"
;
import * as FileSystem from
"expo-file-system"
;
import { useRef, useState } from
"react"
;
export
default
function
App() {
const [fileUri, setFileUri] = useState(
""
);
const [fileType, setFileType] = useState(
""
);
const [heightAspect, setHeightAspect] = useState(
"3"
);
const [widthAspect, setWidthAspect] = useState(
"4"
);
const handlePickFile = async () => {
if
(heightAspect ==
"0"
|| widthAspect ==
"0"
) {
const res =
await ImagePicker.launchImageLibraryAsync({
mediaTypes:
ImagePicker.MediaTypeOptions.Images,
quality: 1,
allowsEditing:
true
,
allowsMultipleSelection:
false
,
});
if
(res.canceled)
return
;
setFileUri(res.assets[0].uri);
setFileType(res.assets[0].type);
}
else
{
const res =
await ImagePicker.launchImageLibraryAsync({
mediaTypes:
ImagePicker.MediaTypeOptions.Images,
quality: 1,
aspect: [
parseInt(widthAspect),
parseInt(heightAspect),
],
allowsEditing:
true
,
allowsMultipleSelection:
false
,
});
if
(res.canceled)
return
;
setFileUri(res.assets[0].uri);
setFileType(res.assets[0].type);
}
};
const saveFile = async (uri, mimetype) => {
let fileName = Date.now() +
".jpg"
;
const permissions =
await
FileSystem.StorageAccessFramework
.requestDirectoryPermissionsAsync();
if
(permissions.granted) {
const base64 =
await FileSystem.readAsStringAsync(uri, {
encoding:
FileSystem.EncodingType.Base64,
});
await FileSystem.StorageAccessFramework.createFileAsync(
permissions.directoryUri,
fileName,
mimetype
)
.then(async (uri) => {
await FileSystem.writeAsStringAsync(
uri,
base64,
{
encoding:
FileSystem.EncodingType
.Base64,
}
);
})
.
catch
((e) => console.log(e));
}
else
{
alert(
"Permission not granted"
);
}
};
return
(
<View style={styles.container}>
<Text style={styles.heading1}>
Image Crop GeeksforGeeks
</Text>
{fileUri.length != 0 ? (
<Image
source={{ uri: fileUri }}
style={{
width: 400,
height: 400,
objectFit:
"contain"
,
}}
/>
) : (
<View></View>
)}
<View
style={{
flexDirection:
"column"
,
alignItems:
"center"
,
}}>
<Text
style={{ fontSize: 24, color:
"red"
}}>
Aspect ratio
</Text>
<Text>
Set values 0
for
any
for
free aspect
ratio. Default is 4:3
</Text>
<View
style={{
display:
"flex"
,
flexDirection:
"row"
,
alignContent:
"center"
,
}}>
<Text style={styles.inputLabel}>
Width:{
" "
}
</Text>
<TextInput
onChangeText={setWidthAspect}
value={widthAspect}
inputMode=
"numeric"
keyboardType=
"numeric"
style={styles.input}
maxLength={2}/>
</View>
<View
style={{
display:
"flex"
,
flexDirection:
"row"
,
alignContent:
"center"
,
}}>
<Text style={styles.inputLabel}>
Height:{
" "
}
</Text>
<TextInput
onChangeText={setHeightAspect}
value={heightAspect}
inputMode=
"numeric"
keyboardType=
"numeric"
style={styles.input}
maxLength={2}/>
</View>
</View>
<View
style={{
display:
"flex"
,
flexDirection:
"row"
,
justifyContent:
"space-evenly"
,
width:
"100%"
,
padding: 10,
}}>
<Button
title=
"Pick Image"
onPress={handlePickFile}/>
{fileUri.length != 0 ? (
<>
<Button
title=
"Save Image"
onPress={() =>
saveFile(fileUri, fileType)
}
/>
<Button
title=
"reset"
onPress={() => setFileUri(
""
)}
/>
</>
) : (
<></>
)}
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor:
"#fff"
,
alignItems:
"center"
,
justifyContent:
"space-evenly"
,
height:
"100%"
,
},
heading1: {
fontSize: 28,
fontWeight:
"bold"
,
color:
"green"
,
textAlign:
"center"
,
},
input: {
width: 50,
height: 30,
borderColor:
"gray"
,
borderWidth: 1,
textAlign:
"center"
,
borderRadius: 5,
padding: 5,
margin: 5,
},
inputLabel: {
fontSize: 20,
margin: 5,
padding: 5,
},
});