Android - 使用Android相机进行颜色检测

时间:2014-02-23 10:08:08

标签: android colors camera detection

我目前正在开发一款可以显示相机并拍照的应用。该应用程序将识别图片的触摸部分的颜色。

这是代码。它捕获但当我触摸图片时没有任何反应。

private Camera cameraObject;
   private ShowCamera showCamera;
   public static Camera isCameraAvailiable(){
      Camera object = null;
      try {
         object = Camera.open(); 
      }
      catch (Exception e){
      }
      return object; 
   }

   private PictureCallback capturedIt = new PictureCallback() {

      @Override
      public void onPictureTaken(byte[] data, Camera camera) {

      Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);


      if(bitmap==null){
         Toast.makeText(getApplicationContext(), "not taken", Toast.LENGTH_SHORT).show();
      }
      else
      {
         Toast.makeText(getApplicationContext(), "taken", Toast.LENGTH_SHORT).show();       
      }
      cameraObject.release();
   }

};

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.colo_act);
 cameraObject = isCameraAvailiable();
 showCamera = new ShowCamera(this, cameraObject);
 final FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
 preview.addView(showCamera);

 /*
Get coordinates and the pixel
*/

 preview.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        if(event.getAction() == MotionEvent.ACTION_UP){
               int x = (int)event.getX();
               int y = (int)event.getY();
               /*
                * Change to Bitmap
                */
               preview.setDrawingCacheEnabled(true);
               preview.buildDrawingCache();
               Bitmap bitmap = preview.getDrawingCache();
               preview.buildDrawingCache();
               int pixel = bitmap.getPixel(x, y);

               int redValue = Color.RED;
               int blueValue = Color.BLUE;
               int greenValue = Color.GREEN;

               if(pixel==redValue){
                   Toast.makeText(getApplicationContext(), "Correct Thats Red", Toast.LENGTH_LONG).show();
               }
               else if(pixel==blueValue){
                   Toast.makeText(getApplicationContext(), "Correct Thats Blue", Toast.LENGTH_LONG).show();
               }
               else if(pixel==greenValue){
                   Toast.makeText(getApplicationContext(), "Correct Thats Green", Toast.LENGTH_LONG).show();
               }
               else{
                   Toast.makeText(getApplicationContext(), "That's Not Red , Blue or Green", Toast.LENGTH_LONG).show();
               }
     }
        return false;
    }
 });

}

public void snapIt(View view){
 cameraObject.takePicture(null, null, capturedIt);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
 getMenuInflater().inflate(R.menu.main, menu);
 return true;
}
}

ShowCamera

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder holdMe;
private Camera theCamera;

public ShowCamera(Context context,Camera camera) {
  super(context);
  theCamera = camera;
  holdMe = getHolder();
  holdMe.addCallback(this);
 }

 @Override
 public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
 }

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  try   {
     theCamera.setPreviewDisplay(holder);
     theCamera.startPreview();
     theCamera.setDisplayOrientation(90);
  } catch (IOException e) {
  }
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder arg0) {
  }

  }

我需要这样做才能捕获图像以获得触摸坐标的颜色。

0 个答案:

没有答案