Oggi avevo affrontato un problema in Android surfaceview for camera customization
. Ho provato il codice seguente.surfaceview per la fotocamera non funziona in Android Lollipop OS
Il problema si è verificato quando ho catturato l'immagine, si ferma l'anteprima della fotocamera e non ritorna all'attività.
Il codice seguente verrà implementato nel programma. Ho preso questo codice da un riferimento esistente su stackoverflow
Classe di supporto.
public class AndroidCameraSurfaceview extends Activity implements SurfaceHolder.Callback { TextView testView; Camera camera; SurfaceView surfaceView; SurfaceHolder surfaceHolder; boolean preview; PictureCallback rawCallback; ShutterCallback shutterCallback; PictureCallback jpegCallback; int displayheight, displaywidth; Camera.PreviewCallback previewCallback; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.camerasurfaceview); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); surfaceView = (SurfaceView) findViewById(R.id.surfaceView); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); jpegCallback = new PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { Bundle b = new Bundle(); b.putByteArray("Image", data); Intent intent = new Intent(); intent.putExtras(b); setResult(RESULT_OK, intent); finish(); // refreshCamera(); } }; } public void captureImage(View v) throws IOException { // take the picture camera.takePicture(null, null, jpegCallback); } public void refreshCamera() { if (surfaceHolder.getSurface() == null) { // preview surface does not exist return; } try { camera.stopPreview(); } catch (Exception e) { } try { camera.setDisplayOrientation(90); camera.setPreviewDisplay(surfaceHolder); camera.startPreview(); } catch (Exception e) { } } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (preview) { camera.stopPreview(); } try{ Camera.Parameters parameters = camera.getParameters(); List<Size> sizes = parameters.getSupportedPreviewSizes(); Size optimalSize = getOptimalPreviewSize(sizes, width, height); parameters.setPreviewSize(optimalSize.width, optimalSize.height); camera.setParameters(parameters); try { camera.setDisplayOrientation(90); camera.setPreviewDisplay(holder); camera.startPreview(); preview = true; } catch (IOException e) { e.printStackTrace(); } }catch(Exception e){ System.out.println("Surface Exception---=>"+e); } } public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); if (camera != null) { Camera.Parameters params = camera.getParameters(); camera.setDisplayOrientation(90); camera.setParameters(params); } } public void surfaceDestroyed(SurfaceHolder holder) { // stop preview and release camera camera.stopPreview(); camera.release(); camera = null; } private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) { final double ASPECT_TOLERANCE = 1; double targetRatio = (double) w/h; if (sizes == null) return null; Size optimalSize = null; double minDiff = Double.MAX_VALUE; int targetHeight = h; for (Size size : sizes) { double ratio = (double) size.width/size.height; if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; if (Math.abs(size.height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } if (optimalSize == null) { minDiff = Double.MAX_VALUE; for (Size size : sizes) { if (Math.abs(size.height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } } return optimalSize; } }
2.Implemented in attività
pubblico captureImage void() {
Intent intentDriver = new Intent(AddNewDevice_Activity.this, AndroidCameraSurfaceview.class); startActivityForResult(intentDriver, 0); // // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // // Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // // intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // // // start the image capture Intent // startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // // fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // // intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // // // start the image capture Intent // startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0) { System.out.println("Result Code: " + resultCode); if (resultCode == RESULT_OK && data != null) { Bundle bundle = data.getExtras(); byte[] test = bundle.getByteArray("Image"); Bitmap bpCamera = BitmapFactory.decodeByteArray(test, 0, test.length); Matrix matrix = new Matrix(); matrix.postRotate(90); bpCamera = Bitmap .createBitmap(bpCamera, 0, 0, bpCamera.getWidth(), bpCamera.getHeight(), matrix, true); imageView_camera.setImageBitmap(bpCamera); selectedImageStr = encodeTobase64(bpCamera); } } else { finish(); } }
Significa che 'onActivityResult()' non viene chiamato dopo 'AndroidCameraSurfaceview.finish()' in 'onPictureTaken()'? Il tuo registro mostra che è stato chiamato 'onPictureTaken()'? Forse, la tua attività principale 'onCreate()' viene chiamata dopo? –
onPictureTaken() non è stato mostrato nel mio log, mostra solo la transazione Failed Binder in LogCat e ErrorLog ha mostrato l'eccezione del ciclo di eventi UnHandled. Sono ancora in bianco per risolvere questo problema, gentilmente aiutami. –
Grazie per aver formattato il signor SudhAnsu. Scusa per l'errore. –