Skip to content

Commit

Permalink
🎨 added camera
Browse files Browse the repository at this point in the history
  • Loading branch information
priyansh71 committed Aug 16, 2022
1 parent a3b01bd commit b57715b
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 3 deletions.
11 changes: 9 additions & 2 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
SystemProgram,
Transaction,
} from "@solana/web3.js";
import { CameraDetailScreen } from "./components/Camera";

const NETWORK = clusterApiUrl("mainnet-beta");

Expand Down Expand Up @@ -59,6 +60,7 @@ export default function App() {
const connection = new Connection(NETWORK);
const addLog = useCallback((log: string) => setLogs((logs) => [...logs, "> " + log]), []);
const scrollViewRef = useRef<any>(null);
const [showCamera, setShowCamera] = useState(false);

// store dappKeyPair, sharedSecret, session and account SECURELY on device
// to avoid having to reconnect users.
Expand Down Expand Up @@ -340,14 +342,19 @@ export default function App() {
))}
</ScrollView>
</View>
<View style={{ flex: 0, paddingTop: 20, paddingBottom: 40 }}>
{showCamera ?

<View style={{ flex: 0, paddingTop: 20, paddingBottom: 40 }}>
<Btn title="Connect" onPress={connect} />
<Btn title="Disconnect" onPress={disconnect} />
<Btn title="Sign And Send Transaction" onPress={signAndSendTransaction} />
<Btn title="Sign All Transactions" onPress={signAllTransactions} />
<Btn title="Sign Transaction" onPress={signTransaction} />
<Btn title="Sign Message" onPress={signMessage} />
</View>
:
<CameraDetailScreen />
}
</View>
);
}
Expand All @@ -358,4 +365,4 @@ const Btn = ({ title, onPress }: { title: string; onPress: () => Promise<void> }
<Button title={title} onPress={onPress} />
</View>
);
};
};
10 changes: 10 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
},
"assetBundlePatterns": [
"**/*"
],
"plugins": [
[
"expo-media-library",
{
"photosPermission": "Allow $(PRODUCT_NAME) to access your photos.",
"savePhotosPermission": "Allow $(PRODUCT_NAME) to save photos.",
"isAccessMediaLocationEnabled": true
}
]
]
}
}
129 changes: 129 additions & 0 deletions components/Camera.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { Camera } from 'expo-camera';
import React , { useRef, useState } from 'react';
import * as MediaLibrary from 'expo-media-library';
import { StyleSheet, TouchableOpacity, View, Image, Button, Text } from 'react-native';


export function CameraDetailScreen() {
const [type, setType] = useState("back");
const [permission, requestCameraPermission] = Camera.useCameraPermissions();
const [status, requestMediaPermission] = MediaLibrary.usePermissions();
const [clicked, setClicked] = useState(false);
const [image, setImage] = useState("https://avatars.githubusercontent.com/u/109650484?s=400&u=953955669a2c724d06e73e976f07c83413400443&v=4",);
const ref = useRef(null)


const toggleCameraType = () => {
setType((current) => (
current === "back" ? "front" : "back"
));
}

const _takePhoto = async () => {
const {uri} = await ref.current.takePictureAsync();
await MediaLibrary.saveToLibraryAsync(uri);
setImage(uri);
setClicked(true);
}


if (!permission || !status) return <View />;

if (!permission.granted || !status.granted) {
return (
<View style={styles.container}>
<Button title="Camera Access" onPress={requestCameraPermission} />
<Button title="Gallery Access" onPress={requestMediaPermission} />
</View>
);
}

return (
<View style={styles.container}>

{
clicked ? (
<View style={styles.view} >
<Image source={{
uri: image,
width: 330,
height: 500,
}} />
</View>
) :
(
<Camera style={styles.camera} type={type} ref={ref} autoFocus ratio="16:9">
<View style={styles.buttonContainer}>
<TouchableOpacity style={styles.repeat} onPress={toggleCameraType} >
<Text>Toggle</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.cameraIcon}
onPress={_takePhoto}
>
<Text> Click</Text>
</TouchableOpacity>
</View>
</Camera>
)

}


</View>
);
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
flexDirection: 'column',
alignItems: 'center',
},
view : {
justifyContent: 'center',
flexDirection: 'column',
alignItems: 'center',
},
icons : {
flexDirection: 'column',
justifyContent: 'space-between',
alignItems: 'stretch',
},
camera: {
width: '100%',
height: '95%'
},
buttonContainer: {
flex: 1,
flexDirection: 'row',
backgroundColor: 'transparent',
margin: 32,
},
repeat: {
position: 'absolute',
bottom: 0,
right: 0,
},
cameraIcon: {
position: 'absolute',
bottom: 0,
left: 0,
},
text: {
fontSize: 24,
fontWeight: 'bold',
color: 'white',
},
});



