注意:这个答案实际上只是扩展了 riwnodennyk 的答案更容易实现。这都是渲染脚本,不关心任何 NDK。
涵盖所有常见的旋转和获取方向。
要获得旋转,您需要使用 ExifInterface。使用支持库中的那个,因为 sdk 版本存在问题。它适用于 JPEG 和 RAW(及类似)文件,因为此信息嵌入在文件中(而不是在解码的位图中)。
添加到 build.gradle
implementation "com.android.support:exifinterface:28.0.0"
如果你有文件的句柄就用这个
import android.renderscript.Allocation;
import android.renderscript.RenderScript;
import your.application.package.rs.ScriptC_rotator;
...
public static Bitmap getCorrectlyRotatedBitmap(@NonNull Context context, @NonNull File imageFile) throws IOException {
ExifInterface ei = new ExifInterface(imageFile.getPath());
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getPath());
int neededRotationClockwise = ei.getRotationDegrees() % 360;
return rotateClockwise(context, bitmap, neededRotationClockwise);
}
对于位图旋转本身
public static Bitmap rotateClockwise(@NonNull Context context, @NonNull Bitmap bitmap, int degrees) {
Log.i(TAG, "rotate bitmap degrees: " + degrees);
if (degrees == 0F) return bitmap;
RenderScript rs = RenderScript.create(context);
ScriptC_rotator script = new ScriptC_rotator(rs);
script.set_inWidth(bitmap.getWidth());
script.set_inHeight(bitmap.getHeight());
Allocation sourceAllocation = Allocation.createFromBitmap(rs, bitmap,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
bitmap.recycle();
script.set_inImage(sourceAllocation);
Bitmap.Config config = bitmap.getConfig();
switch (degrees) {
case 90: {
Bitmap target = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getWidth(), config);
final Allocation targetAllocation = Allocation.createFromBitmap(rs, target,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
script.forEach_rotate_90_clockwise(targetAllocation, targetAllocation);
targetAllocation.copyTo(target);
rs.destroy();
return target;
}
case 180: {
Bitmap target = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), config);
final Allocation targetAllocation = Allocation.createFromBitmap(rs, target,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
script.forEach_rotate_180(targetAllocation, targetAllocation);
targetAllocation.copyTo(target);
rs.destroy();
return target;
}
case 270: {
Bitmap target = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getWidth(), config);
final Allocation targetAllocation = Allocation.createFromBitmap(rs, target,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
script.forEach_rotate_270_clockwise(targetAllocation, targetAllocation);
targetAllocation.copyTo(target);
rs.destroy();
return target;
}
default:
throw new IllegalArgumentException("rotateClockwise() only supports 90 degree increments");
}
}
还有渲染脚本,在src/main/rs/rotator.rs 中创建文件,这是使用渲染脚本的默认位置,但您必须自己创建目录。
根据需要更改your.application.package.rs
#pragma version(1)
#pragma rs java_package_name(your.application.package.rs)
rs_allocation inImage;
int inWidth;
int inHeight;
uchar4 __attribute__ ((kernel)) rotate_270_clockwise (uchar4 in, uint32_t x, uint32_t y) {
uint32_t inX = inWidth - 1 - y;
uint32_t inY = x;
const uchar4 *out = rsGetElementAt(inImage, inX, inY);
return *out;
}
uchar4 __attribute__ ((kernel)) rotate_90_clockwise (uchar4 in, uint32_t x, uint32_t y) {
uint32_t inX = y;
uint32_t inY = inHeight - 1 - x;
const uchar4 *out = rsGetElementAt(inImage, inX, inY);
return *out;
}
uchar4 __attribute__ ((kernel)) rotate_180 (uchar4 in, uint32_t x, uint32_t y) {
uint32_t inX = inWidth - 1 - x;
uint32_t inY = inHeight - 1 - y;
const uchar4 *out = rsGetElementAt(inImage, inX, inY);
return *out;
}
uchar4 __attribute__ ((kernel)) flip_vertical (uchar4 in, uint32_t x, uint32_t y) {
uint32_t inX = x;
uint32_t inY = inHeight - 1 - y;
const uchar4 *out = rsGetElementAt(inImage, inX, inY);
return *out;
}
uchar4 __attribute__ ((kernel)) flip_horizontal (uchar4 in, uint32_t x, uint32_t y) {
uint32_t inX = inWidth - 1 - x;
uint32_t inY = y;
const uchar4 *out = rsGetElementAt(inImage, inX, inY);
return *out;
}