![](/img/trans.png)
[英]How can I make an ImageView rotate the image when the device is rotated from portrait/landscape?
[英]How rotate a Landscape Image to Portrait in Android OpenCV 3
這段代碼將圖像從橫向旋轉到縱向,但是在Android中無法做到。 等效代碼是什么?
import cv2
import numpy
img = cv2.imread('original.png')
h, w = img.shape[:2]
img2 = numpy.zeros((w, h, 3), numpy.uint8)
cv2.transpose(img, img2)
cv2.flip(img2, 1, img2)
cv2.imwrite('rotate.png', img2)
我假設您將圖像作為Android中的OpenCV Mat(您可以使用Imgcodecs.imread()
方法加載圖像)。
然后,您可以像這樣:
Mat src = Imgcodecs.imread("path/to/file"); // initialize this with your image from file
Core.flip(src.t(), src, 1); // this will rotate the image 90° clockwise
Core.flip(src.t(), src, 0); // this will rotate the image 90° counter-clockwise
之后,使用Imgcodecs.imwrite()
保存圖像。 確保將權限添加到清單中:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
[解決了]...
獲得風景(W)800 x(H)600,然后輸出(H)800 x(W)600。 :)
Core.transpose(mRgba, mRgbaT); Imgproc.resize(mRgbaT, mRgbaF, mRgbaT.size(), 0, 0, 0); Core.flip(mRgbaF, mRgbaF, 1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.