Tag: bytearray

从原始数据的字节数组中获取缓冲图像

我正在使用JNA。 我从我的c ++方法获取原始数据的字节数组。 现在我被困在如何使用这个原始数据字节数组在java中获取缓冲图像。 我曾尝试过很少的东西来把它作为tiff图像,但我得到了成功。 这是我到目前为止尝试的代码。 这里我的字节数组包含16位灰度图像的数据。 我从x传感器设备获得这些数据。 现在我需要从这个字节数组中获取图像。 第一次尝试 byte[] byteArray = myVar1.getByteArray(0, 3318000);//array of raw data ImageInputStream stream1=ImageIO.createImageInputStream(newByteArrayInputStream(byteArray)); ByteArraySeekableStream stream=new ByteArraySeekableStream(byteArray,0,3318000); BufferedImage bi = ImageIO.read(stream); 第二个尝试 SeekableStream stream = new ByteArraySeekableStream(byteArray); String[] names = ImageCodec.getDecoderNames(stream); ImageDecoder dec = ImageCodec.createImageDecoder(names[0], stream, null); //at this line get the error ArrayIndexOutOfBoundsException: 0 RenderedImage im = dec.decodeAsRenderedImage(); […]

Android中的字符串加密

我正在使用代码进行加密和加密。 它没有给出字符串结果。 字节数组未转换为字符串。 我尝试了一切几乎将字节数组转换为char但它没有给出结果。 public class EncryptionTest extends Activity { EditText input, output, outputDecrypt; String plain_text; byte[] key, encrypted_bytes,keyStart,byte_char_text,decrpyted_bytes ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_encryption_test); input = (EditText) findViewById(R.id.text_inputText); output = (EditText) findViewById(R.id.text_Result); outputDecrypt = (EditText) findViewById(R.id.text_decrypt_Result); Button encrypt_btn = (Button) findViewById(R.id.btn_encrpyt); Button decrypt_btn = (Button) findViewById(R.id.btn_Decrypt); plain_text = input.getText().toString(); keyStart = […]

使用FileChannel和ByteArrays读取ASCII文件

我有以下代码: String inputFile = “somefile.txt”; FileInputStream in = new FileInputStream(inputFile); FileChannel ch = in.getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(BUFSIZE); // BUFSIZE = 256 /* read the file into a buffer, 256 bytes at a time */ int rd; while ( (rd = ch.read( buf )) != -1 ) { buf.rewind(); for ( int i = 0; […]

如何检测字节数组中的字符串结尾到字符串转换?

我从socket接收一个字节数组中的字符串,如下所示: [128,5,6,3,45,0,0,0,0,0] 网络协议给出的大小是字符串的总长度(包括零),因此,在我的例子中10。 如果我只是这样做: String myString = new String(myBuffer); 我在字符串5的末尾没有正确的字符。 转换似乎没有检测到字符串caracter(0)的结束。 要获得正确的大小和正确的字符串,我这样做: int sizeLabelTmp = 0; //Iterate over the 10 bit to get the real size of the string for(int j = 0; j<(sizeLabel); j++) { byte charac = datasRec[j]; if(charac == 0) break; sizeLabelTmp ++; } // Create a temp byte array to make […]

将字节数组解码为已用Java压缩的位图

我正在以下列方式压缩位图 Bitmap bmpSig = getMyBitMap(); int size = bmpSig.getWidth() * bmpSig.getHeight(); ByteArrayOutputStream out = new ByteArrayOutputStream(size); bmpSig.compress(Bitmap.CompressFormat.JPEG, 100, out); byte[] bytSig = out.toByteArray(); 然后我尝试从字节数组中在Android ImageView中显示图像。 当我这样做时,我得到的图像是完全黑色的图像。 ImageView myImg = (ImageView) findViewById(R.id.img_view); myImg.setImageBitmap(BitmapFactory.decodeByteArray(bytSig, 0, bytSig.length)); 我假设它是因为我在BitmapFactory.decodeByteArray()之前错过了一个步骤来反转jpeg压缩。 或者我误解了压缩是如何工作的?

C#和Java中的hex到字节数组给出了不同的结果

首先,对于长篇文章感到抱歉,我想包括我的所有想法,以便你们更容易找到我的代码有什么问题。 我想将一个Hex字符串从C#应用程序传输到Java应用程序。 但是,当我在两种语言中将相同的hex值转换为字节数组时,输出是不同的。 例如,相同的hex值给出 [101, 247, 11, 173, 46, 74, 56, 137, 185, 38, 40, 191, 204, 104, 83, 154] 在C#和 [101, -9, 11, -83, 46, 74, 56, -119, -71, 38, 40, -65, -52, 104, 83, -102] 在Java中 以下是我在C#中使用的方法: public static string ByteArrayToHexString(byte[] byteArray) { return BitConverter.ToString(byteArray).Replace(“-“,””); //To convert the whole array } public static […]

将MAC地址字节数组格式化为String

我正在使用此代码来查找计算机的MAC地址。 此代码直接打印MAC地址,但我想将其作为字符串返回。 我完全糊涂了。 请帮忙。 try { InetAddress add = InetAddress.getByName(“10.123.96.102”); NetworkInterface ni1 = NetworkInterface.getByInetAddress(add); if (ni1 != null) { byte[] mac1 = ni1.getHardwareAddress(); if (mac1 != null) { for (int k = 0; k < mac1.length; k++) { System.out.format("%02X%s", mac1[k], (k < mac1.length – 1) ? "-" : ""); } } else { System.out.println("Address doesn't […]

如何将字节数组存储为磁盘上的映像文件?

我有一个Image的字节数组表示。 如何将其作为映像文件保存在磁盘上。 我已经这样做了 OutputStream out = new FileOutputStream(“a.jpg”); out.write(byteArray); out.flush(); out.close(); 但是当我通过双击打开图像时,它不会显示任何图像。

Mysql字节数组存储

我有一个用Java创建的字节数组。 它代表某些文件的内容。 我不确切知道这个数组的最大大小。 它可以是不同的尺寸。 我想将它存储在mysql中。 我应该在mysql中使用什么类型的?

将包含ASCII字符的字节数组转换为String

我有一个字节数组,由我希望转换为String的ASCII字符组成。 例如: byte[] myByteArray = new byte[8]; for (int i=0; i<8; i++) { byte[i] = (byte) ('0' + i); } 循环后,myByteArray应包含字符串“12345678”。 如何将此字符串转换为String变量? 谢谢!