系统裁剪图片后不清晰

十三邵 2015-08-21 04:24:35
裁剪图片
Intent intent = new Intent();
intent.setAction("com.android.camera.action.CROP");
intent.setDataAndType(originalUri, "image/*");// mUri是已经选择的图片Uri
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);// 裁剪框比例
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 150);// 输出图片大小
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
ModifyPersonalActivity.this.startActivityForResult(intent, 1);

接收裁剪图片
Bitmap bmap = data.getParcelableExtra("data");

但是得到的图片特别不清晰,不知道是什么问题!
...全文
931 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qjjhz130 2015-08-25
  • 打赏
  • 举报
回复
安卓内存是个瓶颈
  • 打赏
  • 举报
回复
我也遇到过这样的问题,是这样解决的: 拿到剪裁的图片,转化为bitmap存到本地,这样会好点。
	/**
	 * 处理磁盘图片,如果大于500*500 则缩小图片
	 */
	private void handlePicture(final Uri uri) {
		Rect rect = mBitmapUtils.getBitmapBounds(uri);
		int area = rect.right * rect.bottom;
		if (area > 500 * 500) {
			Bitmap bitmap = mBitmapUtils.getBitmap(uri, 500, 500);
			mBitmapUtils.saveBitmap(bitmap, Environment.getExternalStorageDirectory().getAbsolutePath(), TEMP_PHOTO_FILE, Bitmap.CompressFormat.JPEG);
		}
	}
    public Rect getBitmapBounds(Uri uri) {
        Rect bounds = new Rect();
        InputStream is = null;

        try {
            is = context.getContentResolver().openInputStream(uri);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, options);

            bounds.right = options.outWidth;
            bounds.bottom = options.outHeight;
            Log.i(TAG, "options.outWidth="+options.outWidth+" , "+"options.outHeight="+options.outHeight);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeStream(is);
        }

        return bounds;
    }
    /**
     * Saves the bitmap by given directory, filename, and format; if the directory is given null,
     * then saves it under the cache directory.
     */
    public File saveBitmap(
            Bitmap bitmap, String directory, String filename, CompressFormat format) {

        if (directory == null) {
            directory = context.getCacheDir().getAbsolutePath();
        } else {
            // Check if the given directory exists or try to create it.
            File file = new File(directory);
            if (!file.isDirectory() && !file.mkdirs()) {
                return null;
            }
        }

        File file = null;
        OutputStream os = null;

        try {
            filename = (format == CompressFormat.PNG) ? filename + ".png" : filename + ".jpg";
            file = new File(directory, filename);
            os = new FileOutputStream(file);
            bitmap.compress(format, DEFAULT_COMPRESS_QUALITY, os);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeStream(os);
        }
        return file;
    }
十三邵 2015-08-24
  • 打赏
  • 举报
回复
大神指点一下啊。。。
一袭白衣 2015-08-24
  • 打赏
  • 举报
回复
不要直接返回bitmap,android存一个大的bitmap是非常非常消耗内存的,并且非常容易出现out of memory 总体思路和2楼类似, 裁剪图片,保存到本地,返回 图片的 uri 具体细节可以参考 http://ryanhoo.github.io/blog/2014/06/03/the-ultimate-approach-to-crop-photos-on-android-2/ 有上中下3篇,看完应该会对bitmap裁剪有个比较好的理解
十三邵 2015-08-21
  • 打赏
  • 举报
回复
把intent.putExtra("outputX", 150);和intent.putExtra("outputY", 150);设置大些图片就清晰些,但跟直接被截的图来比还是模糊,而且到350的时候就会报错了,不知道这个最大值是怎么来的

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