- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
使用以下方法打开图像选择器后:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "choose image", 1);
从选定的 Uri 获取位图的推荐方法是使用以下代码:
InputStream input = cxt.getContentResolver().openInputStream(fileUri);
Bitmap bmp = BitmapFactory.decodeStream(input);
但是这种方法不会自动旋转用相机以人像模式拍摄的图像。
如何仅使用 Uri 获取位图方向? (考虑到 Uri 可能来自 Google Drive 或其他可能无法转化为磁盘上真实文件的位置)
最佳答案
我用这个类:src是选中图片的字符串,bitmap是从图片中提取出来的位图。
public class ExifUtils {
public static Bitmap rotateBitmap(String src, Bitmap bitmap) {
try {
int orientation = getExifOrientation(src);
if (orientation == 1) {
return bitmap;
}
Matrix matrix = new Matrix();
switch (orientation) {
case 2:
matrix.setScale(-1, 1);
break;
case 3:
matrix.setRotate(180);
break;
case 4:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case 5:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case 6:
matrix.setRotate(90);
break;
case 7:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case 8:
matrix.setRotate(-90);
break;
default:
return bitmap;
}
try {
Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return oriented;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return bitmap;
}
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
private static int getExifOrientation(String src) throws IOException {
int orientation = 1;
try {
/**
* if your are targeting only api level >= 5 ExifInterface exif =
* new ExifInterface(src); orientation =
* exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
*/
if (Build.VERSION.SDK_INT >= 5) {
Class<?> exifClass = Class
.forName("android.media.ExifInterface");
Constructor<?> exifConstructor = exifClass
.getConstructor(new Class[] { String.class });
Object exifInstance = exifConstructor
.newInstance(new Object[] { src });
Method getAttributeInt = exifClass.getMethod("getAttributeInt",
new Class[] { String.class, int.class });
java.lang.reflect.Field tagOrientationField = exifClass
.getField("TAG_ORIENTATION");
String tagOrientation = (String) tagOrientationField.get(null);
orientation = (Integer) getAttributeInt.invoke(exifInstance,
new Object[] { tagOrientation, 1 });
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return orientation;
}
}
在需要上传图片的部分。这是如何使用它:
ExifUtils.rotateBitmap(path, decodeSampledBitmap(file, 400, 400)));
decodeSampledBitmap 是:
public Bitmap decodeSampledBitmap(File res, int reqWidth, int reqHeight) {
if (res != null) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
FileInputStream stream2 = new FileInputStream(res);
BitmapFactory.decodeStream(stream2, null, options);
stream2.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Calculate inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
o2.inJustDecodeBounds = false;
FileInputStream stream = null;
try {
stream = new FileInputStream(res);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, o2);
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
} else
return null;
}
public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
如果您要上传位图,还有一种方法可以从位图中检索字节。此方法还压缩了位图大小:
public byte[] getBytesFromBitmap(Bitmap bitmap) {
if (bitmap != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 75, stream);
return stream.toByteArray();
} else
return null;
}
关于Android:从 Uri 获取位图方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31769438/
使用 ruby 1.9.2-p290。我在尝试解析如下 URI 时遇到问题: require 'uri' my_uri = "http://www.anyserver.com/getdata?anyp
根据 http://tools.ietf.org/html/rfc3986和 http://en.wikipedia.org/wiki/Uniform_resource_identifier , UR
如何在文本 block 中发现 URI? 这个想法是将这样的文本运行变成链接。如果只考虑 http(s) 和 ftp(s) 方案,这很容易做到;但是,我猜测一般问题(考虑 tel、mailto 和其他
我的一些网址上有一些特殊字符。例如: http://blabla.com/title/?t=burası 当我从其他页面提供指向该页面的链接时,我使用: URI.encode(s) 产生这个链接: /
我在 Windows Mobile 上使用紧凑型框架/C#。 在我的应用程序中,我通过序列化对象并使用 HttpWebRequest/POST 请求向上发送信息来将数据上传到服务器。在服务器上,发布数
我正在做一个实验,我发现将 Canvas 转换为 blob,然后转换为数据 URI 会导致与直接从 Canvas 获取数据 URI 不同的 URI。打开时的内容在两个 URI 上几乎相同。 使用 bl
我正在尝试在 Rails 3 中实现 OAuth 提供程序。当我尝试授权客户端应用程序时,出现此错误。我正在使用 RESTful auth 插件和 pelles OAuth 插件。当我通过 Rails
我有一个编码的 UI 测试方法: public void MyTestMethod() { string baseUrl = "www.google.com"; GlobalVaria
我知道这是一个常见的错误,我正在使用一个已知的解决方案,但它仍然给我同样的错误: require 'open-uri' url = "http://website.com/dirs/filex[a]"
我正在尝试使用 .NET 中的 HttpClient 来使用 Web 服务,并且在我完成了 msdn 中提到的所有步骤之后 o 出现以下异常:提供了无效的请求 URI。请求 URI 必须是绝对 URI
我正在尝试检索文件的 URI。该文件存储在: /storage/emulated/0/AppName/FileName.png 如果我使用 Uri.fromFile(file),我得到的是 file:
我想知道 (SIP) URI 中的不同参数分隔符表示什么? 部分以;分隔,例如: . 其他用?隔开和 & ,例如: 最佳答案 SIP 分隔符规则来自RFC 2396 RFC 3986 已弃用.但是在
我想调用decodeUrl(...),我这样做是: import "dart:uri"; main() { decodeUrl("str"); } 但是现在有了最新的Dart-SDK,它会报告
在 URI 中,空格可以编码为 + .既然如此,那么在创建具有国际前缀的 tel URI 时是否应该对前导加号进行编码? 哪个更好?两者在实践中都有效吗? Call me Call me 最佳答案 不
我试图弄清楚电子邮件地址的格式是否可以说符合 URI 的定义,但到目前为止我还没有找到明确的确认。我希望有人可以在这里为我提供一些见解。预先感谢:) 最佳答案 是的,但带有“mailto:”前缀。 U
因此,我尝试将 ID 参数附加到 URI 的末尾,当用户单击我的列表中的项目时,用户将被发送到该 URI。我的代码如下: public void onItemClick(AdapterView par
这是 Converting file path to URI 的后续问题. 考虑: require 'uri' uri = URI.join('file:///', '/home/user/dir1/
我在 pl/sql 中创建了一个名为 tester 的包。但我收到以下消息。 绝对URI中的相对路径:java.net.URI.checkPath(URI.java:1823) --Package D
我在 gitlab 上有一个 git repo,使用私有(private) pod 和其他公共(public) pod,下面是我的 Podfile source 'git@gitlab.mycompa
我正在尝试将我的 Rails 应用程序推送到 heroku 上,我正在使用 heroku RedisToGo 附加组件我经历过这个tutorial并完成了那里提到的所有步骤。 但是在推送 heroku
我是一名优秀的程序员,十分优秀!