所以我正在尝试检查我的图库中图片的RGB值,这是我的代码如何工作:我得到图像并将其设置在我的iv_image变量上,然后当我点击图像时,它会得到我点击的区域周围的RGB值。问题是当我更改图像时,位图值不会改变并保留前一图像的值。
这是我的代码:
iv_image.setOnTouchListener(new View.OnTouchListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public boolean onTouch(View v, MotionEvent motionEvent) {
if(!(iv_image.getDrawable() == null)){
int touchX = (int) motionEvent.getX();
int touchY = (int) motionEvent.getY();
iv_image.buildDrawingCache();
Bitmap bitmap = iv_image.getDrawingCache();
Drawable d = new BitmapDrawable(getResources(),bitmap);
/*
final ImageView iv = (ImageView)findViewById(R.id.iv_image);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
iv.getViewTreeObserver().removeOnPreDrawListener(this);
finalHeight = iv.getMeasuredHeight();
finalWidth = iv.getMeasuredWidth();
return true;
}
});
cicleOverlay.getLayoutParams().width = 150;
c
icleOverlay.getLayoutParams().height = 150;
*/
int valorMinimoR = 0, valorMaximoR = 0, valorMinimoG = 0,
valorMaximoG = 0, valorMinimoB = 0, valorMaximoB = 0;
for (int i=-50; i < 50; i++){
for (int x=-50; x < 50; x++) {
if ((touchX) > 0 && (touchY) > 0 && (touchX) < bitmap.getWidth() && (touchY) < bitmap.getHeight()) {
int pixelColor = bitmap.getPixel(touchX + i, touchY + x);
int A = Color.alpha(pixelColor);
int R = Color.red(pixelColor);
int G = Color.green(pixelColor);
int B = Color.blue(pixelColor);
if (mediaB == 0 && mediaG == 0 && mediaR == 0) {
mediaR = R;
mediaG = G;
mediaB = B;
}
if (valorMinimoR > R || valorMinimoR == 0)
valorMinimoR = R;
if (valorMaximoR < R)
valorMaximoR = R;
if (valorMinimoG > G || valorMinimoG == 0)
valorMinimoG = G;
if (valorMaximoG < G)
valorMaximoG = G;
if (valorMinimoB > B || valorMinimoB == 0)
valorMinimoB = B;
if (valorMaximoB < B)
valorMaximoB = B;
mediaR = (mediaR + R) / 2;
mediaG = (mediaG + G) / 2;
mediaB = (mediaB + B) / 2;
if (i == 0 && x == 0) {
r = R;
g = G;
b = B;
}
if (i >= 49 && x >= 49) {
tv_color.setText("\nVermelho: " + r + "\nVerde: " + g + "\nAzul: " + b +
"\nMedia Vermelho: " + mediaR + "\nMedia Verde: " + mediaG + "\nMedia Azul: " + mediaB +
"\nMenor/Maior Vermelho: " + valorMinimoR + "/" + valorMaximoR + "\nMenor/Maior Verde: " +
valorMinimoG + "/" + valorMaximoG + "\nMenor/Maior Azul: " + valorMinimoB + "/" + valorMaximoB);
int rr = math.round(mediaR);
int gg = math.round(mediaG);
int bb = math.round(mediaB);
//iv_color.setBackgroundColor(Color.rgb(rr, gg, bb));
iv_color.setBackground(d);
}
}
}
}
}
mediaR = 0;
mediaG = 0;
mediaB = 0;
return true;
}
});
我尝试使用bitmap.recycle()和bitmap = null来重置位图值,但是当我这样做时,程序崩溃了,任何人都对如何解决它有什么想法?
编辑1:我设置修改后的图像的部分:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data){
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
iv_image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}