2017-03-23 93 views
1

我正在使用ZxingScannerView掃描條形碼掃描儀。我需要在掃描時關閉閃光燈,但相機組件已棄用,所以是否有其他替代方法可以實現此目的。在此附上我的代碼,請給我一些可能的答案在zxingscanner.Thanks提前感謝如何在Zxing掃描儀視圖中永久關閉閃存?

import android.Manifest; 
    import android.app.Activity; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.content.pm.PackageManager; 
    import android.hardware.Camera; 
    import android.os.Bundle; 
    import android.support.v4.app.ActivityCompat; 
    import android.support.v4.content.ContextCompat; 
    import android.support.v7.app.AppCompatActivity; 
    import android.util.Log; 
    import android.view.Gravity; 
    import android.view.View; 
    import android.view.ViewGroup.LayoutParams; 
    import android.widget.FrameLayout; 
    import android.widget.ImageView; 
    import android.widget.Toast; 

    import com.google.zxing.Result; 

import me.dm7.barcodescanner.zxing.ZXingScannerView; 

public class WorkOut extends AppCompatActivity implements ZXingScannerView.ResultHandler { 

    Integer response = 0; 
    int currentIndex = 0; 

    private ZXingScannerView mScannerView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mScannerView = new ZXingScannerView(this); 
     Camera camera =Camera.open(); 

     Camera.Parameters p; 
     p = camera.getParameters(); 
     p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); 
     camera.setParameters(p); 

     CameraConfigurationUtils.setTorch(p,false); 

     FrameLayout frameLayout = new FrameLayout(this); 

     FrameLayout.LayoutParams mainParam = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     frameLayout.setLayoutParams(mainParam); 





     //end 3 
     scanButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent refresh = new Intent(WorkOut.this, MposHomeScreen.class); 
       startActivity(refresh); 
      } 
     }); 

     frameLayout.addView(mScannerView); 
     frameLayout.addView(scanButton); 

     setContentView(frameLayout); 



    } 

    private void checkPermissions() { 
     try { 
      for (int i = currentIndex; i < permissions.length; i++) { 
       currentIndex = currentIndex + 1; 
       int result = ContextCompat.checkSelfPermission(this, permissions[i]); 
       if (result == PackageManager.PERMISSION_GRANTED) { 
       } else { 
        requestPermission(permissions[i]); 
        return; 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    Activity activity = this; 
    Context context = this; 

    String[] permissions = new String[]{Manifest.permission.CAMERA}; 


    private void requestPermission(String permission) { 


     ActivityCompat.requestPermissions(activity, new String[]{permission}, 
      101); 

    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case 101: 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        checkPermissions(); 
       } else { 
        try { 

        } catch (Exception e) { 
         e.printStackTrace(); 
        } 

       } 
       break; 
     } 
    } 


    @Override 
    public void onResume() { 
     super.onResume(); 

     if (response == 1) { 
      mScannerView = null; 
      mScannerView = new ZXingScannerView(this); 
      setContentView(mScannerView); 
      response = 0; 

     } 

     mScannerView.setResultHandler(this); 
     mScannerView.startCamera(); 

    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 


     mScannerView.stopCamera(); 

    } 

    @Override 
    protected void onRestart() { 
     super.onRestart(); 
     Log.d("ani", "onrestart"); 
    } 

    @Override 
    public void handleResult(Result rawResult) { 
     Toast.makeText(getApplicationContext(), "Scanned", Toast.LENGTH_SHORT).show(); 
     mScannerView.resumeCameraPreview(this); 
    } 
} 

回答

1

對不起,最近的答覆......也許仍然會幫助某人。 ZXingScannerView本身有一套非常簡單的set/get方法來打開/關閉相應的相機手電筒。您不需要嘗試使用(不建議使用的)Camera或Camera2。

首先,打開閃光燈永久關閉,只需添加以下行分配mScannerView後:

mScannerView.setFlash(false); 

其次,用於通過一個按鈕開/關切換閃光燈,只需添加以下行按鈕的onClick方法:

mScannerView.setFlash(!mScannerView.getFlash()); 

就是這樣:)它對我來說太棒了。

+0

每當掃描儀聚焦時,重新聚焦時閃光燈會關閉並再次打開。這是爲什麼發生? – UrielUVD

+0

「掃描儀聚焦」?不清楚......你如何獲得掃描儀的重點? –

+0

當它獲得焦點...當它獲得焦點...當圖像不再模糊時。之後,掃描儀停止一秒鐘,然後關閉手電筒,失去焦點並再次打開燈光,等等。這是爲什麼發生? – UrielUVD