3

I want to rotate a Camera in a viewfinder. My idea is to get the QByteArray and rotate it, so the pixels are shown mirrored in X and Y direction. So if my array looks like this:

5 2 6 8
3 5 2 1
6 7 4 5
3 2 1 7

it should looks like this:

7 1 2 3
5 4 7 6
1 2 5 3
8 6 2 5

Can somebody tell me how I can do that?

Der_Pätti
  • 35
  • 1
  • 9

2 Answers2

2

you can use std::reserve like this

#include <algorithm> 

QByteArray reverse = barr; 
std::reverse(reverse.constBegin(), reverse.constEnd());

or use this

QByteArray reverse;
std::reverse_copy(barr.constBegin(), barr.constEnd(), reverse.begin());
danics
  • 323
  • 1
  • 9
0

How are you accessing your QByteArray?

If iterators are an acceptable solution you could do something like this:

for(std::reverse_iterator<char*> rBegin(myQByteArray.data() + myQByteArray.size()); rBegin != std::reverse_iterator<char*>(myQByteArray.data()); ++rBegin){
    std::cout << *rBegin << " ";
}

Note that if you want to use the const char* QByteArray::data() call you can simply initialize your std::reverse_iterators with const in the template:

std::reverse_iterator<const char*> rBegin(myQByteArray.data())
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • It gave me a error about the std::cout.. is there a mistake? Should it be count? But then there is the mistake that the list of the formal parameters is not right – Der_Pätti Nov 17 '14 at 15:34
  • @Der_Pätti I don't have `QByteArray` on this system, but you can see this working with a `const char*` here: http://ideone.com/BS0xHR – Jonathan Mee Nov 17 '14 at 15:48
  • 1
    use this instead: #include QByteArray reverse = seq; std::reverse(reverse.constBegin(), reverse.constEnd()); – danics Nov 17 '14 at 16:42
  • @danics Your solution is the best one can you add that as an answer? Or if you don't feel like it I'll edit my solution. – Jonathan Mee Nov 17 '14 at 16:58
  • @danics There is an error, that in the you cannot give a constant a value. Do I have to change something in the Code? – Der_Pätti Nov 18 '14 at 08:00
  • @danics never mind When I open the programm everything is ok, but when I try to get the camera, the program crashs. And it gives me the warning, that std::Reverse_copy is unsafe. – Der_Pätti Nov 18 '14 at 08:09
  • @Der_Pätti, why you think problem is reverse operation may be your data assigned to QByteArray isn't valid any more in first position, write some code – danics Nov 18 '14 at 08:37
  • QActionGroup *videoDevicesGroup = new QActionGroup(this); videoDevicesGroup->setExclusive(true); foreach(const QByteArray &deviceName, QCamera::availableDevices()) { QString description = camera->deviceDescription(deviceName); QAction *videoDeviceAction = new QAction(description, videoDevicesGroup); videoDeviceAction->setCheckable(true); videoDeviceAction->setData(QVariant(deviceName)); if (cameraDevice.isEmpty()) { cameraDevice = deviceName; QByteArray cameraDevice; camera = new QCamera(cameraDevice); thats what it looked like before – Der_Pätti Nov 18 '14 at 09:12
  • You can choose in the menu witch camera you want to show. – Der_Pätti Nov 18 '14 at 09:13
  • @Der_Pätti Please make future comments on [danics](http://stackoverflow.com/users/4202858/danics) answer rather than mine for the clarity of any future viewers of this question. – Jonathan Mee Nov 18 '14 at 14:14