// ImagePicker.openPicker({
// width: 300,
// height: 400,
// cropping: true
// }).then(image => {
// console.log(image);
// })
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"@solana/web3.js": "~1.35.0",
"buffer": "^6.0.3",
"expo": "~44.0.0",
"expo-camera": "~12.1.2",
"expo-linking": "~3.0.0",
"expo-media-library": "~14.0.0",
"expo-status-bar": "~1.2.0",
"expo-updates": "~0.11.6",
"react": "17.0.1",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "expo/tsconfig.base",
"compilerOptions": {
"strict": true
"strict": false
}
}
28 changes: 28 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,13 @@
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"

"@koale/useworker@^4.0.2":
version "4.0.2"
resolved "https://registry.yarnpkg.com/@koale/useworker/-/useworker-4.0.2.tgz#cb540a2581cd6025307c3ca6685bc60748773e58"
integrity sha512-xPIPADtom8/3/4FLNj7MvNcBM/Z2FleH85Fdx2O869eoKW8+PoEgtSVvoxWjCWMA46Sm9A5/R1TyzNGc+yM0wg==
dependencies:
dequal "^1.0.0"

"@nodelib/fs.scandir@2.1.5":
version "2.1.5"
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
Expand Down Expand Up @@ -2530,6 +2537,11 @@ depd@~1.1.2:
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
integrity sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=

dequal@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/dequal/-/dequal-1.0.1.tgz#dbbf9795ec626e9da8bd68782f4add1d23700d8b"
integrity sha512-Fx8jxibzkJX2aJgyfSdLhr9tlRoTnHKrRJuu2XHlAgKioN2j19/Bcbe0d4mFXYZ3+wpE2KVobUVTfDutcD17xQ==

destroy@~1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80"
Expand Down Expand Up @@ -2708,6 +2720,15 @@ expo-asset@~8.4.6:
path-browserify "^1.0.0"
url-parse "^1.4.4"

expo-camera@~12.1.2:
version "12.1.2"
resolved "https://registry.yarnpkg.com/expo-camera/-/expo-camera-12.1.2.tgz#b549a8054ce5778fbf37a45b5d41003f36f5bb3c"
integrity sha512-Rr0Evj1V3LGiZE5/YBuuVimuU9uSTwVoqtSBsZ8QoK11+g9vnu2NgBjFMb938yjWD5tqJiXRH8lVTM2mvzSmIQ==
dependencies:
"@expo/config-plugins" "^4.0.2"
"@koale/useworker" "^4.0.2"
invariant "^2.2.4"

expo-constants@~13.0.0, expo-constants@~13.0.2:
version "13.0.2"
resolved "https://registry.yarnpkg.com/expo-constants/-/expo-constants-13.0.2.tgz#b489ecd575cc82a9a0b3dfbf2385d45a44300eb1"
Expand Down Expand Up @@ -2763,6 +2784,13 @@ expo-manifests@~0.2.2:
dependencies:
expo-json-utils "~0.2.0"

expo-media-library@~14.0.0:
version "14.0.1"
resolved "https://registry.yarnpkg.com/expo-media-library/-/expo-media-library-14.0.1.tgz#b1a2ce3699edf6b5f561d32812e608ba064ba43f"
integrity sha512-a7NGFxZvns81y0fRZJsBROXFWKtyEMaodw+x32IPNhKLUAn+X1ggfBS8FbNpUs3bA4ZvhjFQBn59dxPfcHHxEg==
dependencies:
"@expo/config-plugins" "^4.0.2"

expo-modules-autolinking@0.5.5:
version "0.5.5"
resolved "https://registry.yarnpkg.com/expo-modules-autolinking/-/expo-modules-autolinking-0.5.5.tgz#6bcc42072dcbdfca79d207b7f549f1fdb54a2b74"
Expand Down

0 comments on commit b57715b

Please sign in to comment.