Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在上传到服务器之前限制BlackBerry上的图像大小_Blackberry_Java Me_Image Size - Fatal编程技术网

如何在上传到服务器之前限制BlackBerry上的图像大小

如何在上传到服务器之前限制BlackBerry上的图像大小,blackberry,java-me,image-size,Blackberry,Java Me,Image Size,我想增加上传图像大小的限制(当前图像上传到服务器) 当任何人从SD卡获取图像或从相机捕获图像时,我希望向用户显示一条消息,说明它上载了最大文件大小,即500kb,或者我可以将图像调整为较小的大小。例如,将1mb图像大小调整为400-500kb(如Facebook) 下面是我从SD卡获取图像或从相机捕获图像后实现的示例代码 FileConnection file = (FileConnection)Connector.open(url); if(file.exists()) { t

我想增加上传图像大小的限制(当前图像上传到服务器)

当任何人从SD卡获取图像或从相机捕获图像时,我希望向用户显示一条消息,说明它上载了最大文件大小,即500kb,或者我可以将图像调整为较小的大小。例如,将1mb图像大小调整为400-500kb(如Facebook)

下面是我从SD卡获取图像或从相机捕获图像后实现的示例代码

 FileConnection file = (FileConnection)Connector.open(url);
 if(file.exists())
 {
     try{
         String fileName = url .substring(url.lastIndexOf('/') + 1);
         //String fileName = url ;
         Dialog.alert("fileName  " + fileName);
         InputStream inputStream = file.openInputStream();

         ByteArrayOutputStream bos=new ByteArrayOutputStream();
         int buffersize=1024;
         byte[] buffer=new byte[buffersize];
         int length=0;
         while((length=inputStream.read(buffer))!=-1)
         {
             bos.write(buffer,0,length);
         }
         byte[] imagedata=bos.toByteArray();
         Dialog.alert("Url  " + Url  + " Image Data Byte " + imagedata);
         HttpConnection conn = (HttpConnection) Connector.open(Url, Connector.READ_WRITE);
         conn.setRequestMethod(HttpConnection.POST);
         String boundary = "Some_Unique_Text_Also_Alphanumeric";
         conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,                    
             HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA
                                 + ";boundary=" + boundary);

             conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH,
                         String.valueOf(imagedata.length));
         conn.setRequestProperty("x-rim-transcode-content", "none");

         ByteArrayOutputStream out = new ByteArrayOutputStream();
         OutputStream finalOut = conn.openOutputStream();

         String newLine = "\r\n";
         out.write(newLine.getBytes());
         out.write("--".getBytes());
         out.write(boundary.getBytes());
         out.write(newLine.getBytes());
         String contDisp = "Content-Disposition:form-data;name=\"image\";fileName=\"Image.jpg\"";
         String contEnc = "Content-Transfer-Encoding: binary";
         String contentType = "Content-Type:image/jpeg";
         out.write(contDisp.getBytes());
         out.write(newLine.getBytes());
         out.write(contentType.getBytes());
         out.write(newLine.getBytes());
         out.write(contEnc.getBytes());
         out.write(newLine.getBytes());
         out.write(newLine.getBytes());
         out.write(imagedata);
         out.write(newLine.getBytes());
         out.write("--".getBytes());
         out.write(boundary.getBytes());
         out.write("--".getBytes());
         out.write(newLine.getBytes());
         finalOut.write(out.toByteArray());

         out.flush();
         out.close();

         finalOut.flush();
         finalOut.close();
         InputStream instream=conn.openInputStream();
         int ch=0;
         StringBuffer buffesr=new StringBuffer();
         while((ch=instream.read())!=-1)
         {
             buffesr.append((char)ch);
             Dialog.alert("Uploaded");
         }
     }
     catch (Exception e) {

         Dialog.alert("Exception " + e);
     }   
 }

有什么帮助吗?

问题在于,对于相机图片,您无法预测物理图像大小(像素宽度x高度)将对应于字节中的特定大小

如果您对可以上载的大小(以字节为单位)有一个固定的硬限制,您可能需要执行以下操作:

FileConnection file;
InputStream inputStream;

