Base64编码省略了最后2个字节(Base64 encoding omits final 2 bytes)
此代码将位图编码为Base64。 出于调试目的,我还写了几行来将所有数据放入普通文件中。 问题是文件不同:文件直接从位图保存:
DB 5A C4 80 00 00
从解码的b64数据中保存的文件:
DB 5A C4 80
所以在编码/解码后2个字节MIA正确的文件是第一个,因为它可以通过chrome,图像查看器等轻松打开。我的代码:
Bitmap exportedBitmap = Bitmap.createBitmap(EXPORTED_IMAGE_SIZE, EXPORTED_IMAGE_SIZE, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(exportedBitmap); float imageViewRatio = EXPORTED_IMAGE_SIZE / (float) getWidth(); canvas.save(); canvas.scale(imageViewRatio, imageViewRatio); drawPhoto(canvas, mScale); canvas.restore(); ByteArrayOutputStream outStream = new ByteArrayOutputStream(512000); Base64OutputStream base64OutputStream = new Base64OutputStream(outStream, Base64.DEFAULT); String timeString = new Date().toString(); File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "origin"+ timeString +".webp"); if (!outputFile.exists()) { outputFile.delete(); } outputFile.createNewFile(); FileOutputStream fileOutputStream = new FileOutputStream(outputFile); TeeOutputStream teeOutputStream = new TeeOutputStream(base64OutputStream, fileOutputStream); exportedBitmap.compress(Bitmap.CompressFormat.WEBP, 85, teeOutputStream); getContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(outputFile))); exportedBitmap.recycle(); teeOutputStream.flush(); String outString = outStream.toString(); teeOutputStream.close(); decodeDataToFile(timeString, outString);
并且保存解码文件方法(仅供参考 - 在不同平台后端服务上也是错误的结果):
private void decodeDataToFile(String pTimeString, String pOutString) throws IOException { File decodedFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "processed" + pTimeString + ".webp"); decodedFile.createNewFile(); FileOutputStream decodedFileOutputStream = new FileOutputStream(decodedFile); decodedFileOutputStream.write(Base64.decode(pOutString, Base64.DEFAULT)); decodedFileOutputStream.flush(); decodedFileOutputStream.close(); getContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(decodedFile))); }
This code is going to encode a bitmap to Base64. For debugging purposes I've written also few lines to put all the data into ordinary files. Problem is that files are different: File saved directly from bitmap:
DB 5A C4 80 00 00
file saved from decoded b64 data:
DB 5A C4 80
So after encode/decode 2 bytes MIA The right file is the first one as it can be easily opened by chrome, image viewers etc. My code:
Bitmap exportedBitmap = Bitmap.createBitmap(EXPORTED_IMAGE_SIZE, EXPORTED_IMAGE_SIZE, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(exportedBitmap); float imageViewRatio = EXPORTED_IMAGE_SIZE / (float) getWidth(); canvas.save(); canvas.scale(imageViewRatio, imageViewRatio); drawPhoto(canvas, mScale); canvas.restore(); ByteArrayOutputStream outStream = new ByteArrayOutputStream(512000); Base64OutputStream base64OutputStream = new Base64OutputStream(outStream, Base64.DEFAULT); String timeString = new Date().toString(); File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "origin"+ timeString +".webp"); if (!outputFile.exists()) { outputFile.delete(); } outputFile.createNewFile(); FileOutputStream fileOutputStream = new FileOutputStream(outputFile); TeeOutputStream teeOutputStream = new TeeOutputStream(base64OutputStream, fileOutputStream); exportedBitmap.compress(Bitmap.CompressFormat.WEBP, 85, teeOutputStream); getContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(outputFile))); exportedBitmap.recycle(); teeOutputStream.flush(); String outString = outStream.toString(); teeOutputStream.close(); decodeDataToFile(timeString, outString);
And the save decoded file method (just for reference - result of this is wrong also on different platform back end service):
private void decodeDataToFile(String pTimeString, String pOutString) throws IOException { File decodedFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "processed" + pTimeString + ".webp"); decodedFile.createNewFile(); FileOutputStream decodedFileOutputStream = new FileOutputStream(decodedFile); decodedFileOutputStream.write(Base64.decode(pOutString, Base64.DEFAULT)); decodedFileOutputStream.flush(); decodedFileOutputStream.close(); getContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(decodedFile))); }
原文:https://stackoverflow.com/questions/30444520
最满意答案
我能够传入maps.apple.com并使用字符串地址作为对象来查询url。
<%= link_to "#{transferred_from_address(call)} Click to map", "http://maps.apple.com/?q=#{transferred_from_address(call)}", :class => 'btn btn-success btn-medium' %>
I was able to pass in maps.apple.com and use a the string address as an object to query the url.
<%= link_to "#{transferred_from_address(call)} Click to map", "http://maps.apple.com/?q=#{transferred_from_address(call)}", :class => 'btn btn-success btn-medium' %>
相关问答
更多-
您需要将InAppBrowser插件添加到您的项目中,并使用系统Web浏览器打开链接,该浏览器将自动重定向至Apple地图: var mapLocationUrl = 'maps.apple.com/?ll=51.84,-8.30'; var ref = window.open(encodeURI(mapLocationUrl), '_system', 'location=no'); 查看官方的Apple URL Scheme Reference了解完整的参数列表。 You need to add the ...
-
如何在iOS 6中启动具有特定地址的iOS Maps App?(How to launch iOS Maps App with specific address in iOS 6?)[2022-07-24]
在这里找到答案。 它是通过使用CLGeocoder类完成的: // Check for iOS 6 Class mapItemClass = [MKMapItem class]; if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { CLGeocoder *geocoder = [[CLGeocoder alloc] in ... -
查看文档中有一个showsPointsOfInterest iOS 7的showsPointsOfInterest ,只需将它设置为NO (ObjC)或false (Swift)。 Looking at the documentation there is a showsPointsOfInterest as of iOS 7, just set it to NO (ObjC) or false (Swift).
-
只需使用Apple Maps API。 如果您可以通过在Apple Maps中提交其名称来查找商户,则可以通过API找到它。 在你的情况下,正确的参数是daddr ,像这样: http://maps.apple.com/?daddr=1+Infinite+Loop,+Cupertino,+CA 您可以组合多个参数,例如您的起始位置: http://maps.apple.com/?saddr=1024+Market+St,+San+Francisco,+CA&daddr=1+Infinite+Loop,+Cu ...
-
Apple允许这样做。 您可以在许多应用中看到它:它们会要求您在Apple Plan或Google Maps等其他应用之间进行选择以获取路线。 This is allowed by Apple. You can see it in many apps: they ask you to choose between other apps like Apple Plan or Google Maps for directions.
-
如何打开Apple Maps App(How do I open Apple Maps App)[2023-10-11]
目前的技术是创建一个MKMapItem并调用openInMapsWithLaunchOptions 。 MKMapItem可以由描述要显示的位置的MKPlacemark制成。 The current technique is to make an MKMapItem and call openInMapsWithLaunchOptions. The MKMapItem can be made from an MKPlacemark describing the location to be shown. -
如果您正在编写应用程序,则可以使用MKMapItem而不是使用链接URL方法。 使用其中一个,您可以添加一些额外信息,例如商家名称和电话号码,以及纬度/经度。 然后,您可以通过调用openInMapsWithLaunchOptions:在地图应用中打开一个。 If you're writing an app, you could use MKMapItem instead of using the link URL method. With one of these, you can add a bunch ...
-
第二种选择听起来合法。 根据WKInterfaceMap类引用,点击地图将直接打开Apple Watch Maps应用程序。 引用文档: WKInterfaceMap对象显示指定位置的非交互式地图。 必须从接口控制器动态配置映射。 使用WKInterfaceMap类的方法指定地图的可见区域并添加任何注释或兴趣点。 点击地图会在用户的Apple Watch上启动地图应用程序并显示相应的位置。 参考: https : //developer.apple.com/library/prerelease/ios/do ...
-
我能够传入maps.apple.com并使用字符串地址作为对象来查询url。 <%= link_to "#{transferred_from_address(call)} Click to map", "http://maps.apple.com/?q=#{transferred_from_address(call)}", :class => 'btn btn-success btn-medium' %> I was able to pass in maps.apple.com and use a the ...
-
在做了一些搜索后,我调查了MKLocalSearchCompleter是Apple在他们的Map应用程序中使用的类。 After doing some searching I've investigated that MKLocalSearchCompleter is the class that Apple uses in their Map app.