Advertisement
Guest User

Untitled

a guest
Nov 15th, 2012
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.03 KB | None | 0 0
  1. package com.atonea.colorgrabber;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.graphics.Bitmap;
  8. import android.graphics.Color;
  9. import android.graphics.drawable.BitmapDrawable;
  10. import android.os.Bundle;
  11. import android.provider.MediaStore;
  12. import android.view.MotionEvent;
  13. import android.view.View;
  14. import android.widget.Button;
  15. import android.widget.ImageButton;
  16. import android.widget.ImageView;
  17.  
  18. public class ColorGrabberActivity extends Activity {
  19.     /** Called when the activity is first created. */
  20.    
  21.     private static final int PICTURE_RESULT = 8765;
  22.     private ImageView imageView;
  23.     public String popUpString;
  24.     private int[][] rgbValues;
  25.     public float touchX;
  26.     public float touchY;
  27.     public int rgbX;
  28.     public int rgbY;
  29.     public String ColorString;
  30.     public Bitmap bitmap;
  31.     public Bitmap pic;
  32.     public int redValue;
  33.     public int blueValue;
  34.     public int greenValue;
  35.     public boolean picTaken = false;
  36.    
  37.    
  38.     @Override
  39.     public void onCreate(Bundle savedInstanceState) {
  40.         super.onCreate(savedInstanceState);
  41.         setContentView(R.layout.main);
  42.         Button speakButton = (Button) findViewById(R.id.button1);
  43.         speakButton.setOnClickListener(new View.OnClickListener() {
  44.             @Override
  45.             public void onClick(View v) {
  46.                 takePhoto();
  47.             }
  48.         });
  49.     }
  50.    
  51.     public void takePhoto(){
  52.         Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  53.         this.startActivityForResult(camera, PICTURE_RESULT);
  54.     }
  55.    
  56.     protected void onActivityResult(int requestCode,int resultCode, Intent camera)
  57.     {
  58.         if (requestCode == PICTURE_RESULT) //
  59.             if (resultCode == Activity.RESULT_OK) {
  60.                // Display image received on the view
  61.                 Bundle b = camera.getExtras(); // Kept as a Bundle to check for other things in my actual code
  62.                 Bitmap pic = (Bitmap) b.get("data");
  63.  
  64.                 if (pic != null) { // Display your image in an ImageView in your layout (if you want to test it)
  65.                     picTaken = true;
  66.                     setContentView(R.layout.photo);
  67.                     imageView = (ImageView) findViewById(R.id.imageView1);
  68.                     imageView.setImageBitmap(pic);
  69.                     imageView.invalidate();                    
  70.                                      
  71.                 }
  72.             }
  73.             else if (resultCode == Activity.RESULT_CANCELED) {
  74.                
  75.             }
  76.    }
  77.    
  78.    public void popUp(String popUpString) {
  79.     AlertDialog.Builder builder = new AlertDialog.Builder(this);
  80.     builder.setTitle("Color");
  81.     builder.setMessage(popUpString)
  82.                 .setCancelable(false)
  83.                 .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
  84.                         public void onClick(DialogInterface dialog, int id) {
  85.                         dialog.cancel();
  86.               }
  87.              
  88.           });
  89.    builder.show();
  90.    }
  91.    
  92.    public boolean onTouchEvent(MotionEvent e) {
  93.     if(e.getAction() == MotionEvent.ACTION_DOWN)
  94.        {
  95.                touchX = e.getX();
  96.                touchY = e.getY();
  97.                touchX = Math.round(touchX);
  98.                touchY = Math.round(touchY);
  99.                rgbX = (int)touchX;
  100.                rgbY = (int)touchY;
  101.                getcolor(rgbX, rgbY);
  102.                
  103.        }
  104.     return super.onTouchEvent(e);
  105.  
  106.    }
  107.    
  108.    public void getcolor(int PosX, int PosY) {
  109.        Bitmap pic2 = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
  110.        if (pic2 != null) {
  111.            int pixel = pic2.getPixel(rgbX, rgbY);
  112.            redValue = Color.red(pixel);
  113.            blueValue = Color.blue(pixel);
  114.            greenValue = Color.green(pixel);
  115.            ColorString = String.valueOf(redValue) + " " + String.valueOf(blueValue) + " " + String.valueOf(greenValue);
  116.            popUp(ColorString);
  117.        } else {
  118.            popUp("Picture is null");
  119.        }
  120.    }
  121.    
  122.    
  123.    
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement