Kab*_*mro 3 blackberry java-me image-size
我想对上传的图像大小添加限制(当前图像上传到服务器).
当有人从SD卡获取的图像或从相机捕获的图像,我想显示消息的用户,它最大上传文件大小 - 即500KB或我可以将图像尺寸调整到更小的尺寸.例如,1mb图像大小调整为400-500kb(如Facebook).
以下是从SDcard获取图像或从Camera获取图像后实现的示例代码.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助?
问题是,对于相机图片,您无法预测物理图像大小(像素宽度x高度)将对应于特定大小(以字节为单位).
如果您对可以上载的大小(以字节为单位)有严格的固定限制,则可能需要执行以下操作:
尝试一些图像,并找到一个近似的图像大小(宽x高),将生成一个JPG文件,通常适合您的400-500KB限制
在您的应用中,将相机图像的大小调整为该物理大小(宽x高,以像素为单位)
检查新JPG数据的大小,看看它是否符合您的限制
如果它不适合,那么你就必须对原始图像重新缩放为更小的尺寸
如您所见,这并不是那么简单.我见过的大多数服务器(例如Facebook)会告诉您图像的最大物理尺寸(例如,最宽的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
}
}
Run Code Online (Sandbox Code Playgroud)
当然,您应该在后台线程上执行所有这些工作.一旦知道最终图像大小,如果您真的想要,您可以通过以下方式通知用户:
final uploadSizeKb = imageData.length / 1024;
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert(uploadSizeKb + "KB uploaded to server");
}
});
Run Code Online (Sandbox Code Playgroud)
您可以说,您可以使用此算法调整的内容:
在尝试缩放之前,您可以通过检查图像文件是否已经足够小来进行优化.(检查file.fileSize())
您可以使用Bitmap.FILTER_BILINEAR或Bitmap.FILTER_BOX代替来加速图像缩放Bitmap.FILTER_LANCZOS.
转换回JPEG进行上传时,可以将JPEG品质因数从85更改为85
您可能需要检查图像方向,以避免在缩放时浪费太多空间SCALE_TO_FIT.如果摄像机图像方向错误,只需切换scaledImg位图宽度和高度(例如640x480 - > 480x640)
实际上,您可以跳过几个步骤,并在读取图像文件时直接缩放createBitmapFromBytes().最后一个参数是比例参数.不幸的是,由于照片都是不同的,因此也很难选择一种可行的比例.正如我所说,服务器更简单地指定最大图像大小(以像素为单位).
如果您没有OS 5.0中的图像缩放API,那么这个较旧的工具包可能很有用.
| 归档时间: |
|
| 查看次数: |
570 次 |
| 最近记录: |