이미지 내부의 픽셀 값을 분석하고 싶습니다. 16 진수 픽셀 값의 2와 4 위치에서 값을 꺼내어 콘솔에 표시하려고합니다. 내 코드에서 하위 문자열을 사용하고 있습니다. 프로그램을 실행하려고했지만 잠시 후 stringoutofboundexception 오류가 표시됩니다.오류 프로그램 String을 실행하는 동안 StringIndexOutOfBoundsException
오류 표시 :
public class getPixelData
{
private static final double bitPerColor = 4.0;
public getPixelData()
{
}
public int[] getPixelData(BufferedImage img, int w, int h) throws IOException
{
int argb = img.getRGB(w, h);
int rgb[] = new int[]
{
(argb >> 16) & 0xff, //red
(argb >> 8) & 0xff, //green
(argb ) & 0xff //blue
};
int red = rgb[0];
int green = rgb[1]; //RGB Value in Decimal
int blue = rgb[2];
System.out.println("\nRGBValue in Decimal --> " + "\nRed: " + red + " Green: " + green + " Blue: " + blue);
//Convert each channel RGB to Hexadecimal value
String rHex = Integer.toHexString((int)(red));
String gHex = Integer.toHexString((int)(green));
String bHex = Integer.toHexString((int)(blue));
System.out.println("\nRGBValue in Hexa --> " + "\nRed Green Blue " + rHex + gHex + bHex);
//Check position 2 and 4 of hexa value for any changes
String hexa2, hexa4 = "";
String rgbHexa = rHex + gHex + bHex;
hexa2 = rgbHexa.substring(1,2);
System.out.println("\nString RGB Hexa: " + rgbHexa);
System.out.println("\nSubstring at position 2: " + hexa2);
//the program stops at here and then displayed the stringoutofboundexception
hexa4 = rgbHexa.substring(3,4);
System.out.println("\nSubstring at position 4: " + hexa4);
...
return rgb;
}
}
사람이 내 문제를 해결하는 데 도움을위한 희망 :
java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.substring(String.java:1907)
at getPixelData.getPixelData(getPixelData.java:51)
at getPixelRGB.main(getPixelRGB.java:58)
이이 내 코드입니다. 저는 아직 Java에 익숙하지 않습니다.
덕분 substring
방법에
16 진수 문자열이 충분하지 않습니다. 내 추측은 "0A0B0C"대신 "ABC"(즉, 0이 붙지 않음)처럼 보일 것입니다. –
'rgb' 값은 무엇이고, 생성 된 문자열은 인쇄 할 때 어떻게 보이나요? 또한,'substring' 호출의 범위는 이상하게 보입니다. 어떤 부분을 캡처하고 싶습니까? –
예를 들어, rgb 십진수 값에서 139 (빨간색) 117 (녹색) 94 (파란색) hexa 값으로 변환하면 다음과 같습니다. 8B755E. 나는 2 위 자리에서 가치 B를 꺼내고 4 위에서 5 위를 치고 싶습니다. 저를 도울 수 있습니까? – user2890264