【问题标题】:user select image and detect colour用户选择图像并检测颜色
【发布时间】:2015-05-08 01:41:38
【问题描述】:
package com.example.hello;
//import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

/**
* This activity displays the gallery image picker.
* It displays the image that was picked.
*
* @author ITCuties
*
*/
public class GalleryActivity extends Activity implements OnClickListener {

  // Image loading result to pass to startActivityForResult method.
  private static int LOAD_IMAGE_RESULTS = 1;

  // GUI components
  private Button button;  // The button
  private ImageView image;// ImageView

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_gallery);

      // Find references to the GUI objects
      button = (Button)findViewById(R.id.button);
      image = (ImageView)findViewById(R.id.image);

      // Set button's onClick listener object.
      button.setOnClickListener(this);

  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);

      // Here we need to check if the activity that was triggers was the Image Gallery.
      // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
      // If the resultCode is RESULT_OK and there is some data we know that an image was picked.
      if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
          // Let's read picked image data - its URI
          Uri pickedImage = data.getData();
          // Let's read picked image path using content resolver
          String[] filePath = { MediaStore.Images.Media.DATA };
          Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
          cursor.moveToFirst();
          String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

          // Now we need to set the GUI ImageView data with data read from the picked file.
          image.setImageBitmap(BitmapFactory.decodeFile(imagePath));

          // At the end remember to close the cursor or you will end with the RuntimeException!
          cursor.close();
      }
  }

  @Override
  public void onClick(View v) {
      // Create the Intent for Image Gallery.
      Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

      // Start new activity with the LOAD_IMAGE_RESULTS to handle back the results when image is picked from the Image Gallery.
      startActivityForResult(i, LOAD_IMAGE_RESULTS);
  }
}

此代码从图库中选择图像..

package com.example.hello;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ColourPickerActivity extends Activity {

 TextView touchedXY, invertedXY, imgSize, colorRGB;
 ImageView imgSource1, imgSource2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_colourpicker);

       // touchedXY = (TextView)findViewById(R.id.xy);
       // invertedXY = (TextView)findViewById(R.id.invertedxy);
       // imgSize = (TextView)findViewById(R.id.size);
        colorRGB = (TextView)findViewById(R.id.colorrgb);
    // imgSource1 = (ImageView)findViewById(R.id.source1);
     imgSource2 = (ImageView)findViewById(R.id.source2);

     //imgSource1.setOnTouchListener(imgSourceOnTouchListener);
     imgSource2.setOnTouchListener(imgSourceOnTouchListener);

    }

    OnTouchListener imgSourceOnTouchListener
    = new OnTouchListener(){

  @Override
  public boolean onTouch(View view, MotionEvent event) {

   float eventX = event.getX();
   float eventY = event.getY();
   float[] eventXY = new float[] {eventX, eventY};

   Matrix invertMatrix = new Matrix();
   ((ImageView)view).getImageMatrix().invert(invertMatrix);

   invertMatrix.mapPoints(eventXY);
   int x = Integer.valueOf((int)eventXY[0]);
   int y = Integer.valueOf((int)eventXY[1]);

//   touchedXY.setText(
//     "touched position: "
//     + String.valueOf(eventX) + " / " 
//     + String.valueOf(eventY));
//   invertedXY.setText(
//     "touched position: "
//     + String.valueOf(x) + " / " 
//     + String.valueOf(y));

   Drawable imgDrawable = ((ImageView)view).getDrawable();
   Bitmap bitmap = ((BitmapDrawable)imgDrawable).getBitmap();

//   imgSize.setText(
//     "drawable size: "
//     + String.valueOf(bitmap.getWidth()) + " / " 
//     + String.valueOf(bitmap.getHeight()));

   //Limit x, y range within bitmap
//   if(x < 0){
//    x = 0;
//   }else if(x > bitmap.getWidth()-1){
//    x = bitmap.getWidth()-1;
//   }
//   
//   if(y < 0){
//    y = 0;
//   }else if(y > bitmap.getHeight()-1){
//    y = bitmap.getHeight()-1;
//   }

   int touchedRGB = bitmap.getPixel(x, y);

   colorRGB.setText("touched color: " + "#" + Integer.toHexString(touchedRGB));
   colorRGB.setTextColor(touchedRGB);

   return true;
  }};

}

此代码告诉触摸可绘制文件夹中的图像时像素的颜色..我如何整合这两个活动..我想从图库中选择图像并在从图库中选择的图像上应用颜色检测器

【问题讨论】:

    标签: android image-processing colors pixels color-picker


    【解决方案1】:

    首先,您需要从onActivityResult 中的第一个类调用第二个类,将imagePath 添加到 Intent:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
    
      if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
          (previous code)
    
          Intent intent = new Intent(this, ColourPickerActivity.class);
          intent.putExtra("IMAGE_PATH", imagePath);
          startActivity(intent)
      }
    }
    

    然后在您的ColourPickerActivity 中,您需要提取该路径并将其加载到您的 ImageView 中:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        (previous code)
    
        String imagePath = getIntent().getStringExtra("IMAGE_PATH");
    
        //Load image into the ImageView
        imgSource2.setImageBitmap(BitmapFactory.decodeFile(imagePath));
    }
    

    希望对您有所帮助,干杯!

    【讨论】:

    • 这个怎么用?? int touchRGB = bitmap.getPixel(x, y);
    • @user4440416 当用户触摸图像时,ColourPickerActivity 类会自动完成。如果您向上看,您会看到位图是从图像的可绘制对象中提取的。你可以试试这个看看它是否有效? :)
    • 请建议 .. m 遇到麻烦
    • 我提到的代码处于工作状态,但我想从图库中选择图像,然后在该图像上应用颜色选择器 ..
    • @user4440416 这就是我的回答,你必须为这两个活动添加一些额外的代码。
    猜你喜欢
    • 2020-02-07
    • 2018-07-16
    • 2011-04-21
    • 1970-01-01
    • 2011-12-05
    • 2019-05-12
    • 2021-08-17
    • 1970-01-01
    • 2011-01-17
    相关资源
    最近更新 更多