保存照片后获得黑屏 - Android相机

时间:2014-06-24 16:08:14

标签: android camera screen photo

我正在开发一款Android应用程序,可以捕获最大fps并保存到SD卡。

问题是保存的照片是黑屏,我无法理解原因。

谁能告诉我问题出在哪里?

我这样做的代码:

public class PhotoFragment extends Fragment {
private Camera cam;
private CameraPreview camPreview;
private boolean recording = false;
private ArrayList<byte[]> fotos;
private ArrayList<String> tempos;
private Thread thread;

public PhotoFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_photofinish, container, false);
    cam = getCameraInstance();
    if(cam != null) {
        cam.setDisplayOrientation(90);

        // set Camera parameters
        Camera.Parameters cameraParameters = cam.getParameters();

        //set color efects to none
        cameraParameters.setColorEffect(Camera.Parameters.EFFECT_NONE);

        //set antibanding to none
        if (cameraParameters.getAntibanding() != null) {
            cameraParameters.setAntibanding(Camera.Parameters.ANTIBANDING_OFF);
        }

        // set white ballance
        if (cameraParameters.getWhiteBalance() != null) {
            cameraParameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT);
        }

        //set flash
        if (cameraParameters.getFlashMode() != null) {
            cameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        }

        //set zoom
        if (cameraParameters.isZoomSupported()) {
            cameraParameters.setZoom(0);
        }

        //set focus mode
        cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);

        List<Size> sizes = cameraParameters.getSupportedPictureSizes();
        Camera.Size size = sizes.get(0);
        cameraParameters.setPictureSize(size.width, size.height);
        cam.setParameters(cameraParameters);
        fotos = new ArrayList<byte[]>();
        tempos = new ArrayList<String>();
        camPreview = new CameraPreview(this.getActivity(), cam);
        FrameLayout preview = (FrameLayout) rootView.findViewById(R.id.camera_preview);
        preview.addView(camPreview);
        TextView startRecording = (TextView) rootView.findViewById(R.id.start_record_button);
        startRecording.setOnClickListener( new View.OnClickListener() {
            public void onClick(View v) {
                if(!recording)
                {
                    recording = true;   
                    Size previewSize = cam.getParameters().getPreviewSize();
                    int dataBufferSize=(int)(previewSize.height*previewSize.width*(ImageFormat.getBitsPerPixel(cam.getParameters().getPreviewFormat())/8.0));
                    thread.start();
                    cam.addCallbackBuffer(new byte[dataBufferSize]);
                    cam.addCallbackBuffer(new byte[dataBufferSize]);
                    cam.addCallbackBuffer(new byte[dataBufferSize]);
                    cam.setPreviewCallbackWithBuffer(new PreviewCallback() {
                        public void onPreviewFrame(byte[] imageData, Camera arg1) {
                            try {                                   
                                fotos.add(imageData);
                                tempos.add(new SimpleDateFormat("HH_mm_ss_SSS", Locale.getDefault()).format(new Date()));
                            } catch(Exception e) {
                                System.out.println("ERRO: " + e);
                            }

                        }
                    });
                }
                else
                {
                    recording = false;
                    try {
                        thread.join();
                    } catch (Exception e) {

                    }
                }
            }               
        });
        thread = new Thread(new Runnable() {
              public void run() {
                  while(recording) {
                      if(fotos.size()>0 && tempos.size()>0)
                      {
                          File pictureFile = getOutputMediaFile(1, tempos.get(0));
                            if (pictureFile == null){
                                System.out.println("Error creating media file, check storage permissions: ");
                                return;
                            }

                            try {               
                                FileOutputStream fos = new FileOutputStream(pictureFile);
                                fos.write(fotos.get(0));
                                fos.close();
                                pictureFile = null;
                                cam.addCallbackBuffer(fotos.get(0));
                                fotos.remove(0);
                                tempos.remove(0);           
                            } catch (FileNotFoundException e) {
                                System.out.println("ERRO FILE NOT FOUND! : " + e);
                            } catch (IOException e) {
                                System.out.println("ERRO IOException!");
                            }
                      }
                  }

              }
        });
    }
    else
    {
        Toast.makeText(getActivity(), "Camera not available", Toast.LENGTH_LONG).show();
    }

    return rootView;
}

public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open();
    }
    catch (Exception e){

    }
    return c;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    cam.release();
}

private static File getOutputMediaFile(int type, String timeStamp){
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "L_P");
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            System.out.println("L_P failed to create directory");
            return null;
        }
    }

    String timeStampDay = new SimpleDateFormat("ddMMyyyy", Locale.getDefault()).format(new Date());

    new File(mediaStorageDir.getPath() + File.separator + timeStampDay).mkdirs();

    File mediaFile;
    if (type == 1){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + timeStampDay + File.separator + "IMG_"+ timeStamp + ".jpg");
    } else if(type == 2) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_"+ timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

}

1 个答案:

答案 0 :(得分:0)

您声称该文件是JPEG。但是,您没有做任何将图像转换为JPEG的操作。默认情况下,预览框不是JPEG格式,而是NV21格式。使用getSupportedPreviewFormats()查看是否可以使用JPEG预览,然后使用setPreviewFormat()请求JPEG预览。

而且,正如我在上一个问题中所提到的, NOT 有两个线程与ArrayList一起使用。此外,让您的后台线程忙碌 - 等待不断查看图片显示在ArrayList上。使用LinkedBlockingQueue或其他线程安全的东西,并允许后台线程在等待图像时阻塞。