Created
May 27, 2015 05:27
-
-
Save yaronv/cbb36eaa7fe8b4759648 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package yv.recipe; | |
import android.app.AlertDialog; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.database.Cursor; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.provider.MediaStore; | |
import android.support.v4.view.ViewPager; | |
import android.support.v7.app.ActionBarActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.ImageButton; | |
import android.widget.ImageView; | |
import org.opencv.android.BaseLoaderCallback; | |
import org.opencv.android.LoaderCallbackInterface; | |
import org.opencv.android.OpenCVLoader; | |
import java.io.File; | |
import yv.recipe.adapters.ViewPagerAdapter; | |
import yv.recipe.layouts.SlidingTabLayout; | |
public class MainActivity extends ActionBarActivity { | |
// Declaring Your View and Variables | |
Toolbar toolbar; | |
ViewPager pager; | |
ViewPagerAdapter adapter; | |
SlidingTabLayout tabs; | |
ImageButton FAB; | |
CharSequence Titles[] = {"Recipes", "Popular"}; | |
int Numboftabs = 2; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
toolbar = (Toolbar) findViewById(R.id.tool_bar); | |
setSupportActionBar(toolbar); | |
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs); | |
pager = (ViewPager) findViewById(R.id.pager); | |
pager.setAdapter(adapter); | |
tabs = (SlidingTabLayout) findViewById(R.id.tabs); | |
tabs.setDistributeEvenly(true); | |
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { | |
@Override | |
public int getIndicatorColor(int position) { | |
return getResources().getColor(R.color.tabsScrollColor); | |
} | |
}); | |
FAB = (ImageButton) findViewById(R.id.imageButton); | |
FAB.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
selectImage(); | |
} | |
}); | |
// Setting the ViewPager For the SlidingTabsLayout | |
tabs.setViewPager(pager); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
int id = item.getItemId(); | |
// case camera icon clicked | |
// if (id == R.id.action_camera) { | |
// selectImage(); | |
// } | |
return super.onOptionsItemSelected(item); | |
} | |
private void selectImage() { | |
final CharSequence[] options = { "Take Photo", "Choose from Gallery", "Cancel" }; | |
AlertDialog.Builder builder = new AlertDialog.Builder(this); | |
builder.setTitle("Add Photo To Start!"); | |
builder.setItems(options, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int item) { | |
if (options[item].equals("Take Photo")) | |
{ | |
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg"); | |
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); | |
startActivityForResult(intent, 1); | |
} | |
else if (options[item].equals("Choose from Gallery")) | |
{ | |
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); | |
startActivityForResult(intent, 2); | |
} | |
else if (options[item].equals("Cancel")) { | |
dialog.dismiss(); | |
} | |
} | |
}); | |
builder.show(); | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
ImageView viewImage = (ImageView) findViewById(R.id.image_placeholder); | |
ImageView analyzeIcon = (ImageView) findViewById(R.id.analyze_icon); | |
if (resultCode == RESULT_OK) { | |
if (requestCode == 1) { | |
File f = new File(Environment.getExternalStorageDirectory().toString()); | |
for (File temp : f.listFiles()) { | |
if (temp.getName().equals("temp.jpg")) { | |
f = temp; | |
break; | |
} | |
} | |
try { | |
Bitmap bitmap; | |
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); | |
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions); | |
viewImage.setImageBitmap(bitmap); | |
viewImage.setBackgroundResource(R.drawable.shape_view_border); | |
analyzeIcon.setVisibility(View.VISIBLE); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} else if (requestCode == 2) { | |
Uri selectedImage = data.getData(); | |
String[] filePath = { MediaStore.Images.Media.DATA }; | |
Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null); | |
c.moveToFirst(); | |
int columnIndex = c.getColumnIndex(filePath[0]); | |
String picturePath = c.getString(columnIndex); | |
c.close(); | |
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath)); | |
// Log.w("path of image from gallery", picturePath + ""); | |
viewImage.setImageBitmap(thumbnail); | |
analyzeIcon.setVisibility(View.VISIBLE); | |
} | |
} | |
} | |
@Override | |
public void onResume() | |
{ | |
super.onResume(); | |
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_11, this, mLoaderCallback); | |
} | |
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { | |
@Override | |
public void onManagerConnected(int status) { | |
switch (status) { | |
case LoaderCallbackInterface.SUCCESS: | |
{ | |
Log.i("opencv", "opencv success"); | |
} break; | |
default: | |
{ | |
super.onManagerConnected(status); | |
} break; | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment