Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.atonea.colorgrabber;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.Color;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Bundle;
- import android.provider.MediaStore;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageButton;
- import android.widget.ImageView;
- public class ColorGrabberActivity extends Activity {
- /** Called when the activity is first created. */
- private static final int PICTURE_RESULT = 8765;
- private ImageView imageView;
- public String popUpString;
- private int[][] rgbValues;
- public float touchX;
- public float touchY;
- public int rgbX;
- public int rgbY;
- public String ColorString;
- public Bitmap bitmap;
- public Bitmap pic;
- public int redValue;
- public int blueValue;
- public int greenValue;
- public boolean picTaken = false;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button speakButton = (Button) findViewById(R.id.button1);
- speakButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- takePhoto();
- }
- });
- }
- public void takePhoto(){
- Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- this.startActivityForResult(camera, PICTURE_RESULT);
- }
- protected void onActivityResult(int requestCode,int resultCode, Intent camera)
- {
- if (requestCode == PICTURE_RESULT) //
- if (resultCode == Activity.RESULT_OK) {
- // Display image received on the view
- Bundle b = camera.getExtras(); // Kept as a Bundle to check for other things in my actual code
- Bitmap pic = (Bitmap) b.get("data");
- if (pic != null) { // Display your image in an ImageView in your layout (if you want to test it)
- picTaken = true;
- setContentView(R.layout.photo);
- imageView = (ImageView) findViewById(R.id.imageView1);
- imageView.setImageBitmap(pic);
- imageView.invalidate();
- }
- }
- else if (resultCode == Activity.RESULT_CANCELED) {
- }
- }
- public void popUp(String popUpString) {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("Color");
- builder.setMessage(popUpString)
- .setCancelable(false)
- .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.cancel();
- }
- });
- builder.show();
- }
- public boolean onTouchEvent(MotionEvent e) {
- if(e.getAction() == MotionEvent.ACTION_DOWN)
- {
- touchX = e.getX();
- touchY = e.getY();
- touchX = Math.round(touchX);
- touchY = Math.round(touchY);
- rgbX = (int)touchX;
- rgbY = (int)touchY;
- getcolor(rgbX, rgbY);
- }
- return super.onTouchEvent(e);
- }
- public void getcolor(int PosX, int PosY) {
- Bitmap pic2 = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
- if (pic2 != null) {
- int pixel = pic2.getPixel(rgbX, rgbY);
- redValue = Color.red(pixel);
- blueValue = Color.blue(pixel);
- greenValue = Color.green(pixel);
- ColorString = String.valueOf(redValue) + " " + String.valueOf(blueValue) + " " + String.valueOf(greenValue);
- popUp(ColorString);
- } else {
- popUp("Picture is null");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement