我正在为Android上的图像处理开发一个应用程序,但我仍然在编写用于保存图像的代码.
这是我使用的方法:
private void saveImageToExternalStorage(Bitmap finalBitmap) {
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
// Tell the media scanner about the new file so that it is …Run Code Online (Sandbox Code Playgroud) 我想从RenderScript中的一个Allocation访问更多元素.我们来自Google的示例代码:
uchar4 __attribute__((kernel)) invert(uchar4 in, uint32_t x, uint32_t y) {
uchar4 out = in;
out.r = 255 - in.r;
out.g = 255 - in.g;
out.b = 255 - in.b;
return out;
}
Run Code Online (Sandbox Code Playgroud)
它需要一个uchar4,谁是分配的一个元素.是否可以访问和操作多个元素?就像展开一个循环一样,例如,来自Bitmap的8个像素.
谢谢.
我必须在多线程 Linux TCP 服务器中实现一个套接字,该服务器在超时后通过使用 SO_RCVTIMEO 和 SO_SNDTIMEO 设置的两个 setsockopt 断开连接。
这是代码:
if(setsockopt(receive_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout))<0){
printf("errore sock option rcvtimeo\n");
exit(EXIT_FAILURE);
}
if(setsockopt(receive_sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout))<0){
printf("errore sock option sndtimeo\n");
exit(EXIT_FAILURE);
}
Run Code Online (Sandbox Code Playgroud)
在生成与此套接字关联的线程后,服务器通过以下函数保持在接收状态:
int receive_message(int descriptor, char* buffer){
memset(buffer,0,sizeof(buffer));
int ret;
while((ret= recv(descriptor, buffer, BUFFER_SIZE-1, MSG_NOSIGNAL))<=0){
if (errno == EWOULDBLOCK || errno == EPIPE){
//stuff...
pthread_exit(NULL);
}
else if (errno == EINTR) continue;
else exit(EXIT_FAILURE);
}
buffer[ret]= '\0';
return ret;
}
Run Code Online (Sandbox Code Playgroud)
如果客户端保持在线但没有响应,它将被 if (errno == EWOULDBLOCK || errno == …