try {
    file = (FileConnection) Connector.open(url);  // JPG file:// URL
    if (file.exists())
    {
        inputStream = file.openInputStream();           
        byte[] data = IOUtilities.streamToBytes(inputStream);

        Bitmap original = Bitmap.createBitmapFromBytes(data, 0, data.length, 1);

        Bitmap scaledImg = new Bitmap(640, 480);    // maximum width and height
        original.scaleInto(scaledImg, 
                           Bitmap.FILTER_LANCZOS,   /* LANCZOS is for best quality */
                           Bitmap.SCALE_TO_FIT);  

        // http://stackoverflow.com/a/14147236/119114
        int jpegQuality = 85;
        EncodedImage encodedImg = JPEGEncodedImage.encode(scaledImg, jpegQuality);
        byte[] imageData = encodedImg.getData(); 
        // TODO: send imageData as you already were       
    }
} catch (Exception e) {
    // log exception
} finally {
    try {
        if (file != null) {
            file.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
    } catch (IOException ioe) {
        // nothing can be done here
    }
}
  • 尝试一些图像,并找到一个大致的图像大小(宽度x高度),它将生成一个JPG文件,该文件通常符合您的400-500KB限制

  • 在应用程序中,将相机图像调整为该物理大小(宽度x高度,以像素为单位)

  • 检查新JPG数据的大小,看看它是否符合您的限制

  • 如果它不适合,则必须将原始图像重新缩放为较小的尺寸

正如你所看到的,这并不是那么简单。我见过的大多数服务器(例如)都会告诉您图像可以达到的最大物理大小(例如960像素作为最宽的大小…宽度或高度)。如果这对您的服务器来说足够好,那么在BlackBerry客户端编写代码就容易多了

限制为固定的像素宽度和高度 您可以使用以下内容:

FileConnection file;
InputStream inputStream;

try {
    file = (FileConnection) Connector.open(url);  // JPG file:// URL
    if (file.exists())
    {
        inputStream = file.openInputStream();           
        byte[] data = IOUtilities.streamToBytes(inputStream);

        Bitmap original = Bitmap.createBitmapFromBytes(data, 0, data.length, 1);

        Bitmap scaledImg = new Bitmap(640, 480);    // maximum width and height
        original.scaleInto(scaledImg, 
                           Bitmap.FILTER_LANCZOS,   /* LANCZOS is for best quality */
                           Bitmap.SCALE_TO_FIT);  

        // http://stackoverflow.com/a/14147236/119114
        int jpegQuality = 85;
        EncodedImage encodedImg = JPEGEncodedImage.encode(scaledImg, jpegQuality);
        byte[] imageData = encodedImg.getData(); 
        // TODO: send imageData as you already were       
    }
} catch (Exception e) {
    // log exception
} finally {
    try {
        if (file != null) {
            file.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
    } catch (IOException ioe) {
        // nothing can be done here
    }
}
当然,您应该在后台线程上执行所有这些工作。一旦您知道最终图像大小,如果您确实想知道,您可以通过以下方式通知用户:

final uploadSizeKb = imageData.length / 1024;
UiApplication.getUiApplication().invokeLater(new Runnable() {
   public void run() {
      Dialog.alert(uploadSizeKb + "KB uploaded to server");
   }
});
进一步优化 正如您可能知道的那样,您可以使用此算法调整一些事情:

  • 在尝试缩放图像文件之前,可以通过检查图像文件是否已经足够小来进行优化。(检查
    file.fileSize()

  • 您可以通过使用
    Bitmap.FILTER\u biliner
    Bitmap.FILTER\u BOX
    而不是
    Bitmap.FILTER\u LANCZOS
    来加速图像缩放

  • 当您转换回JPEG进行上传时,可以将JPEG质量因子从85更改为85

  • 您可能需要检查图像方向,以避免在使用
    scale\u to\u FIT
    进行缩放时浪费太多空间。如果相机图像方向错误,只需切换位图的宽度和高度(例如640x480->480x640)

  • 实际上,您可以跳过几个步骤,使用
    createBitmapFromBytes()
    在读取图像文件时直接缩放。最后一个参数是比例参数。不幸的是,由于照片都是不同的,因此也很难选择一个能起作用的比例。正如我所说的,更常见的情况是服务器只指定最大图像大小(以像素为单位)

操作系统<5.0支持
如果操作系统5.0中没有图像缩放API可用,.

您想在客户端(blackberry)还是服务器端设置限制?blackberry端,当客户端从SD卡或照相机图像中选择图像时,您需要支持的blackberry操作系统的最低要求是什么?5.0?黑莓操作系统6.0及以上。好消息。所以,也许你可以使用比640x480稍大一点的尺寸。你的选择。