11

I'm attempting to access the raw feed of android's front facing camera. By default, the front facing camera's preview is flipped horizontally so users can see themselves as if looking into a mirror - that's great, but not what I need. What's the best way to get the raw feed? Is there some way to disable the automatic flipping, or should I attempt to flip it in code myself? My application needs to display a real-time feed of the front facing camera without it being flipped like a mirror.

Braiam
  • 1
  • 11
  • 47
  • 78
daedalus28
  • 1,637
  • 1
  • 12
  • 24

2 Answers2

12

If you want to use a front-facing camera for barcode scanning you can use TextureView and apply a transformation matrix to it. When the texture is updated you can read the image data and use that.

See https://github.com/hadders/camera-reverse

Specifically from MainActivity.java

mCamera.setDisplayOrientation(90);
Matrix matrix = new Matrix();
matrix.setScale(-1, 1);
matrix.postTranslate(width, 0);
mTextureView.setTransform(matrix);
Steve
  • 161
  • 1
  • 4
  • Hey @GreenFox Fox. I just cloned the repo and ran 'gradle installDebug' from the command line and installed onto my Nexus 5. This runs. When I launch the app and hold up some text 'ABC' it shows 'ABC' on the screen rather than the normal inverted back-to-front 'CBA' as expected. What is the exact problem? – Steve Nov 27 '13 at 08:39
  • Hey @GreenFox. What does "nothing works" mean? You should see an image. You must have a phone with a front facing camera as this thread is about front facing cameras. – Steve Nov 27 '13 at 22:10
  • the downloaded projects has no classes or whatsoever in it. – jofftiquez Nov 28 '13 at 05:45
  • Hey @GreenFox . There is only one class; an activity. Here: https://github.com/hadders/camera-reverse/blob/master/CameraReverse/src/main/java/com/hadders/camerareverse/MainActivity.java . I suggest you read up on AndroidStudio. – Steve Dec 01 '13 at 21:41
  • doesnt matter anyway i got the problem solved, but tnx for the help though :))) @Steve – jofftiquez Dec 02 '13 at 02:16
  • 1
    Thanks so much for the right answer! Be sure to check which camera is used and setTransform(null) when switching to the back camera. – colintheshots May 08 '15 at 15:29
  • if the device gets rotated, then the preview is flipped to be upside-down – nyarian Mar 25 '19 at 15:04
  • it's only for preview – user924 Mar 17 '21 at 09:18
1

The data from the front camera is as the camera the sees it, looking at you. The left side of its image is your right side. I think this is what you want already? When put onto a SurfaceView it is flipped so it acts as you say, but that's a separate cosmetic transformation.

At least, this is how every device I've seen works and I've looked hard at this to implement front camera support in Barcode Scanner / Barcode Scanner+.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • That makes sense. I am in fact attempting to render it with a SurfaceView - how should I go about it instead? – daedalus28 Mar 17 '12 at 23:05
  • Ah. Then I don't know. I think you'd have to receive the image data and paint it manually onto the `View`. Or maybe you have to reverse it by hand since it will just re-reverse it. I have never touched that aspect myself, just always let it fling whatever it wants onto the `SurfaceView` while I listen for the data separately. – Sean Owen Mar 18 '12 at 12:51
  • I too would like to render it non-mirrored in the SurfaceView. Did you ever find a solution? – Mark Jul 04 '12 at 20:25
  • @SeanOwen did you find the solution for fipping the surface view to see a non-mirrored preview... am struggling for the solution – Aswathy Jan 02 '14 at 04:55