I am creating a flashlight app. In my code, if camera(hardware) is there, it should show an alert, but it does not show the alert. My code is as below:
if (!isCameraFlash) {
showNoCameraAlert();
} else {
camera = Camera.open();
params = camera.getParameters();
}
but i get an error on
showNoCameraAlert();
Is this an invalid tag? or the tutorial i am using is old? I am using android studio. Here is my full Java code:
public class MainActivity extends ActionBarActivity {
private Camera camera;
ImageButton flashlightSwitchImg;
private boolean isFlashlightOn;
Parameters params;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flashlightSwitchImg = (ImageButton) findViewById(R.id.flashlightSwitch);
boolean isCameraFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!isCameraFlash) {
showNoCameraAlert();
} else {
camera = Camera.open();
params = camera.getParameters();
}
}
}
Any help will be appreciated. Thanks :)
You are calling the showNoCameraAlert() method. You need to add it to your MainActivity (that is the file where you put the code in.) So add it like this:
private void showNoCameraAlert(){
Toast.makeText(getApplicationContext(), "Camera flashlight not available in this Android device!", Toast.LENGTH_SHORT).show();
}
The full code for your MainActivity:
package com.example.administrator.flashlight;
import ...;
public class MainActivity extends ActionBarActivity {
private Camera camera;
ImageButton flashlightSwitchImg;
private boolean isFlashlightOn;
Parameters params;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flashlightSwitchImg = (ImageButton) findViewById(R.id.flashlightSwitch);
//check if phone has a flashlight
boolean isCameraFlash = getApplicationContext().getPackageManager() .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)
if (!isCameraFlash) {
//show error ( showNoCamereAlert() is below)
showNoCameraAlert();
} else {
//open the camera
camera = Camera.open();
params = camera.getParameters();
}
}
private void showNoCameraAlert(){
//Show Error toast
Toast.makeText(getApplicationContext(), "Camera flashlight not available in this Android device!", Toast.LENGTH_SHORT).show();
}
}
I'm starting to think that you didn't implement the showNoCameraAlert() method.
Try adding something like this to your MainActivity class:
private void showNoCameraAlert(){
Toast.makeText(getApplicationContext(), "There's no camera on the device or it doesn't have a flash", Toast.LENGTH_SHORT).show();
}
Related
I want to take a frame image from my TextureView (a preview form camera) when pressing a button. How can I do it?
I create an Activity with a TextureView and two buttons, one for start and stop the camera previewing and one for taking a frame from the preview.
This is my Activity:
public class PreviewActivity : Activity
{
bool myPreviewing;
Camera myCamera;
TextureView myTextureView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Preview);
Button startPreviewButton = FindViewById<Button>(Resource.Id.startPreview);
Button saveImageButton = FindViewById<Button>(Resource.Id.saveImage);
myTextureView = FindViewById<TextureView>(Resource.Id.myTextureView);
startPreviewButton.Click += delegate {
try
{
if (!myPreviewing)
{
myCamera = Open();
myCamera.SetPreviewTexture(myTextureView.SurfaceTexture);
myCamera.SetDisplayOrientation(90);
myCamera.StartPreview();
}
else
{
myCamera.StopPreview();
myCamera.Release();
}
}
catch (Java.IO.IOException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
myPreviewing = !myPreviewing;
}
};
saveImageButton.Click += delegate {
// do something for take frame
};
}
}
I am new to android and currently working on an app which uses the camera API. I am having trouble with the implementation of Zoom function in the app. I am using Camera kit API to make the app and Camera-kit Api does not provide support for zoom controls. Please help me with the code required. I am unable to figure it out since a week. I need to implement Zoom to the camera Preview.
this is my Code.
public class MicroscopeAcitvity extends AppCompatActivity implements View.OnClickListener {
CameraView cameraView;
ImageButton cameraTakePic;
ImageButton buttonFlash;
Camera mCamera; //= Camera.open();
Camera.Parameters params;
TextView T1,T2;
int counter = 0;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_microscope_acitvity);
cameraView = (CameraView) findViewById(R.id.camera);
cameraTakePic = (ImageButton) findViewById(R.id.button_take_picture);
cameraTakePic.setOnClickListener(this);
buttonFlash = (ImageButton) findViewById(R.id.button_Flash);
buttonFlash.setOnClickListener(this);
T1 = (TextView) findViewById(R.id.flash_text1);
T1.setOnClickListener(this);
//T1.setVisibility(View.GONE);
T2 = (TextView) findViewById(R.id.flash_text2);
T2.setOnClickListener(this);
//T2.setVisibility(View.GONE);
cameraView.setCameraListener(new CameraListener() {
#Override
public void onPictureTaken(byte[] picture) {
super.onPictureTaken(picture);
// Create a bitmap
Bitmap result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
}
});
cameraView.setFocus(CameraKit.Constants.FOCUS_TAP);
}
#Override
protected void onResume() {
super.onResume();
cameraView.start();
}
#Override
protected void onPause() {
cameraView.stop();
super.onPause();
}
#Override
public void onClick(View v) {
int id;
id = v.getId();
if(id == R.id.button_take_picture){
cameraView.captureImage();
}
if(id == R.id.button_Flash){
if(counter % 2 != 0){
T1.setVisibility(View.VISIBLE);
T2.setVisibility(View.VISIBLE);
counter = counter+1;
}
else{
T1.setVisibility(View.GONE);
T2.setVisibility(View.GONE);
counter = counter+1;
}
}
if(id == R.id.flash_text1){
T1.setTextColor(Color.RED);
T2.setTextColor(Color.DKGRAY);
cameraView.setFlash(CameraKit.Constants.FLASH_ON);
}
if (id == R.id.flash_text2){
T2.setTextColor(Color.RED);
T1.setTextColor(Color.DKGRAY);
cameraView.setFlash(CameraKit.Constants.FLASH_OFF);
}
}
}
If you want to zoom by pinch you have to implement a ScaleGestureDetector.SimpleOnScaleGestureListener
To set the zoom you simply have to change the camera parameters
Camera.Parameters parameters = camera.getParameters();
parameters.setZoom(zoomFactorValue);
camera.setParameters(parameters);
if you have some time you can code the rest yourself or look up how he did it
http://opencamera.sourceforge.net/
He uses pinch zoom on a surfaceview (but his code is not efficient and really ugly, very hard to understand on some points)
I need to show list of all possible displays , and after that select one of them to connect with miracast. Here is my code:
private Context context;
private DisplayManager mDisplayManager;
private WifiP2pManager wifiP2pManager;
private ArrayList<WifiP2pDevice> devices;
private WifiP2pManager.Channel channel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
context = this;
devices = new ArrayList<WifiP2pDevice>();
buttonScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startScan();
}
});
mDisplayManager = (DisplayManager)this.getSystemService(Context.DISPLAY_SERVICE);
wifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = wifiP2pManager.initialize(this, getMainLooper(), new WifiP2pManager.ChannelListener() {
public void onChannelDisconnected() {
channel = null;
Logger.makeLog("onChannelDisconnected");
}
});
}
WifiP2pManager.PeerListListener myPeerListListener = new WifiP2pManager.PeerListListener() {
#Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
// Out with the old, in with the new.
devices.clear();
devices.addAll(peerList.getDeviceList());
Logger.makeLog("devices size " + devices.size());
Toast.makeText(context, "devices size " + devices.size(), Toast.LENGTH_LONG).show();
if (devices.size() == 0) {
Logger.makeLog("No devices found");
return;
}
}
};
private void startScan() {
wifiP2pManager.requestPeers(channel, myPeerListListener);
}
After compiling this code, i press buttonScan, and it show, that 0 displays in area. But one display was near me. But then i connected to the display (TV), entered my app, pressed buttonScan, and it shows me, that 2 displays around me (TV and display of mobile). But its bad, i need to scan all possible displays to connect BEFORE connect... So, what am I doing wrong?
I am trying to build an app that reads a QRCode from an IDCard that everyone on my company will use.
I would like to use the front camera as default when I run the app. I managed to do it with the rear camera but ideally I don't want to have to use a button to switch.
MainActivity:
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private static final int REQUEST_CAMERA = 1;
private ZXingScannerView mScannerView;
Passageiros mPassageiro; //
Collection<Passageiros> listaPassageiros;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mScannerView = new ZXingScannerView(this){
#Override
protected IViewFinder createViewFinderView(Context context) {
return new CustomZXingScannerView(context);
}
};
List<BarcodeFormat> formats = new ArrayList<>();
formats.add(BarcodeFormat.QR_CODE);
setContentView(mScannerView);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (checkPermission()) {
Toast.makeText(getApplicationContext(), "Permission already granted", Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
}
...
}
How can I do this?
Looking at the sources of ZXing library
ZXingCameraView is extending BarcodeScannerView, which has a private CameraWrapper which you can set with setupLayout method. CameraWrapper allows you to initialize com.android.Camera.
And how to choose necessary out of available cameras is shown in that question: How do I open the "front camera" on the Android platform?
Edit: Actually there is even a ZXing CameraUtils class selecting camera on the back of device. Just change it to return id of front camera and pass that id to startCamera(frontCameraId) on your ZXingCameraView.
Android 2.3.3 Emulator doesn't recognize focus modes in camera.
public class LayerCounterActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (checkCameraHardware(getBaseContext())){
Toast.makeText(this, "Camera OK!", Toast.LENGTH_LONG).show();
mCamera=getCameraInstance();
if (mCamera==null){
Toast.makeText(this, "Camera not ready!", Toast.LENGTH_LONG).show();
}
else {
Camera.Parameters params = mCamera.getParameters();
List<String> focusModes = params.getSupportedFocusModes();
Why focusModes is null? When I start the application on a real device, everything works, but Emulator throws null pointer exception.