I have been searching for ways to get the color of a coordinate (x, y) of an image.
I have an image, Image depthImage = mActivityTestRule.getActivity().getDepthImage();
, and I would like to do that, by giving the coordinates.
My project is an ARCore project (git clone https://github.com/google-ar/arcore-android-sdk.git
)
One solution that I found is
int clr = depthImage.getRGB(x, y);
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
System.out.println("Red Color value = " + red);
System.out.println("Green Color value = " + green);
System.out.println("Blue Color value = " + blue);
but the expression getRGB
is highlighted in red. There are not any suggested solutions by Android Studio. I tried importing BufferedImage using java.awt.image.BufferedImage
to use it, but it was not possible even to import it.
I have also found a solution that would look like this:
Color mycolor = new Color(img.getRGB(x, y));
int red = mycolor.getRed();
int green = mycolor.getGreen();
int blue = mycolor.getBlue();
int alpha = mycolor.getAlpha();
But the same thing happens. getRGB is not recognized properly.
Do you have any way to deal with this?
These are my imports:
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.junit.Assert.assertTrue;
import java.awt.Image; //Error: Cannot resolve symbol 'Image'
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.Image;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import androidx.test.espresso.ViewInteraction;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;
import androidx.test.uiautomator.UiDevice;
import com.google.ar.core.Anchor;
import com.google.ar.core.examples.java.helloar.HelloArActivity;
import com.google.ar.core.examples.java.helloar.R;
import com.google.ar.core.examples.java.helloar.WrappedAnchor;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
And this is my build.gradle(:app)
:
apply plugin: 'com.android.application'
android {
compileSdkVersion 32
defaultConfig {
applicationId "com.google.ar.core.examples.java.helloar"
// AR Optional apps must declare minSdkVersion >= 14.
// AR Required apps must declare minSdkVersion >= 24.
minSdkVersion 24
targetSdkVersion 32
versionCode 1
versionName '1.0'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"//
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
// ARCore (Google Play Services for AR) library.
implementation 'com.google.ar:core:1.34.0'
// Obj - a simple Wavefront OBJ file loader
// https://github.com/javagl/Obj
implementation 'de.javagl:obj:0.2.1'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.test.ext:junit:1.1.3'
//testImplementation 'junit:junit:4.13.2'
//androidTestImplementation 'androidx.test.ext:junit:1.1.3'
implementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test:runner:1.0.1'
implementation 'com.android.support.test:rules:1.0.2'
implementation 'androidx.test.uiautomator:uiautomator:2.2.0'//
}
I have finally solved it, thanks to these answers: How to convert android.media.Image to bitmap object? and How do you get the RGB values from a Bitmap on Android?
According to How to convert android.media.Image to bitmap object?,
Image depthImage = mActivityTestRule.getActivity().getDepthImage();
try {
ByteBuffer buffer = depthImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
Bitmap bitmapImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
} catch (Exception e) {
mActivityTestRule.getActivity().testFinishedMessage(false);
Thread.sleep(60000);
assertTrue(false);
}
And according to How do you get the RGB values from a Bitmap on Android?,
int color = bitmapImage.getPixel(x, y);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);