java - 如何获取从相机拍摄的图像并将其添加为谷歌地图上的标记?
问题描述
好的,所以我要做的是长按我的地图,然后使用相机意图拍照,然后将该照片作为用户“长按”的地图点上的标记返回
有效的是长按然后拍照。不起作用的是将该图像作为标记返回到地图。
这就是我用来添加标记并使用相机拍摄图像的方法:
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng mylocation = new LatLng(-xx.xxxx, xx.xxxx);
mMap.addMarker(new MarkerOptions().position(mylocation).title("Marker in mylocation"));
CameraPosition cameraPosition = new CameraPosition.Builder().target(mylocation).zoom(12).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
googleMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng latLng) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
root = Environment.getExternalStorageDirectory().toString() + "/My Folder";
imageFolderPath = root + "/saved_images";
File imagesFolder = new File(imageFolderPath);
imagesFolder.mkdirs();
imageName = "test.png";
File image = new File(imageFolderPath, imageName);
fileUri = Uri.fromFile(image);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePictureIntent, TAKE_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data, LatLng latlng) {
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
try {
GetImageThumbnail getImageThumbnail = new GetImageThumbnail();
bitmap = getImageThumbnail.getThumbnail(fileUri, this);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
MarkerOptions markerOptions = new MarkerOptions()
.position(latlng)
.icon(BitmapDescriptorFactory.fromBitmap(bitmap));
googleMap.addMarker(markerOptions);
}
}
});
这是 GetImageThumbnail 类:
public class GetImageThumbnail {
private static int getPowerOfTwoForSampleRatio(double ratio) {
int k = Integer.highestOneBit((int) Math.floor(ratio));
if (k == 0)
return 1;
else
return k;
}
public Bitmap getThumbnail(Uri uri, GoogleMap.OnMapLongClickListener test)
throws FileNotFoundException, IOException {
InputStream input = ((Context) test).getContentResolver().openInputStream(uri);
BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
onlyBoundsOptions.inJustDecodeBounds = true;
onlyBoundsOptions.inDither = true;// optional
onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
input.close();
if ((onlyBoundsOptions.outWidth == -2)
|| (onlyBoundsOptions.outHeight == -2))
return null;
int originalSize = Math.max(onlyBoundsOptions.outHeight, onlyBoundsOptions.outWidth);
double ratio = (originalSize > 200) ? (originalSize / 175) : 0.5;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
bitmapOptions.inDither = true;
bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
input = ((Context) test).getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
input.close();
return bitmap;
}
}
正如我提到的相机意图启动并且我可以拍照,但是在拍照后它返回到地图但没有标记?也没有错误消息或任何我能找到的关于它为什么不显示标记/图像的信息。
我确定我在这里做错了什么,但不知道是什么?
谢谢
解决方案
推荐阅读
- sql-server - 在 powershell 中使用 WMI 查询 SQL Server 数据库时出错
- c++ - 通过平移矩阵而不是 glTranslate() 在 OpenGL 中移动模型
- python-pptx - 在 python-pptx 中隐藏图表系列
- c# - 连接必须有效并打开 asp .net core
- php - 无法将 docker 用于 php 项目
- c - 为什么位为 01110011 的字节不会变成字符 `s`?
- python - 在 SQLAlchemy 中注释“exists”子查询
- php - 如何在php中格式化URL的字符串
- c# - 不可为空的整数在传递给存储过程时变为空
- javascript - 如何获取对象的元素?