2013-04-28 16 views
0

我在Android上創建一個谷歌地圖應用程序,我面臨問題。我有文本格式的高程數據。它看起來像這樣如何在每個像素中創建包含信息的位圖?

longtitude latitude elevation 
491222  163550 238.270000 
491219  163551 242.130000 
etc. 

此高程信息存儲在10x10米的網格中。這意味着每10米是一個標高值。這段文字太大了,所以我可以找到我需要的信息,所以我想用這些信息創建一個位圖。

我需要做的是在特定時刻掃描我的位置周圍的高程。可以有很多要掃描的點,所以我想快點。這就是爲什麼我在考慮位圖。

我不知道這是否可能,但我的想法是,將有一個我的文字網格大小的位圖,並在每個像素將有關高程的信息。所以它應該像google地圖上的隱形地圖一樣,放置在根據座標的地方,當我需要了解有關我的位置的高程時,我只需看一下這些像素並讀取高程值即可。

您認爲可以創建這樣的位圖嗎?我只是這個想法,但不知道如何實現它。例如,如何在其中存儲高程信息,如何讀取該信息,如何創建位圖..我將非常感謝您給我提供的每個建議,方向和來源。非常感謝!

回答

0

BufferedImage在android中不可用,但可以使用android.graphics.Bitmap。位圖必須以無損格式保存(例如PNG)。

double[] elevations={238.27,242.1301,222,1}; 
int[] pixels = doublesToInts(elevations); 

    //encoding 
Bitmap bmp=Bitmap.createBitmap(2, 2, Config.ARGB_8888); 
bmp.setPixels(pixels, 0, 2, 0, 0, 2, 2); 
File file=new File(getCacheDir(),"bitmap.png"); 
try { 
    FileOutputStream fos = new FileOutputStream(file); 
    bmp.compress(CompressFormat.PNG, 100, fos); 
    fos.close(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

//decoding 
Bitmap out=BitmapFactory.decodeFile(file.getPath()); 
if (out!=null) 
{ 
    int [] outPixels=new int[out.getWidth()*out.getHeight()]; 
    out.getPixels(outPixels, 0, out.getWidth(), 0, 0, out.getWidth(), out.getHeight()); 
    double[] outElevations=intsToDoubles(outPixels); 
} 

static int[] doublesToInts(double[] elevations) 
{ 
    int[] out=new int[elevations.length]; 
    for (int i=0;i<elevations.length;i++) 
    { 
     int tmp=(int) (elevations[i]*1000000);   
     out[i]=0xFF000000|tmp>>8; 
    } 
    return out; 
} 
static double[] intsToDoubles(int[] pixels) 
{ 
    double[] out=new double[pixels.length]; 
    for (int i=0;i<pixels.length;i++) 
     out[i]=(pixels[i]<<8)/1000000.0; 
    return out; 
} 
+0

謝謝你的回覆!它完美的作品。你會如何推薦我繪製高程值,以便它與顏色的int值兼容。我的意思是,例如,當我想要映射高程23827.當我將它轉換爲十六進制(5D13)時,我可以在像素中看到值23827,但在outPixels中看到的是0.我只有這種方式int elevation = 0xFF023827; '工作,但我猜想它很複雜。 – Bodyboard 2013-04-28 17:00:43

+0

@Bodyboard 0x5D13 = 0x00 00 5D 13.請注意,alpha分量是零,因此無論其餘分量是像素,它都是有效透明的。我已經將示例映射代碼添加到答案中。 – koral 2013-04-28 22:29:04

+0

非常感謝! :)我現在明白了 – Bodyboard 2013-04-30 10:58:47

0

顏色爲紅色,綠色,藍色和alpha(不透明度/透明度)。從所有像素開始透明。並填寫相應的值爲(R,G,B),不透明(高八位)(或其他約定爲「未填寫」。

RGB形成整數的低24位。

經度和緯度x和y

海拔爲整數少0x01_00_00_00反之亦然:。

的BufferedImage與
double elevation = 238.27; 
int code = (int)(elevation * 100); 
Color c = new Color(code); // BufferedImage uses int, so 'code' sufThat does not fices. 
code = c.getRGB(); 
elevation = ((double)code)/100; 

setRGB(code)左右(有不同的可能性)

使用Oracles javadoc,通過在BufferedImage之後進行搜索等。

要填充未使用的像素,請在第二個BufferedImage中進行評估。所以永遠不要平均到原始像素。

P.S.對於我的荷蘭海拔可能小於零,所以也許+ ...。

+0

謝謝你的回覆!我已經使用android.graphics.Bitmap作爲@droid提到,但我會在接下來的步驟中使用你的建議。 Bestregards – Bodyboard 2013-04-28 17:07:37

相關問題