194 lines
5.6 KiB
JavaScript
Raw Normal View History

2016-05-18 12:07:36 +02:00
import React, {Component} from 'react';
2016-09-11 16:31:51 +02:00
import {
View, Text, StyleSheet, ScrollView,
Image, TouchableOpacity, NativeModules, Dimensions
} from 'react-native';
import Video from 'react-native-video';
2016-05-18 12:07:36 +02:00
var ImagePicker = NativeModules.ImageCropPicker;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
button: {
backgroundColor: 'blue',
marginBottom: 10
},
text: {
color: 'white',
2016-08-11 23:45:33 +02:00
fontSize: 20,
textAlign: 'center'
2016-05-18 12:07:36 +02:00
}
});
export default class App extends Component {
constructor() {
super();
this.state = {
image: null,
images: null
};
}
2016-08-11 20:00:59 +02:00
pickSingleWithCamera(cropping) {
ImagePicker.openCamera({
cropping: true,
// cropperCircleOverlay: true,
2016-08-11 20:00:59 +02:00
width: 500,
height: 500
}).then(image => {
console.log('received image', image);
this.setState({
image: {uri: image.path, width: image.width, height: image.height},
images: null
});
}).catch(e => alert(e));
}
pickSingleBase64(cropit) {
ImagePicker.openPicker({
width: 300,
height: 300,
cropping: cropit,
includeBase64: true
}).then(image => {
console.log('received image', image);
this.setState({
image: {uri: `data:${image.mime};base64,`+ image.data, width: image.width, height: image.height},
images: null
});
2016-09-11 16:31:51 +02:00
}).catch(e => alert(e));
}
2016-09-07 21:41:31 +02:00
cleanupImages() {
ImagePicker.clean().then(() => {
console.log('removed tmp images from tmp directory');
}).catch(e => {
alert(e);
});
}
cleanupSingleImage() {
let image = this.state.image || (this.state.images && this.state.images.length ? this.state.images[0] : null);
console.log('will cleanup image', image);
ImagePicker.cleanSingle(image ? image.uri : null).then(() => {
console.log(`removed tmp image ${image.uri} from tmp directory`);
}).catch(e => {
alert(e);
})
}
pickSingle(cropit, circular=false) {
2016-05-18 12:07:36 +02:00
ImagePicker.openPicker({
width: 300,
height: 300,
2016-09-11 16:31:51 +02:00
cropping: cropit,
cropperCircleOverlay: circular,
2016-09-11 16:31:51 +02:00
compressVideo: true
2016-05-18 12:07:36 +02:00
}).then(image => {
console.log('received image', image);
2016-05-18 12:07:36 +02:00
this.setState({
2016-09-11 16:31:51 +02:00
image: {uri: image.path, width: image.width, height: image.height, mime: image.mime},
2016-05-18 12:07:36 +02:00
images: null
});
}).catch(e => {
console.log(e.code);
alert(e);
});
2016-05-18 12:07:36 +02:00
}
pickMultiple() {
ImagePicker.openPicker({
multiple: true
}).then(images => {
this.setState({
image: null,
images: images.map(i => {
console.log('received image', i);
2016-09-11 16:31:51 +02:00
return {uri: i.path, width: i.width, height: i.height, mime: i.mime};
2016-05-18 12:07:36 +02:00
})
});
2016-09-11 16:31:51 +02:00
}).catch(e => alert(e));
2016-05-18 12:07:36 +02:00
}
scaledHeight(oldW, oldH, newW) {
return (oldH / oldW) * newW;
}
2016-09-11 16:31:51 +02:00
renderVideo(uri) {
return <View style={{height: 300, width: 300}}>
<Video source={{uri}}
style={{position: 'absolute',
top: 0,
left: 0,
bottom: 0,
right: 0
}}
rate={1}
paused={false}
volume={1}
muted={false}
resizeMode={'cover'}
onLoad={load => console.log(load)}
onProgress={() => {}}
onEnd={() => { console.log('Done!'); }}
repeat={true} />
</View>;
}
renderImage(image) {
return <Image style={{width: 300, height: 300, resizeMode: 'contain'}} source={image} />
}
renderAsset(image) {
if (image.mime && image.mime.toLowerCase().indexOf('video/') !== -1) {
return this.renderVideo(image.uri);
}
return this.renderImage(image);
}
2016-05-18 12:07:36 +02:00
render() {
return <View style={styles.container}>
<ScrollView>
2016-09-11 16:31:51 +02:00
{this.state.image ? this.renderAsset(this.state.image) : null}
{this.state.images ? this.state.images.map(i => <View key={i.uri}>{this.renderAsset(i)}</View>) : null}
2016-05-18 12:07:36 +02:00
</ScrollView>
2016-08-11 20:00:59 +02:00
<TouchableOpacity onPress={() => this.pickSingleWithCamera(false)} style={styles.button}>
<Text style={styles.text}>Select Single With Camera</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => this.pickSingleWithCamera(true)} style={styles.button}>
<Text style={styles.text}>Select Single With Camera With Cropping</Text>
</TouchableOpacity>
2016-05-18 12:07:36 +02:00
<TouchableOpacity onPress={() => this.pickSingle(false)} style={styles.button}>
<Text style={styles.text}>Select Single</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => this.pickSingleBase64(false)} style={styles.button}>
<Text style={styles.text}>Select Single Returning Base64</Text>
</TouchableOpacity>
2016-05-18 12:07:36 +02:00
<TouchableOpacity onPress={() => this.pickSingle(true)} style={styles.button}>
<Text style={styles.text}>Select Single With Cropping</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => this.pickSingle(true, true)} style={styles.button}>
<Text style={styles.text}>Select Single With Circular Cropping</Text>
</TouchableOpacity>
2016-05-18 12:07:36 +02:00
<TouchableOpacity onPress={this.pickMultiple.bind(this)} style={styles.button}>
<Text style={styles.text}>Select Multiple</Text>
</TouchableOpacity>
2016-09-07 21:41:31 +02:00
<TouchableOpacity onPress={this.cleanupImages.bind(this)} style={styles.button}>
<Text style={styles.text}>Cleanup All Images</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.cleanupSingleImage.bind(this)} style={styles.button}>
<Text style={styles.text}>Cleanup Single Image</Text>
</TouchableOpacity>
2016-05-18 12:07:36 +02:00
</View>;
}
}