Tag: bytearray

使用JSONObject放置/获取字节数组值

我尝试使用以下代码从JSONObject获取我的byte[]值,但我没有获得原始byte[]值。 JSONArray jSONArray = jSONObject.getJSONArray(JSONConstant.BYTE_ARRAY_LIST); int len = jSONArray.length(); for (int i = 0; i < len; i++) { byte[] b = jSONArray.get(i).toString().getBytes(); //Following line creates pdf file of this byte arry "b" FileCreator.createPDF(b, "test PDF From Web Resource.pdf"); } } 上面的代码创建pdf文件,但文件无法打开即损坏的文件。 当我使用相同的类和方法来创建文件时: FileCreator.createPDF(b, “test PDF From Web Resource.pdf”); 在添加到JSONObject之前,如下所示: JSONObject jSONObject = new JSONObject(); […]

在队列中记录语音并将其发送到服务器

我正在开发语音应用程序。 我需要一个某种类型的缓冲队列,以便我在一个线程中连续记录,将缓冲区充满字节放入队列并传输到服务器,然后从队列中取出下一个缓冲区。 这是录音代码: Queue qArray = new LinkedList(); recordingThread = new Thread(new Runnable() { @Override public void run() { bData = new byte[BufferElements]; while (isRecording) { recorder.read(bData, 0, BufferElements); qArray.add(bData); if (AudioRecord.ERROR_INVALID_OPERATION != 0) { SendAudio(); } } } }, “AudioRecorder Thread”); recordingThread.start(); 但是在将它发送到服务器时,它仍然缺少少量的byte []数据 这是向服务器代码发送语音: try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost […]

从pdf的字节数组中获取pdf的第一页作为图像

我在byte array得到一个pdf。 我想将pdf的第一页转换为image 。 我已经厌倦了com.lowagie.text.pdf提供的课程,如下所示 – PdfReader reader = new PdfReader(input); reader.selectPages(“1”); File file = new File(“D:/img1.jpg”); BufferedImage pdfImage = ImageIO.read(new ByteArrayInputStream(reader.getPageContent(1))); ImageIO.write(pdfImage, “jpg”, file); 当调用ImageIO.write时,这样做会给我一个exception? 当我获取reader.getPageContent(1)返回的字节数组的大小时,我得到一个1000+的值。 令我困惑的是为什么我会得到例外。 例外 – java.lang.IllegalArgumentException: image == null! 我也试过了itext,但没用。 您能否建议我从pdf文件的字节数组中获取第1页(第1页为图像)的图像?

创建一个字节数组

我想将波形文件中的字节读入数组。 由于读取的字节数取决于波形文件的大小,因此我创建了一个最大大小为1000000的字节数组。但这会导致数组末尾出现空值。 所以,我想创建一个动态增加的数组,我发现ArrayList是解决方案。 但是AudioInputStream类的read()函数只将字节读入字节数组! 如何将值传递给ArrayList?

‘str = new String(bytes,“UTF8”)’和’bytes = str.getBytes(“UTF8”)’中的字节值不一样

我可以看到它们与我创建字符串的字节不同! 我用“AES / CBC / PKCS5Padding”来获取字符串。 public static void main(String[] args) { try { int randomNumber = CNStationQueueUtil.randInt(0, 99999); String key = “AES_KEY_TAKENUMB”; byte[] bytes = EncryptHelper.encrypt(key, String.format(“%%%d%%%d”, 1001, randomNumber)); String str = new String(bytes, “UTF8”); System.out.println(“str = ” + str); System.out.println(); byte[] utf8Bytes = str.getBytes(“UTF8”); printBytes(utf8Bytes, “utf8Bytes”); } catch (Exception e) { e.printStackTrace(); } […]

BufferedReader直接到byte

有没有可能我的后续BufferedReader能够将输入直接放入byte []? public static Runnable reader() throws IOException { Log.e(“Communication”, “reader”); din = new DataInputStream(sock.getInputStream()); brdr = new BufferedReader(new InputStreamReader(din), 300); boolean done = false; while (!done) { try { char[] buffer = new char[200]; int length = brdr.read(buffer, 0, 200); String message = new String(buffer, 0, length); btrar = message.getBytes(“ISO-8859-1”); int i=0; for (int […]

将Document对象转换为Byte

我是这样的init对象: DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); 之后,我通过将数据插入doc对象来构建XML文件。 最后,我将内容写入计算机上的文件。 我的问题是如何将doc的内容写入byte[] ?* 这就是我将内容写入XML文件的方式: TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(“changeOut.xml”)); // Output to console for testing // StreamResult result = new StreamResult(System.out); transformer.transform(source, result);

使用蓝牙热敏打印机从Android应用程序打印图像?

我一直试图获得一张图片的印刷品,但到目前为止我还没有成功。 该打印机是本地制造的2英寸热敏打印机,打印分辨率为8点/ mm,384点/线,203 dpi。 该打印机基于具有“NXP 2388 ARM v7 Microproc”的电路板。 内存FLASH大小为512 KB,RAM:128 KB,接收缓冲区大小为16KB。 我一直跟着这个问题 。 问题:我尝试打印的图像是576×95 res。 打印图像(有一些错误LED点亮并调试蜂鸣器噪音:D)但图像的方向是垂直的而不是水平打印; 也是在页面的最左侧,图像的顶部也被切断了 假设我没有通过一些标志(同时使“数据包”)与我拥有的打印机兼容。 我之前没有从事蓝牙打印,所以任何帮助都很明显:) 我现有的主要活动: public class MainActivity extends Activity { // will show the statuses TextView myLabel; // will enable user to enter any text to be printed EditText myTextbox; EditText devName; public TableLayout tl2; String devid; String[] […]

Java:浮点数/整数的字节数

我有一个从x-plane通过UDP发送的字节数组。 字节(4)都是浮点数或整数…我试图将它们转换为浮点数但到目前为止没有运气… 示例数组:字节数据[41] = { – 66,30,73,0}; 如何将4个字节转换为int或float并且不浮动使用8个字节?

将ByteArray转换为UUID java

问题是如何将ByteArray转换为GUID。 以前我将我的guid转换为字节数组,在一些事务之后我需要从字节数组返回guid。 我怎么做。 虽然不相关但从Guid到byte []的转换如下 public static byte[] getByteArrayFromGuid(String str) { UUID uuid = UUID.fromString(str); ByteBuffer bb = ByteBuffer.wrap(new byte[16]); bb.putLong(uuid.getMostSignificantBits()); bb.putLong(uuid.getLeastSignificantBits()); return bb.array(); } 但是如何将其转换回来? 我试过这个方法,但它没有给我相同的价值 public static String getGuidFromByteArray(byte[] bytes) { UUID uuid = UUID.nameUUIDFromBytes(bytes); return uuid.toString(); } 任何帮助将不胜感激。