简体   繁体   English

如何处理没有黑屏的android相机打开

[英]How to handle android camera opening without a black screen

I am developing an android application in which open camera in a fragment. 我正在开发一个android应用程序,其中片段中打开相机。 Whenever Camera is opened for the FIRST TIME. 每当相机第一次打开时。 It loads with a small jerk of 1 second approximately. 它以大约1秒的小加速度加载。 Making screen black. 使屏幕变黑。 How to prevent screen from turning black for that second completely. 如何完全防止屏幕在那一秒内变黑。

Detailed Explanation: 详细说明:
When we open camera in Facebook messenger or even try to open camera normally in your phone. 当我们在Facebook Messenger中打开相机或什至尝试在手机中正常打开相机时。 It takes one second to open and meanwhile screens turns black. 打开需要一秒钟,同时屏幕变黑。 The same thing is happening. 同样的事情正在发生。 Can this be prevented? 可以预防吗? Any how? 怎么样? your reply will be highly appreciated Please guys. 谢谢您的回覆。

Below Is the working code with same problem as described above. 以下是与上述问题相同的工作代码。

 public class scan extends Fragment implements ZXingScannerView.ResultHandler{ private ZXingScannerView zXingScannerView; private SurfaceView mySurfaceView; private QREader qrEader; private Camera mCamera; private CameraPreview mPreview; private String m_Text=""; private String number=""; private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; private String mParam1; private String mParam2; private OnFragmentInteractionListener mListener; public scan() { } @Override public void handleResult(Result rawResult) { Log.e("handler", rawResult.getText()); Log.e("handler", rawResult.getBarcodeFormat().toString()); AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity()); builder.setTitle("Scan Result"); builder.setMessage(rawResult.getText()); number = rawResult.getText().substring(rawResult.getText().length() - 13); // final EditText input = new EditText(this.getActivity()); input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); builder.setView(input); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { m_Text = input.getText().toString(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); AlertDialog alert1 = builder.create(); alert1.show(); zXingScannerView.resumeCameraPreview(this); } public static scan newInstance(String param1, String param2) { scan fragment = new scan(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } if(checkCameraHardware(getActivity().getApplicationContext())) { zXingScannerView = new ZXingScannerView(this.getActivity().getApplicationContext()); zXingScannerView.setResultHandler(this); zXingScannerView.startCamera(); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_scan, container, false); FrameLayout preview =(FrameLayout)view.findViewById(R.id.camera_preview); preview.addView(zXingScannerView); return view; } @Override public void onPause() { super.onPause(); zXingScannerView.stopCamera(); } @Override public void onResume() { super.onResume(); zXingScannerView.setResultHandler(this); // Register ourselves as a handler for scan results. zXingScannerView.startCamera(); // Start camera on resume } /** Check if this device has a camera */ public boolean checkCameraHardware(Context context) { if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){ // this device has a camera return true; } else { // no camera on this device return false; } } public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); } } @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof OnFragmentInteractionListener) { mListener = (OnFragmentInteractionListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { if (mCamera!= null) { mCamera.stopPreview(); mCamera.release(); mCamera = null; } super.onDetach(); mListener = null; } public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } } 

I see that you use the deprecated Camera API. 我看到您使用了不赞成使用的 Camera API。 If you want your app to perform the best on devices with Lollipop or higher, you should switch to the new camera2 API. 如果希望您的应用在具有Lollipop或更高版本的设备上表现最佳,则应切换到新的camera2 API。 Please see the discussion here: Android camera android.hardware.Camera deprecated . 请在此处查看讨论内容: 不推荐使用Android相机android.hardware.Camera

The new API can improve performance significantly, but if you are stuck with the old one, don't despair. 新的API可以显着提高性能,但是如果您对旧的API感到困惑,请不要失望。 If you are not using the latest version of ZXingScannerView, update this class to open the camera in a background thread. 如果您使用的不是最新版本的ZXingScannerView,请更新此类以在后台线程中打开相机。 This change improved startup significantly. 此更改显着改善了启动。

If your fragment is part of a ViewPager (like camera preview within viewpager ), make sure that the camera preview is started before the pager brings the scan fragment to screen. 如果您的片段是ViewPager的一部分(例如viewpager中的相机预览),请确保在分页器将扫描片段带到屏幕之前启动相机预览。

In other scenarios, it is preferable to go for a variation of splash screen paradigm, which has it pros and cons. 在其他情况下,最好使用初始屏幕范式的变体,这有其优点和缺点。 You can show for a short while another non-black view on top of the camera preview surface, or show a predefined texture if you use OpenGL preview. 您可以在相机预览表面上方短暂显示另一个非黑色视图,或者如果使用OpenGL预览则显示预定义的纹理。

Try this below code to open camera and click picture as it works for me. 尝试以下代码打开相机并单击对我有用的图片。

public class CameraImage extends Fragment {

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1888;
Button button;
ImageView imageView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    final View rootView = inflater.inflate(R.layout.camera_image,
            container, false);

    button = (Button) rootView.findViewById(R.id.button);
    imageView = (ImageView) rootView.findViewById(R.id.imageview);

    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent,
                    CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

        }
    });

    return rootView;

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {

            Bitmap bmp = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream stream = new ByteArrayOutputStream();

            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();

            // convert byte array to Bitmap

            Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,
                    byteArray.length);

            imageView.setImageBitmap(bitmap);

        }
    }        
 }    
}

Hope it will work for you. 希望它对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM