从内存中打开 PNG 文件并通过 TCP 套接字发送
Opening a PNG File from Memory and Send Through TCP Sockets
我想将图像从我的 android 设备发送到计算机 / raspberry pi 运行ning python。我已经使用以下代码从我的 windows 笔记本电脑(运行 宁 java 客户端)实现了通信。
BufferedImage image = ImageIO.read (new File("C:/Users/****/OneDrive/Pictures/Om-nom.png"));
s = new Socket("192.168.2.69",8000);
ImageIO.write(image, "png", s.getOutputStream());
s.close();
但是,android 不支持 BufferedImage 库 (java.awt.image)。我如何才能在 android studio 中实现与 android 设备上的 运行 类似的功能,即将 PNG 文件从我设备上的内存读取到字节缓冲区中,然后可以将其发送到服务器PC.
Note: The location of my file would be similar to the following /storage/emulated/0/Downloads/frog-face_1f438.png
要获取图像,您可以这样做:
// TODO check sdcard is available
File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File photoPath = new File(directory, "temp.jpg");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
final Bitmap bitmap = BitmapFactory.decodeFile(photoPath.getAbsolutePath(), options);
要发送到您的服务器,您可以使用 okHttp 客户端或您使用哪种协议与您的服务器通信。
更新:
在清单中你应该指出这个权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
如果您 运行 在 android 6 及更高版本上编写代码,您应该处理 运行时间许可 (WRITE_EXTERNAL_STORAGE):
// oncreate method or whereever you want to call your code
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
STATUS_CODE);
} else {
// TODO run your code
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case STATUS_CODE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// TODO run your code
} else {
// TODO show warning
}
感谢 Gleichmut 的帮助。如果有人在他们的应用程序中需要类似的功能(通过 TCP 套接字发送图像),我已经设法编写了一些可用的工作代码 GitHub。
GitHub 存储库在这里:https://github.com/Lime-Parallelogram/Sending-Images-Through-Sockets-Android-to-Python-
Note: My code on GitHub includes a python server and a java client designed to run on android - written in Android-Studio
我想将图像从我的 android 设备发送到计算机 / raspberry pi 运行ning python。我已经使用以下代码从我的 windows 笔记本电脑(运行 宁 java 客户端)实现了通信。
BufferedImage image = ImageIO.read (new File("C:/Users/****/OneDrive/Pictures/Om-nom.png"));
s = new Socket("192.168.2.69",8000);
ImageIO.write(image, "png", s.getOutputStream());
s.close();
但是,android 不支持 BufferedImage 库 (java.awt.image)。我如何才能在 android studio 中实现与 android 设备上的 运行 类似的功能,即将 PNG 文件从我设备上的内存读取到字节缓冲区中,然后可以将其发送到服务器PC.
Note: The location of my file would be similar to the following /storage/emulated/0/Downloads/frog-face_1f438.png
要获取图像,您可以这样做:
// TODO check sdcard is available
File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File photoPath = new File(directory, "temp.jpg");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
final Bitmap bitmap = BitmapFactory.decodeFile(photoPath.getAbsolutePath(), options);
要发送到您的服务器,您可以使用 okHttp 客户端或您使用哪种协议与您的服务器通信。
更新: 在清单中你应该指出这个权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
如果您 运行 在 android 6 及更高版本上编写代码,您应该处理 运行时间许可 (WRITE_EXTERNAL_STORAGE):
// oncreate method or whereever you want to call your code
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
STATUS_CODE);
} else {
// TODO run your code
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case STATUS_CODE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// TODO run your code
} else {
// TODO show warning
}
感谢 Gleichmut 的帮助。如果有人在他们的应用程序中需要类似的功能(通过 TCP 套接字发送图像),我已经设法编写了一些可用的工作代码 GitHub。
GitHub 存储库在这里:https://github.com/Lime-Parallelogram/Sending-Images-Through-Sockets-Android-to-Python-
Note: My code on GitHub includes a python server and a java client designed to run on android - written in Android-Studio