繁体   English   中英

getRGB 在尝试获取图像坐标的颜色时抛出错误

[英]getRGB throws error when trying to get the color of coordinates of an image

我一直在寻找获取图像坐标 (x, y) 颜色的方法。

我有一张图片, Image depthImage = mActivityTestRule.getActivity().getDepthImage(); ,我想通过给出坐标来做到这一点。

我的项目是一个ARCore 项目git clone https://github.com/google-ar/arcore-android-sdk.git

我找到的一种解决方案是

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);

但表达式getRGB以红色突出显示。 Android Studio 没有任何建议的解决方案。 我尝试使用java.awt.image.BufferedImage导入 BufferedImage 来使用它,但甚至无法导入它。

我还找到了一个看起来像这样的解决方案:

Color mycolor = new Color(img.getRGB(x, y));
int red = mycolor.getRed();
int green = mycolor.getGreen();
int blue = mycolor.getBlue();
int alpha = mycolor.getAlpha();

但同样的事情发生了。 getRGB 无法正确识别。

你有什么办法处理这个吗?

附加信息:

这些是我的进口商品:

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;

这是我的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'//
}

由于这些答案,我终于解决了它: How to convert android.media.Image to bitmap object? 以及如何从 Android 上的 Bitmap 获取 RGB 值?

根据如何将 android.media.Image 转换为 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);
}

根据 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM