对于想了解java-如何使用gzip将图像转换为base64字符串的读者,本文将提供新的信息,并且为您提供关于c#–如何将图像(.png)转换为base64字符串,反之亦然,并将其转换为指定位置、ja
对于想了解java-如何使用gzip将图像转换为base64字符串的读者,本文将提供新的信息,并且为您提供关于c# – 如何将图像(.png)转换为base64字符串,反之亦然,并将其转换为指定位置、java – 问题将Base64字符串转换为十六进制字符串、javascript-将PNG Base-64字符串转换为TIFF Base-64字符串、JavaScript-将图像转换为没有画布的base64的有价值信息。
本文目录一览:- java-如何使用gzip将图像转换为base64字符串
- c# – 如何将图像(.png)转换为base64字符串,反之亦然,并将其转换为指定位置
- java – 问题将Base64字符串转换为十六进制字符串
- javascript-将PNG Base-64字符串转换为TIFF Base-64字符串
- JavaScript-将图像转换为没有画布的base64
java-如何使用gzip将图像转换为base64字符串
我正在尝试转换和压缩从@L_301_0@上的文件路径获取的图像,以使用base64的gzip进行转换(我正在使用它,因为我的桌面版本是用Java编写的,因此也是如此).这是我目前用于压缩的内容:
Bitmap bm = BitmapFactory.decodeFile(imagePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
String base64Str = null;
ByteArrayOutputStream out_bytes = new ByteArrayOutputStream();
OutputStream out = new Base64.OutputStream(out_bytes);
try {
out.write(data);
out.close();
byte[] encoded = out_bytes.toByteArray();
base64Str = Base64.encodeBytes(encoded, Base64.GZIP);
baos.close();
} catch (Exception e) {}
解决方法:
这是您的代码当前正在执行的操作:
//1. Decode data from image file
Bitmap bm = BitmapFactory.decodeFile(imagePath);
...
//2. Compress decoded image data to JPEG format with max quality
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
...
//3. Encode compressed image data to base64
out.write(data);
...
//4. Compress to gzip format, before encoding gzipped data to base64
base64Str = Base64.encodeBytes(encoded, Base64.GZIP);
我不知道您的台式机版本是如何做到的,但是步骤3是不必要的,因为您要执行与步骤4相同的操作.
(已删除部分答案)
编辑:以下代码将从文件中读取字节,对这些字节进行gzip压缩并将其编码为base64.它适用于所有小于2 GB的可读文件.传递给Base64.encodeBytes的字节将与文件中的字节相同,因此不会丢失任何信息(与上面的代码相反,在上面的代码中,您首先将数据转换为JPEG格式).
/*
* imagePath has changed name to path, as the file doesn't have to be an image.
*/
File file = new File(path);
long length = file.length();
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
if(length > Integer.MAX_VALUE) {
throw new IOException("File must be smaller than 2 GB.");
}
byte[] data = new byte[(int)length];
//Read bytes from file
bis.read(data);
} catch (IOException e) {
e.printstacktrace();
} finally {
if(bis != null)
try { bis.close(); }
catch(IOException e) {}
}
//Gzip and encode to base64
String base64Str = Base64.encodeBytes(data, Base64.GZIP);
EDIT2:这应该解码base64字符串,并将解码的数据写入文件:
//outputPath is the path to the destination file.
//Decode base64 String (automatically detects and decompresses gzip)
byte[] data = Base64.decode(base64str);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outputPath);
//Write data to file
fos.write(data);
} catch(IOException e) {
e.printstacktrace();
} finally {
if(fos != null)
try { fos.close(); }
catch(IOException e) {}
}
c# – 如何将图像(.png)转换为base64字符串,反之亦然,并将其转换为指定位置
解决方法
public string ImagetoBase64(Image image,System.Drawing.Imaging.ImageFormat format) { using (MemoryStream ms = new MemoryStream()) { // Convert Image to byte[] image.Save(ms,format); byte[] imageBytes = ms.ToArray(); // Convert byte[] to Base64 String string base64String = Convert.ToBase64String(imageBytes); return base64String; } } public Image Base64ToImage(string base64String) { // Convert Base64 String to byte[] byte[] imageBytes = Convert.FromBase64String(base64String); using (var ms = new MemoryStream(imageBytes,imageBytes.Length)) { // Convert byte[] to Image ms.Write(imageBytes,imageBytes.Length); Image image = Image.FromStream(ms,true); return image; } }
java – 问题将Base64字符串转换为十六进制字符串
我遗漏的边缘情况是什么,或者我的算法中将Base64字符串转换为十六进制字符串是否有错误?
我最近决定尝试Matasano Crypto Challenges,但无论出于何种原因,我决定尝试编写第一个挑战,而不使用库来转换Hex和Base64字符串.
我已经设法让Hex到Base64转换工作,但是从输出中可以看出,当我尝试将Base64转换回Hex时会有轻微的异常(例如,将Base64的最后四个值与Hex输出进行比较) ).
Hex To Base64:
Should Print: SSdtIGtpbGxpbmcgeW91ciBicmfpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t
Actually Prints: SSdtIGtpbGxpbmcgeW91ciBicmfpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29tBase64 to Hex:
Should Print: 49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d
Actually Prints: 49276d206b696c6c696e6720796e717220627261696e206c696b65206120706e69732e6e6f3573206c717328726f2e6d
我使用https://conv.darkbyte.ru/来检查我的一些值,并假设该站点上的代码是正确的,似乎我的问题是从Base64获取Base10表示,而不是从Base10到Hex:
Decimal Equivalent
My Output:
73,39,109,32,107,105,108,110,103,121,110,113,114,98,97,101,112,115,46,111,53,108,40,109Site’s Output:
73,111,117,109,104,109
似乎所有带错误的值都聚集在40-60和100-120之间,但我不确定从那里到底要去哪里.我猜是有一些边缘情况我会注意处理,但我不确定那会是什么.
相关代码:
private static final Character[] base64Order = new Character[] { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/',}; private static final Character[] hexOrder = new Character[] { '0','f' }; public static String base64ToHex(String base64) throws Exception { if (base64.length() % 4 != 0 || base64.contains("[^a-zA-Z0-9\\+/]")) throw new Exception("InputNotBase64"); else { int charValue = 0; int index = 0; String hex = ""; BitSet bits = new BitSet(); for (int i = 0; i < base64.length(); i++) { charValue = base64.charat(i); // get actual value from ASCII table if (charValue > 64 && charValue < 91) charValue -= 65; if (charValue > 96 && charValue < 123) charValue -= 71; /// loop that adds to the BitSet reads right-to-left,so reverse // the bits and then shift charValue = Integer.reverse(charValue << 24) & 0xff; charValue >>= 2; // append binary values to the BitSet while (charValue != 0L) { if (charValue % 2 != 0) { bits.set(index); } index++; charValue >>= 1; } // account for trailing 0s while (index % 6 != 0) { index++; } } // read 8-bit integer value for hex-value lookup String temp; int remainder; for (int i = 0; i < index; i++) { charValue = (charValue | (bits.get(i) ? 1 : 0)); if ((i + 1) % 8 == 0) { temp = ""; while (charValue != 0L) { remainder = charValue % 16; temp = hexOrder[remainder] + temp; charValue /= 16; } hex += temp; } charValue <<= 1; } return hex; } }
解决方法
如果您替换以下代码
if (charValue > 64 && charValue < 91) charValue -= 65; if (charValue > 96 && charValue < 123) charValue -= 71;
通过
charValue = getPositionInBase64(charValue);
哪里
public static int getPositionInBase64(int n) { for (int p = 0; p < base64Order.length; p++) { if (n == base64Order[p]) { return p; } } return -1; }
一切正常
此外,当您使用字符而不是幻数时,代码更易读
if (charValue >= 'A' && charValue <= 'Z') charValue -= 'A'; ...
在这种情况下,发现问题更容易
因为你问我正在提出可能的改进来加速计算.
准备下表并初始化一次
// index = character,value = index of character from base64Order private static final int[] base64ToInt = new int[128]; public static void initBase64ToIntTable() { for (int i = 0; i < base64Order.length; i++) { base64ToInt[base64Order[i]] = i; } }
现在您可以通过简单的操作替换您的if / else链
charValue = base64ToInt[base64.charat(i)];
使用这个我写的方法比你的方法快几倍
private static String intToHex(int n) { return String.valueOf(new char[] { hexOrder[n/16],hexOrder[n%16] }); } public static String base64ToHexVer2(String base64) throws Exception { StringBuilder hex = new StringBuilder(base64.length()*3/4); //capacity Could be 3/4 of base64 string length if (base64.length() % 4 != 0 || base64.contains("[^a-zA-Z0-9\\+/]")) { throw new Exception("InputNotBase64"); } else { for (int i = 0; i < base64.length(); i += 4) { int n0 = base64ToInt[base64.charat(i)]; int n1 = base64ToInt[base64.charat(i+1)]; int n2 = base64ToInt[base64.charat(i+2)]; int n3 = base64ToInt[base64.charat(i+3)]; // in descriptions I treat all 64 base chars as 6 bit // all 6 bites from 0 and 1st 2 from 1st (00000011 ........ ........) hex.append(intToHex(n0*4 + n1/16)); // last 4 bites from 1st and first 4 from 2nd (........ 11112222 ........) hex.append(intToHex((n1%16)*16 + n2/4)); // last 2 bites from 2nd and all from 3rd (........ ........ 22333333) hex.append(intToHex((n2%4)*64 + n3)); } } return hex.toString(); }
我认为这段代码更快,主要是因为简单的转换为十六进制.如果您想要并且需要它,您可以测试它.
要测试速度,您可以使用以下构造
String b64 = "SSdtIGtpbGxpbmcgeW91ciBicmfpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"; try { Base64ToHex.initBase64ToIntTable(); System.out.println(Base64ToHex.base64ToHex(b64)); System.out.println(Base64ToHex.base64ToHexVer2(b64)); int howManyIterations = 100000; Date start,stop; long period; start = new Date(); for (int i = 0; i < howManyIterations; i++) { Base64ToHex.base64ToHexVer2(b64); } stop = new Date(); period = stop.getTime() - start.getTime(); System.out.println("Ver2 taken " + period + " ms"); start = new Date(); for (int i = 0; i < howManyIterations; i++) { Base64ToHex.base64ToHex(b64); } stop = new Date(); period = stop.getTime() - start.getTime(); System.out.println("Ver1 taken " + period + " ms"); } catch (Exception ex) { }
示例结果是
49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d 49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d Ver2 taken 300 ms Ver1 taken 2080 ms
但它只是近似值.当您首先检查Ver1而将Ver2检查为第二个时,结果可能会略有不同.另外,对于不同的javas(第6,7,8)以及用于启动java的不同设置,结果可能不同
javascript-将PNG Base-64字符串转换为TIFF Base-64字符串
我正在使用一个返回PNG编码的base64字符串的插件,我无法更改它,我必须使用它,但是我真正需要的是tiff编码值(base-64).有办法吗?
我试图创建一个画布,加载png base64,然后使用toDataURL(‘image / tiff’),但经过一些研究,我发现不支持tiff作为toDataURL()的输出.
有什么建议么?
解决方法:
由于浏览器通常不将TIFF作为目标文件格式来支持,因此您必须通过使用类型化数组并按照file specifications(请参见此处的Photoshop notes)建立文件结构来手动编码TIFF文件. doable:
>从画布获取原始RGBA位图(请记住,CORS很重要)
>在DataView视图中使用类型化数组,以便能够在未对齐的位置写入各种数据
>建立文件头,定义最少的TAGS集,并以所需的方式对RGBA数据进行编码(未压缩很容易实现,也可以简单地进行RLE压缩).
>构造最终文件缓冲区.在这里,您有一个ArrayBuffer可以作为字节传输,可以选择:
>使用ArrayBuffer和tiff mime-type转换为Blob.
>使用ArrayBuffer作为基础转换为Data-URI
更新canvas-to-tiff可用于将画布另存为TIFF图像(免责声明:我是作者).
要使用canvas-to-tiff获得Data-URI,您只需执行以下操作:
CanvasToTIFF.toDataURL(canvasElement, function(url) {
// url Now contains the data-uri.
window.location = url; // download, does not work in IE; just to demo
});
虽然,我建议使用toBlob(),或者如果您想给用户一个链接,则使用toObjectURL()(而不是toDataURL).
使用Data-URI的演示
var c = document.querySelector("canvas"),
ctx = c.getContext("2d");
// draw some graphics
ctx.stroke;
ctx.linewidth = 30;
ctx.arc(200, 200, 170, 0, 2*Math.PI);
ctx.stroke();
// Covert to TIFF using Data-URI (slower, larger size)
CanvasToTIFF.toDataURL(c, function(url) {
var a = document.querySelector("a");
a.href = url;
a.innerHTML = "Right-click this link, select Save As to save the TIFF";
})
<script src="https://cdn.rawgit.com/epistemex/canvas-to-tiff/master/canvastotiff.min.js">
</script>
<a href=""></a><br>
<canvas width=400 height=400></canvas>
使用对象URL的演示
var c = document.querySelector("canvas"),
ctx = c.getContext("2d");
// draw some graphics
ctx.stroke;
ctx.linewidth = 30;
ctx.arc(200, 200, 170, 0, 2*Math.PI);
ctx.stroke();
// Covert to TIFF using Object-URL (faster, smaller size)
CanvasToTIFF.toObjectURL(c, function(url) {
var a = document.querySelector("a");
a.href = url;
a.innerHTML = "Right-click this link, select Save As to save the TIFF";
})
<script src="https://cdn.rawgit.com/epistemex/canvas-to-tiff/master/canvastotiff.min.js">
</script>
<a href=""></a><br>
<canvas width=400 height=400></canvas>
JavaScript-将图像转换为没有画布的base64
我需要将图像转换为base64编码.我知道用画布很容易.但是问题在于它也应该在IE8中运行.但是IE8不支持HTML5,因此我无法使用画布来做到这一点.还有其他使用javaScript的方法将图像转换为base64吗?
谢谢
解决方法:
这里有一些答案
How can you encode a string to Base64 in JavaScript?
你有没有尝试过?
今天的关于java-如何使用gzip将图像转换为base64字符串的分享已经结束,谢谢您的关注,如果想了解更多关于c# – 如何将图像(.png)转换为base64字符串,反之亦然,并将其转换为指定位置、java – 问题将Base64字符串转换为十六进制字符串、javascript-将PNG Base-64字符串转换为TIFF Base-64字符串、JavaScript-将图像转换为没有画布的base64的相关知识,请在本站进行查询。
本文标签: