【问题标题】:how to display or use an image received from Android phone on the server如何在服务器上显示或使用从 Android 手机接收到的图像
【发布时间】:2011-12-18 02:24:04
【问题描述】:

我正在将图像从 android 手机发送到处理它的服务器,但现在我对如何在服务器中使用图像感到困惑

我发送图像的安卓手机代码是

                            Log.i("sAMPLE","Info:" );
                //String postURL = HOST_SERVER_URL + HOST_PHOTO_UPLOAD_URI;
                String postURL ="http://10.0.2.2:8080/SimpleServlet/simple-servlet";//server URL
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost postRequest = new HttpPost(postURL);

                ByteArrayBody bab = new ByteArrayBody(imageBytes, "file_name_ignored");
                MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                reqEntity.addPart("source", bab);
                postRequest.setEntity(reqEntity);

                HttpResponse response = httpClient.execute(postRequest); 

而我在服务器中处理图像的代码是这样的

        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
    boolean isMultipart = ServletFileUpload.isMultipartContent(req);
    System.out.println("Before Mutlipart");
    if(!isMultipart)
        throw new ServletException("upload using multipart");

    ServletFileUpload upload = new ServletFileUpload(fif);
    upload.setSizeMax(1024 * 1024 * 10 /* 10 mb */);
    List<FileItem> items;
    try {
        items = upload.parseRequest(req);
    //}// catch (FileUploadException e) {
      //  throw new ServletException(e);
    } catch (FileUploadException e) {
        // TODO Auto-generated catch block
        throw new ServletException(e);
    }

    if(items == null || items.size() == 0)
        throw new ServletException("No items uploaded");

    FileItem item = items.get(0);
    //BufferedImage Img=item.getString();
    System.out.println(item.getContentType());
    byte[]data=item.get();

现在我如何使用字节数组在服务器上显示图像或使用字符串、其他图像等其他内容编辑图像。

【问题讨论】:

  • 我应该如何在 servlet 上使用从手机接收到的数据作为图像,以便我可以编辑它。我收到的图像在 item 变量中
  • 编辑是什么意思?你想裁剪吗?,调整大小?操纵?
  • manipulate..我想给它添加一些文字描述..
  • 只是为了确定;您想通过在其上放置文本来修改图像。例如名称、版权页脚或角落中的类似内容?之后你想用它做什么?存储在磁盘上?将它显示给发布它的用户?
  • 添加一些描述后,我需要将其发送回手机应用程序,该应用程序会将其发布到 Facebook

标签: java android image jakarta-ee servlets


【解决方案1】:

我认为这应该适合你...

http://www.mkyong.com/java/how-to-convert-byte-to-bufferedimage-in-java/

在服务器端(imageInByte 是您的数据[])...

//convert byte array back to BufferedImage
InputStream in = new ByteArrayInputStream(imageInByte);
BufferedImage bImageFromConvert = ImageIO.read(in);

ImageIO.write(bImageFromConvert, "jpg", 
         new File("c:\\image\\mypic_new.jpg")); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多