- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请原谅我的英语不好,我是法国人!
在我的 Android 应用程序中,我必须先调整图片大小并裁剪图库中的图片,然后再将它发送到服务器WITHOUT保存它。
这里是我要发送到服务器的代码:
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
String pathToOurFile = imagePath;
String urlServer = "http://ip/serverApp/upload/transfert.php";
Log.e("UploadImage", urlServer);
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try
{
File file = new File(imagePath);
FileInputStream fileInputStream = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
fileInputStream.read(bytes);
fileInputStream.close();
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
connection.setDoOutput(true);
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
int bufferLength = 1024;
for (int i = 0; i < bytes.length; i += bufferLength) {
int progress = (int)((i / (float) bytes.length) * 100);
publishProgress(progress);
if (bytes.length - i >= bufferLength) {
outputStream.write(bytes, i, bufferLength);
} else {
outputStream.write(bytes, i, bytes.length - i);
}
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
outputStream.close();
outputStream.flush();
InputStream inputStream = connection.getInputStream();
// read the response
inputStream.close();
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.w("Upload image", "Response -> Code:"+serverResponseCode+" Message:"+serverResponseMessage);
return serverResponseCode;
}
catch (Exception ex)
{
ex.printStackTrace();
}
现在我需要编写代码来调整图片大小并裁剪图片,以获得尺寸为 350px/350px 的图片。
你知道我该怎么做吗?
非常感谢。
最佳答案
好的!!!!对于正确的方法,请遵循此代码!
但是:注意,这是一个例子! -> 您不应该在主线程中执行 Internet 请求
为了执行这段代码,函数exec();
应该放入 asyncTask<Object, Object, Object>();
的“doInBackground()”中
startActivityForResult()
和 onActivityResult()
的覆盖应该上 Activity 课
告诉我它是否正确!!!!
private int ACTIVITY_ID_PICK_PHOTO = 42;
private int maxWidth = 350;
private int maxHeight = 350;
private String url = "http://ip/serverApp/upload/transfert.php"
//Call the activity for select photo into the gallery
private void SelectPhoto(){
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, ACTIVITY_ID_PICK_PHOTO);
}
// check the return of the result
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//check th id of the result
if (requestCode == ACTIVITY_ID_PICK_PHOTO)
selectPhotoControl(data);
}
//Working data
private void selectPhotoControl(Intent data) {
//check if any photo is selected
if (data == null)
return;
//get the uri of the picture selected
Uri photoUri = data.getData();
if (photoUri != null) {
//decode the Uri
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(photoUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
//get the uri of the image
String filePath = cursor.getString(columnIndex);
cursor.close();
//get the image in the bitmap and resize the image
Bitmap bp = resize(filePath);
if (bp != null)
postImage(bp, filePath);
}
}
public static HttpResponse postImage(Bitmap bp, String uristr) throws ClientProtocolException, IOException {
//initialization of the postrequest
HttpPost httpPost = new HttpPost(url);
//create the multipart entitiy (if you want send another content)
MultipartEntity entity = new MultipartEntity(
//the boundary for separate the informations
HttpMultipartMode.BROWSER_COMPATIBLE, "------CustomBoundary", null);
if (bp != null) {
//create the bytes array for send the image
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//if you want to compress the image -> write the result into bos
bp.compress(CompressFormat.JPEG, 100, bos);
//get the filename of the image
String filename = uristr.substring(uristr.lastIndexOf("/") + 1,
uristr.length());
//put the picture into the body of this part
FormBodyPart fbp = new FormBodyPart("photo", new ByteArrayBody(
bos.toByteArray(), "image/jpeg", filename));
//add the part to the entity
entity.addPart(fbp);
}
//set the entity into the request
httpPost.setEntity(entity);
//execute the request
return exec(httpPost);
}
protected synchronized static HttpResponse exec(HttpRequestBase base) throws ClientProtocolException, IOException{
if (base != null)
//Execute the request
return mHttpClient.execute(base);
else
return null;
}
private Bitmap resize(String path){
// create the options
BitmapFactory.Options opts = new BitmapFactory.Options();
//just decode the file
opts.inJustDecodeBounds = true;
Bitmap bp = BitmapFactory.decodeFile(path, opts);
//get the original size
int orignalHeight = opts.outHeight;
int orignalWidth = opts.outWidth;
//initialization of the scale
int resizeScale = 1;
//get the good scale
if ( orignalWidth > maxWidth || orignalHeight > maxHeight ) {
final int heightRatio = Math.round((float) orignalHeight / (float) maxHeight);
final int widthRatio = Math.round((float) orignalWidth / (float) maxWidth);
resizeScale = heightRatio < widthRatio ? heightRatio : widthRatio;
}
//put the scale instruction (1 -> scale to (1/1); 8-> scale to 1/8)
opts.inSampleSize = resizeScale;
opts.inJustDecodeBounds = false;
//get the futur size of the bitmap
int bmSize = (orignalWidth / resizeScale) * (orignalHeight / resizeScale) * 4;
//check if it's possible to store into the vm java the picture
if ( Runtime.getRuntime().freeMemory() > bmSize ) {
//decode the file
bp = BitmapFactory.decodeFile(path, opts);
} else
return null;
return bp;
}
关于Android - 在不保存的情况下发送前调整大小和裁剪图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16168627/
是否有可能(在 cmd 批处理 ffmpeg 中)拍摄宽度已知(1920px)但高度未知的图像,如果超过,则将高度裁剪为特定的值?基本上是最大高度裁剪。 我玩过缩放和裁剪,但我无法得到我需要的结果。任
我有两个 SpatialPolygonsDataFrame文件:dat1、dat2 extent(dat1) class : Extent xmin : -180 xmax
我在 TensorFlow 上实现了全卷积网络。它使用编码器-解码器结构。训练时,我始终使用相同的图像大小(224x224,使用随机裁剪)并且一切正常。 在干扰阶段,我想一次预测一张图像,因为我想使用
我在 TensorFlow 上实现了全卷积网络。它使用编码器-解码器结构。训练时,我始终使用相同的图像大小(224x224,使用随机裁剪)并且一切正常。 在干扰阶段,我想一次预测一张图像,因为我想使用
我有一个需要裁剪的 View 。我有 4 个 View 显示在主视图上查看的视频 subview 。由于视频比例,我需要裁剪使视频成为正方形而不是矩形的 View 。这是我的代码: - (void)v
我正在构建一个使用 Parse 作为我的后端的网络应用程序。 部分注册过程涉及用户上传和裁剪图片,然后我将其传递到我的数据库(图片是用户个人资料的一部分,类似于您在 Twitter 上所做的)。 我已
我正在制作一个基于立方体的游戏(一切都是立方体),目前正在尝试通过不在视野之外绘制东西来优化它。 以下内容仅适用于 x 和 y 平面,稍后我会担心 z ......所以现在只进行侧面裁剪。 我知道我自
我正在尝试在 iOS 上实现单指图像缩放/裁剪。类似于柯比·特纳的单指旋转。我正在寻找现有的库,或者如果您可以帮助我处理代码本身,那就太好了。 最佳答案 我不太清楚你所说的一指裁剪是什么意思,但我为
从这里: http://www.kylejlarson.com/blog/2011/how-to-create-pie-charts-with-css3/ .pieContainer
我已经设置了一个 SKScene 用作 SKReferenceNode。雪橇是一个 SKSpriteNode,在引用节点场景中定义了一个自定义类,所有的狗都是雪橇 Sprite 的 child 。自定
我有一个库,其中包含一些图像处理算法,包括感兴趣区域(裁剪)算法。使用 GCC 编译时,自动矢量化器会加速很多代码,但会降低 Crop 算法的性能。是否有标记某个循环以被矢量化器忽略的方法,或者是否有
代码位于 http://jsfiddle.net/rSSXu/ Child #parent { margin-left:auto; margin-right:auto;
我搜索了很多以删除不需要的空间,但找不到。我只找到可用于删除黑白背景空间的链接。但我的背景图片可以是任何东西。所以,如果我有这些图片, 我如何提取我需要的图像部分。例如, 最佳答案 这是我对你的问题的
我正在尝试将 CMSampleBufferRef 中的图像裁剪为特定大小。我正在执行 5 个步骤 - 1. 从 SampleBuffer 获取 PixelBuffer 2. 将 PixelBuffer
我读到它是自动的,但在我的案例中似乎没有发生。使用 UIImagePickerController 并将 allowsEditing 设置为 YES 我得到了带有裁剪方形叠加层的编辑 View ,但是
我正在寻找一种高效的方法来裁剪二维数组。考虑这个例子: 我有一个构成 100x100 网格的二维数组。我只想返回其中的一部分,60x60。这是一个“a”方法的示例,但我正在寻找指向执行此操作的最高性能
我有一个接受 UIImage 的类,用它初始化一个 CIImage,如下所示: workingImage = CIImage.init(image: baseImage!) 然后使用图像以 3x3 的
我正在尝试显示来自 mysql 数据库的图像。有些图像显示正确,但有些图像在底部显示为剪切/裁剪,裁剪部分仅显示为空白,当它成为图像的一部分时,您真的无法摆脱。 CSS 无法解决这个问题,使用 ima
我有个问题。我有什么理由不应该使用这个 Intent: Intent intent = new Intent("com.android.camera.action.CROP"); 为了裁剪我刚刚拍摄的
我有一些代码可以调整图像大小,因此我可以获得图像中心的缩放 block - 我使用它来获取 UIImage 并返回一个小的方形表示图片,类似于在照片应用程序的相册 View 中看到的内容。 (我知道我
我是一名优秀的程序员,十分优秀!