我有两个问题,我想你们可以解决!
1> 一个是通过PHP上传图像到服务器。 实际上我可以将图像上传到服务器。 但是当我压缩图像后将图像发送到服务器时,图像质量太差了! 我在Java上使用了ByteStreamArray和编码系统。 所以,如果你们有其他解决方案或任何其他建议,请为我答复!
2 - ; 另一个是控制图像。 正如你在其他应用程序(Facebook,Instagram等...)上看到的,我想控制我选择的图像。尺寸控制或旋转控制...... 我寻找每一个地方,但我找不到解决方案
感谢您阅读本文,我期待您的回复〜
答案 0 :(得分:1)
通过此代码控制图像尺寸和图像质量
Uri uri = Uri.fromFile(new File(imagepath));
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(mcontext.getContentResolver()
.openInputStream(uri), null, o);
Bitmap bitmap = BitmapFactory.decodeStream(mcontext
.getContentResolver().openInputStream(uri), null,
scalingBitmap(o));
int orientation = 0;
try {
ExifInterface ei = new ExifInterface(uri.getPath());
orientation = ei.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
Log.d("image", "orientation is " + orientation);
bitmap = rotateImageOrientation(orientation, bitmap);
} catch (Exception e) {
e.printStackTrace();
}
Log.d("image", "bitmap width " + bitmap.getWidth() + " height "
+ bitmap.getHeight());
//在服务器上传
byte[] data = null;
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(uploadPhotoUrl);
HttpParams mHttpParams = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(mHttpParams, 150000);
httpPost.setParams(mHttpParams);
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE); ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 90, bos);
data = bos.toByteArray();
entity.addPart("somefile", new ByteArrayBody(data, "temp.png")); httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
InputStream instream = response.getEntity().getContent();
//创建此方法以查找位图的缩放因子
BitmapFactory.Options scalingBitmap(BitmapFactory.Options options) {
int width_tmp = options.outWidth, height_tmp = options.outHeight;
Log.d("image", "image width" + width_tmp + "image height" + height_tmp);
int scale = 1;
int REQUIRED_SIZE = 200;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return o2;
}
//创建此方法以控制位图的旋转
Bitmap rotateImageOrientation(int angle, Bitmap bitmap) {
Matrix matrix = new Matrix();
switch (angle) {
case ExifInterface.ORIENTATION_NORMAL:
System.out.println("ORIENTATION_NORMAL" + angle);
return bitmap;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
System.out.println("ORIENTATION_FLIP_HORIZONTAL" + angle);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
default:
return bitmap;
}
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
return bitmap;
}
答案 1 :(得分:0)
1&gt;确保在上传之前不缩放图片。
2 - ;参考这些库并结合您实际需要的功能
https://github.com/edmodo/cropper