Nexus 10, front camera. Preview black (no preview)

So, I am working on a project related to the camera, and I tested it on many devices, and they all passed the tests, with the exception of Nexus 10.

I can’t understand what is happening and no one is talking about a problem on the Internet.

I was able to reproduce the problem on two different Nexus 10 (wifi) devices.

Here is my activity code:

public class MainActivity extends Activity { private static Camera mCamera; private static boolean mCameraOpen; private static ImageView mPreviewImageView; private SurfaceView mPreviewSurfaceView; private static boolean mPreviewRunning; private static Handler mHandler; private static int TESTS_COUNT = 0; private Camera.Parameters mCameraParameters; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mHandler = new Handler(); mPreviewSurfaceView = (SurfaceView) findViewById(R.id.surfaceview); mPreviewImageView = (ImageView) findViewById(R.id.imageview); mPreviewSurfaceView.getHolder().addCallback(mCallback); TextView view = (TextView) findViewById(R.id.textview); view.setText("Format: " + String.valueOf(TESTS_COUNT)); } @Override public void onResume(){ super.onResume(); if (mCamera == null){ for (int i = 0; i < Camera.getNumberOfCameras(); i++){ Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(i, info); if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){ mCamera = Camera.open(i); Camera.Parameters params = mCamera.getParameters(); params.set("camera-id", 2); List<Integer> formats = params.getSupportedPreviewFormats(); if (formats.size() > TESTS_COUNT) { Log.e("Camera", "testing preview format at index: " + TESTS_COUNT); params.setPreviewFormat(formats.get(TESTS_COUNT)); mCamera.setParameters(params); mCameraOpen = true; SurfaceHolder holder = mPreviewSurfaceView.getHolder(); if (holder != null && holder.getSurface() != null && holder.getSurface().isValid()) { mCallback.surfaceCreated(holder); } mCameraParameters = params; break; } else { finish(); } } } } } @Override public void onPause(){ super.onPause(); if (mPreviewRunning){ mCamera.stopPreview(); mCamera.setPreviewCallback(null); mPreviewRunning = false; } if (mCameraOpen){ mCamera.release(); mCamera = null; mCameraOpen = false; } } @Override public void onDestroy(){ super.onDestroy(); } private final SurfaceHolder.Callback mCallback = new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder surfaceHolder) { if (mCameraOpen && mCamera != null){ try { mCamera.setPreviewDisplay(surfaceHolder); mCamera.setPreviewCallback(new Camera.PreviewCallback() { private int count; private int total; @Override public void onPreviewFrame(byte[] bytes, Camera camera) { if (count == 15){ Camera.Size previewSize = mCamera.getParameters().getPreviewSize(); // pWidth and pHeight define the size of the preview Frame ByteArrayOutputStream out = new ByteArrayOutputStream(); // Alter the second parameter of this to the actual format you are receiving YuvImage yuv = new YuvImage(bytes, ImageFormat.NV21, previewSize.width, previewSize.height, null); // bWidth and bHeight define the size of the bitmap you wish the fill with the preview image yuv.compressToJpeg(new Rect(0, 0, previewSize.width, previewSize.height), 50, out); byte[] bitmapBytes = out.toByteArray(); final Bitmap bitmap= BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length); mHandler.post(new Runnable() { @Override public void run() { mPreviewImageView.setImageBitmap(bitmap); } }); count = 0; total++; if (total > 5){ TESTS_COUNT++; if (TESTS_COUNT == mCameraParameters.getSupportedPreviewSizes().size()) { finish(); return; } Intent intent = new Intent( MainActivity.this, MainActivity.class); startActivity(intent); } } else { count++; } } }); } catch (IOException e) { e.printStackTrace(); } } } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) { for (Camera.Size size : mCameraParameters.getSupportedPreviewSizes()){ if (size.width == width && size.height == height){ if (mCameraOpen && mCamera != null && mPreviewRunning == false) { mCameraParameters.setPreviewSize(width, height); mCamera.setParameters(mCameraParameters); mCamera.startPreview(); mPreviewRunning = true; break; } } } if (mPreviewRunning == false){ Log.e("CameraPreview", "size not supported"); } } @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { } }; } 

My layout:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:weightSum="2"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="horizontal" android:layout_weight="1" android:weightSum="2"> <SurfaceView android:layout_width="640px" android:layout_height="480px" android:id="@+id/surfaceview"/> <ImageView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/imageview"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="40sp"/> </LinearLayout> </LinearLayout> 

and manifest:

 <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> 

There are no error messages and the screen is just black

Screenshot

+7
android samsung-mobile android-camera android-4.2-jelly-bean nexus-10
source share
3 answers

Please confirm your device using the CtsVerifier application, this application is part of the Android Open Source project and is included in the compatibility test suite for Android devices (CTS). CTS Verifier has a series of tests that test the camera. check this link

You can be sure that if the front camera does not work in this application. Your device may be damaged, or some damage may have occurred on the hal layer. If so, you can try downgrading / updating your Android files. If not, it could be equipment damage.

+1
source share

The main question: "How to debug this?" The answer, as always, is to present as many hypotheses as possible that could explain these results, and then test these hypotheses.

Here are some hypotheses (think more):

  • He does not find a camera.
  • He does not find the camera from the front.
  • This is a misinterpretation of the camera settings.
  • Configuring mCallback too late, that is, after necessary.
  • He does not find a valid surface.
  • It is not successfully registered for camera frames.
  • It registers but does not accept camera frames.
  • It takes camera frames, but does not display them, for example. images are not in the expected format, or setImageBitmap(bitmap) Runnable does not work.
  • TESTS_COUNT does not do what you want.

You can test hypotheses through logging, but for most of this, it would be faster and more informative to go through it in a debugger one-step and examine data values ​​such as params .

If you are not familiar with the debugger, now is the time!

+6
source share

I am testing activity on Nexus 10 with Lollipop 5.1. Sometimes I notice that when I rotate the screen, the application crashes. I fix this problem by changing the start of the surfaceChanged method with:

 @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) { if (mCameraParameters==null) return; ... } 

In addition, if you want to study a working example about using camera hardware on Android, you can refer to the android_instacam project and its source code.

0
source share

All Articles