简体   繁体   English

如何使用每个像素中的信息创建位图?

[英]How to create a bitmap with information in every pixel?

I'm creating a google maps application on Android and I'm facing problem. 我正在Android上创建Google Maps应用程序,但遇到了问题。 I have elevation data in text format. 我有文本格式的海拔数据。 It looks like this 看起来像这样

longtitude latitude elevation
491222     163550   238.270000
491219     163551   242.130000
etc.

This elevation information is stored in grid of 10x10 meters. 此海拔信息存储在10x10米的网格中。 It means that for every 10 meters is an elevation value. 这意味着每10米为一个高程值。 This text is too large so that I could find there the information I need so I would want to create a bitmap with this information. 该文本太大,因此我无法在此处找到所需的信息,因此我想使用此信息创建位图。

What I need to do is in certain moment to scan the elevation around my location. 我需要做的是在特定时刻扫描我所在位置附近的海拔高度。 There can be a lot of points to be scanned so I want to make it quick. 可能有很多要扫描的点,所以我想使其快速。 That's why I'm thinking about the bitmap. 这就是为什么我在考虑位图。

I don't know if it's even possible but my idea is that there would be a bitmap of size of my text grid and in every pixel would be information about the elevation. 我不知道这是否有可能,但我的想法是在我的文本网格上设置一个大小的位图,并且在每个像素中都包含有关高程的信息。 So it should be like invisible map over the google map placed in the place according to coordinates and when I need to learn the elevation about my location, I would just look at these pixels and read the value of elevation. 因此,这应该像是根据坐标放置在该位置的google地图上的不可见地图,当我需要了解有关我的位置的高程时,我只需查看这些像素并读取高程的值即可。

Do you think that is possible to create such a bitmap? 您认为可以创建这样的位图吗? I have just this idea but no idea how to implement it. 我只有这个主意,但不知道如何实施。 Eg how to store in it the elevation information, how to read that back, how to create the bitmap.. I would be very grateful for every advice, direction, source which you can give me. 例如,如何在其中存储高程信息,如何将其读回,如何创建位图。.对于您能给我的每条建议,方向和资源,我将不胜感激。 Thank you so much! 非常感谢!

As color with red, green, blue and alpha (opacity/transparency). 颜色为红色,绿色,蓝色和Alpha(不透明度/透明度)。 Start with all pixels transparent. 从所有透明像素开始。 and fill in the corresponding value as (R, G, B), non-transparent (the high eight bits. (Or an other convention for "not filled in." 并以(R,G,B)非透明(高八位)填充相应的值(或“不填充”的其他约定。

RGB form the lower 24 bits of an integer. RGB构成整数的低24位。

Longitude and latitude to x and y x和y的经度和纬度

Elevation to integer less 0x01_00_00_00. 高程到整数减去0x01_00_00_00。 And vice versa: 反之亦然:

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;  

BufferedImage with setRGB(code) or so (there are different possibilities). setRGB(code)左右的BufferedImage(存在不同的可能性)。

Use Oracles javadoc, by googling after BufferedImage and such. 通过在BufferedImage等之后进行谷歌搜索来使用Oracle的javadoc。

To fill unused pixels do an avaraging, in a second BufferedImage. 要填充未使用的像素,请在第二个BufferedImage中进行平均。 So as never to average to original pixels. 因此,永远不要平均到原始像素。

PS for my Netherlands elevation might be less than zero, so maybe + ... . 我荷兰海拔的PS可能小于零,所以也许+ ...。

BufferedImage is not available in android but android.graphics.Bitmap can be used. BufferedImage在android中不可用,但可以使用android.graphics.Bitmap Bitmap must be saved in lossless format (eg. PNG). 位图必须以无损格式(例如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;
}

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

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