拍照上传的图片被旋转问题

来源:互联网 发布:淘宝信誉查询软件 编辑:程序博客网 时间:2024/04/30 12:21

在做项目过程中,要给后台传图片并显示在用户端的界面上,下次登录后会读取上传到后台的信息并显示;

也就是http://blog.csdn.net/gly742279097/article/details/41700505这一块

遇到两个问题:

1、拍照后显示的图片被旋转,所以上传到后台后在显示也是被旋转了

解决方法:

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {Bitmap photo = null;if (requestCode == 999) {if (file != null && file.exists()) {BitmapFactory.Options options = new BitmapFactory.Options();options.inSampleSize = 2;path = file.getPath();photo = BitmapUtils.opBigBitmap(path, ChangeInfoActivity.this, 70, 70);}} else if (requestCode == 888) {if (data == null) {return;}Uri uri = data.getData();String[] proj = { MediaStore.Images.Media.DATA };Cursor cursor = this.managedQuery(uri, proj, null, null, null);int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);cursor.moveToFirst();path = cursor.getString(column_index);sp.edit().putString("person_path_location", path).commit();if (photo != null && !photo.isRecycled())// 如果不释放的话,不断取图片,将会内存不够{photo.recycle();photo = null;}System.gc();photo = BitmapUtils.opBigBitmap(path, ChangeInfoActivity.this, 70, 70);}Log.d("ee", "高:" + photo.getHeight() + "  " + "宽:" + photo.getWidth());Log.d("ee", "bitmap所占字节:" + photo.getRowBytes() * photo.getHeight() + "");/** * 获取图片的旋转角度,有些系统把拍照的图片旋转了,有的没有旋转 */file = new File(path);int degree = BitmapUtils.readPictureDegree(file.getAbsolutePath());Log.d("ee", "角度degree:" + degree);/** * 把图片旋转为正的方向 */photo = BitmapUtils.rotaingImageView(degree, photo);file = BitmapUtils.bitmapToFile(photo, path);//上传图片给后台的参数,写在这里保证上传给后台的图片以旋转正常imageView.setImageBitmap(photo);}

/** * 读取图片属性:旋转的角度 *  * @param path *            图片绝对路径 * @return degree旋转的角度 */public static int readPictureDegree(String path) {int degree = 0;try {ExifInterface exifInterface = new ExifInterface(path);int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);switch (orientation) {case ExifInterface.ORIENTATION_ROTATE_90:degree = 90;break;case ExifInterface.ORIENTATION_ROTATE_180:degree = 180;break;case ExifInterface.ORIENTATION_ROTATE_270:degree = 270;break;}} catch (IOException e) {e.printStackTrace();}return degree;}

/* * 旋转图片 *  * @param angle *  * @param bitmap *  * @return Bitmap */public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {// 旋转图片 动作Matrix matrix = new Matrix();;matrix.postRotate(angle);System.out.println("angle2=" + angle);// 创建新的图片Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);return resizedBitmap;}

2、因为信息可被修改再次提交给后台,但还想用上次传给服务器的图片,为确保本地已不存在该图片,就要在本地保存后台返回给的图片路径,再以file类型的参数传给后台就要转换

解决方法:

private void saveIntro() {sp = this.getSharedPreferences("login", Context.MODE_PRIVATE);String url = Contants.CHANGE_PERSON_URL;RequestParams params = new RequestParams(); // 绑定参数params.put("webName", sp.getString("username", null));params.put("token", sp.getString("token", null));params.put("intro", personExplan.getText().toString().trim());try {if (file == null) {params.put("file", BitmapUtils.bitmapToFile(BitmapUtils.returnBitMap(sp.getString("changeinfo_iconPath", null)), getFilesDir() + "/image_file.png"));} else {params.put("file", file);}} catch (FileNotFoundException e1) {e1.printStackTrace();}RequstClient.post(url, params, new LoadResponseLoginouthandler(ChangeInfoActivity.this, new LoadDatahandler(ChangeInfoActivity.this) {@Overridepublic void onStart() {super.onStart();}@Overridepublic void onSuccess(String data) {super.onSuccess(data);try {JSONObject object = new JSONObject(data);boolean success = object.getBoolean("success");String msg = object.getString("msg");if (success) {showShort(ChangeInfoActivity.this, "保存成功");ChangeInfoActivity.this.finish();} else {showShort(ChangeInfoActivity.this, "保存失败");}} catch (JSONException e) {e.printStackTrace();}}@Overridepublic void onFailure(String error, String message) {super.onFailure(error, message);}@Overridepublic void onFinish() {super.onFinish();}}));}

主要是这句

BitmapUtils.bitmapToFile(BitmapUtils.returnBitMap(sp.getString("changeinfo_iconPath", null)), getFilesDir() + "/image_file.png")
sp.getString("changeinfo_iconPath", null)是http://image.baidu.com/channel?c=%E5%AE%A0%E7%89%A9#%E5%AE%A0%E7%89%A9.jpg这种形式的网络图片路径
<pre name="code" class="java">/** * string转成bitmap *  * @param st */public static Bitmap returnBitMap(String url) {URL myFileUrl = null;Bitmap bitmap = null;try {myFileUrl = new URL(url);} catch (MalformedURLException e) {e.printStackTrace();}try {HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();conn.setDoInput(true);conn.connect();InputStream is = conn.getInputStream();bitmap = BitmapFactory.decodeStream(is);is.close();} catch (IOException e) {e.printStackTrace();}return bitmap;}


/** * 将bitmap转换成file *  * @param bitmap * @param nfile * @return */public static File bitmapToFile(Bitmap bitmap, String nfile) {ByteArrayOutputStream os = new ByteArrayOutputStream();bitmap.compress(CompressFormat.PNG, 100, os);byte[] bytes = os.toByteArray();BufferedOutputStream stream = null;File file = null;try {file = new File(nfile);FileOutputStream fstream = new FileOutputStream(file);stream = new BufferedOutputStream(fstream);stream.write(bytes);} catch (Exception e) {e.printStackTrace();} finally {if (stream != null) {try {stream.close();} catch (IOException e1) {e1.printStackTrace();}}}return file;}



0 0
原创粉丝点击