Skip to content

Instantly share code, notes, and snippets.

View RedRussianBear's full-sized avatar

Mikhail Khrenov RedRussianBear

View GitHub Profile
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
// Retrieve object to check for from initiating intent
object = getIntent().getStringExtra(MainActivity.EXTRA_MESSAGE);
info = findViewById(R.id.infoText);
}
@Override
public void onResume() {
super.onResume();
// If we've taken a photo, send it off to Clarifai to check
if (photoPath != null) {
new ClarifaiTask().execute(new File(photoPath));
}
}
@Override
protected void onStart() {
super.onStart();
// Retrieve default ringtone file URI
Uri myUri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE);
// Set up MediaPlayer asynchronously
mediaPlayer = new MediaPlayer();
class Listener implements MediaPlayer.OnPreparedListener {
@Override
protected void onStop() {
super.onStop();
// Release MediaPlayer
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
public void takePicture(View view) {
// Create intent to open camera app
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Proceed only if there is a camera app
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Attempt to allocate a file to store the photo
File photoFile;
try {
File storageDir = getFilesDir();
// Use Clarifai to find the image color scheme
function getColors(file) {
app.models.predict(Clarifai.COLOR_MODEL, {base64: file}).then(
// Response handler
function (response) {
// Get colors from response and sort by descending prevalence
const colors = response.outputs[0].data.colors;
colors.sort((a, b) => (a.value > b.value) ? -1 : ((b.value > a.value) ? 1 : 0));
const holder = $('#colors');
<!-- Include JS !-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinycolor/1.4.1/tinycolor.min.js"></script>
<script src="https://sdk.clarifai.com/js/clarifai-latest.js"></script>
<script src="colors.js"></script>
<!-- Include Styles !-->
<link rel="stylesheet" href="style.css">
$(document).ready(function () {
// Ensure body resizes appropriately
$('body').css('height', $(window).height());
$(window).resize(function () {$('body').css('height', $(window).height());});
// Connect to Clarifai
const app = new Clarifai.App({
apiKey: 'YOUR_API_KEY'
});
* {
box-sizing: border-box;
}
body {
background-size: cover;
background-repeat: no-repeat;
font-family: "Open Sans", sans-serif;
font-size: 14pt;
// Image upload handler
$('#upload').change(function () {
const file = this.files[0];
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
$('body').css('background-image', 'url("' + reader.result + '")');
$('#colors').html('<h2>Processing...</h2>');