Libgdx localisation - android

I want create a Localisation file for my project in Libgdx; however, my code is throwing an error.
My code:
package com.mygdx.mytest;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.utils.I18NBundle;
import java.util.Locale;
public class MyTest extends ApplicationAdapter {
#Override
public void create () {
FileHandle baseFileHandle = Gdx.files.internal("i18n/MyBundle");
Locale locale =new Locale("", "", "");
I18NBundle MyBundle = I18NBundle.createBundle(baseFileHandle, locale);
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
}
My error:
I18NBundle MyBundle = I18NBundle.createBundle(baseFileHandle, locale);
Where is my mistake? Please help.

Your code works, but apparently you don't have a bundle.
You need to have a file with path:
{project root}/android/assets/i18n/MyBundle.properties
Remember to give a error log.

I use this code in my project and it work fine for me.
FileHandle internal = Gdx.files.internal("i18n/lang");
I18NBundle local = I18NBundle.createBundle(internal, Locale.ROOT);
I18NBundle portuguese = I18NBundle.createBundle(internal, new Locale("pt"));

Related

Android Studio How to read image file in OpenCV using resources?

I am trying to read image file using OpenCV Imgcodecs.imread(img) but the image Mat is always empty, I'm using this code:
package com.halocdz.qreagle;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.objdetect.QRCodeDetector;
public class MainActivity extends AppCompatActivity {
TextView detectedData_text;
TextView detectStatus_text;
static
{
if (!OpenCVLoader.initDebug())
Log.e("OpenCv", "Unable to load OpenCV");
else
Log.d("OpenCv", "OpenCV loaded");
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize
detectStatus_text = (TextView)findViewById(R.id.detectStatus_text);
detectedData_text = (TextView)findViewById(R.id.detectedData_text);
}
#SuppressLint("SetTextI18n")
public void onClickDetect(View view)
{
String path = getResources().getResourceName(R.drawable.qrcode);
Mat img = Imgcodecs.imread(path + ".png");
Mat points = new Mat();
QRCodeDetector qrdetector = new QRCodeDetector();
detectStatus_text.setText("Detecting QRCode, standby...");
String data;
if (img.empty())
{
detectStatus_text.setText("Error could not load image file!");
return;
}
if (!qrdetector.detect(img, points))
{
detectStatus_text.setText("Error detecting QRCode!");
return;
}
data = qrdetector.decode(img, points);
if (data.isEmpty())
{
detectStatus_text.setText("Error decoding QRCode!");
return;
}
detectedData_text.append(path +"\n\n");
detectStatus_text.setText("QRCode successfully detected!");
}
}
It appears that Imgcodecs.imread() not finding the file path to load the file, if so I don't know how to get file correct path in app resources.
Note: I put qrcode.png file in app\src\main\res\drawable\qrcode.png.
I am not sure how to do with Imgcodecs.imread() in a proper way. However, if you only want to read a Mat object from a drawable resources, I have two workarounds
1) By using bitmap
Bitmap bMap=BitmapFactory.decodeResource(getResources(),R.drawable.qrcode);
Mat img = new Mat();
Utils.bitmapToMat(bMap, img);
where qrcode is a image under Resources folder of your Android
project. Then convert Bitmap to bytes or Mat and process in C++
(OpenCV) or Java with matToBitmap or MatToBitmap methods in
android-opencv. Ref and Ref2
2) Resource loader from org.opencv.android.utils
Mat img = null;
try {
img = Utils.loadResource(this, R.drawable.qrcode, CvType.CV_8UC4);
} catch (IOException e) {
e.printStackTrace();
}

How to migrate a jzy3d java project into android?

I followed jzy3d's demo and made a 3D plot model with AnalysisLauncher.open() in IntelliJ, win10. It's working great. Now I need to migrate to Android so that the model can be displayed in a mobile application.
What kind of change is needed for my code? A sample project with any jzy3d surface plot would be ideal.
I read the page How to use jzy3d in android using eclipse?. The idea seems promising because jzy3d relies on JOGL 2 and OpenGL is cross-platform API. But I couldn't understand all the instructions by #Martin. Namely how do I replace AWT and 'derive CanvasNewtAwt'? I have no idea how to start the project in Android Studio.
The jzy3d website(http://www.jzy3d.org/) also says 'Android supposed to work if enable approte JOGL jars'. How do I do this?
import MyColorMap.ColorMapGrayColor;
import org.jzy3d.analysis.AbstractAnalysis;
import org.jzy3d.analysis.AnalysisLauncher;
import org.jzy3d.chart.factories.AWTChartComponentFactory;
import org.jzy3d.colors.Color;
import org.jzy3d.colors.ColorMapper;
import org.jzy3d.colors.colormaps.ColorMapGrayscale;
import org.jzy3d.colors.colormaps.IColorMap;
import org.jzy3d.maths.Coord3d;
import org.jzy3d.plot3d.primitives.Point;
import org.jzy3d.plot3d.primitives.Polygon;
import org.jzy3d.plot3d.primitives.Shape;
import org.jzy3d.plot3d.rendering.canvas.Quality;
public class ChartPlot extends AbstractAnalysis {
private int unitSize;
private BufferedImage img;
public static void main(String[] args) throws Exception {
AnalysisLauncher.open(new ChartPlot("Forest.jpg", 10));
}
ChartPlot (String imgName, int unitSize) {
img= getImg(imgName);
this.unitSize= unitSize;
}
#Override
public void init() {
int[][][] xycoords= getXYCoords(img, unitSize);
float[][][] zcoords= getZCoords(img, unitSize);
List<Polygon>[] polygonsArray= (List<Polygon>[])new List[COUNT_COLOR];
for (int iColor=0; iColor < polygonsArray.length; iColor++) {
polygonsArray[iColor]= getPolygons(xycoords, zcoords, iColor);
}
IColorMap[] colorMaps= new IColorMap[] {new ColorMapGrayscale(), new ColorMapGrayColor(ColorMapGrayColor.RED),
new ColorMapGrayColor(ColorMapGrayColor.YELLOW), new ColorMapGrayColor(ColorMapGrayColor.BLUE),
new ColorMapGrayscale()};
Shape[] surfaces= new Shape[COUNT_COLOR];
for (int iColor= 0; iColor < surfaces.length; iColor++) {
surfaces[iColor]= iColor == INDEX_WHITE || iColor == INDEX_BLACK ?
getSurface_Global(polygonsArray[iColor], colorMaps[iColor]) :
getSurface_Local(polygonsArray[iColor], colorMaps[iColor]);
}
chart = AWTChartComponentFactory.chart(Quality.Advanced, getCanvasType());
chart.getScene().getGraph().add(surfaces[INDEX_BLACK]);
chart.getScene().getGraph().add(surfaces[INDEX_RED]);
chart.getScene().getGraph().add(surfaces[INDEX_YELLOW]);
chart.getScene().getGraph().add(surfaces[INDEX_BLUE]);
chart.getScene().getGraph().add(surfaces[INDEX_WHITE]);
}

Getting Appium Driver "Null pointer exception"

I have been working on appium with Selenium and Testng from some time. I was able to execute my script without this null pointer error till yesterday. Can anyone tell me what's wrong with my script.
package testCases;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.Assert;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import utilities.Constant;
import utilities.ExcelUtils;
import pageObjects.DearoLoginPage;
import pageObjects.JobCardsPage;
import TestData.MongoDBConnector;
import utilities.AndroidAppSetup;
public class Main_TestCase {
AndroidDriver<MobileElement> driver;
DearoLoginPage objLogin = new DearoLoginPage(driver);
MongoDBConnector objDB = new MongoDBConnector();
AndroidAppSetup objAndroidAppSetup = new AndroidAppSetup();
#BeforeClass
public void setUp() throws Exception{
//This is to open the Excel file. Excel path, file name and the sheet name are parameters to this method
ExcelUtils.setExcelFileSheet(Constant.Path_TestData+Constant.File_TestData, "Sheet1");
////Get the Desired Capabilities
driver = (AndroidDriver<MobileElement>) objAndroidAppSetup.setupCapabilities();
System.out.println("driver2 =" + driver);
Thread.sleep(2000);
}
#Test
public void loginRegisteredMobile() throws Exception {
//Get the data from excel datasheet
String MobileNumber = ExcelUtils.getCellData(1,0);
System.out.println("driver3= " + driver);
objLogin.MobileNumberOnLogin().clear();
//Enter Mobile number
objLogin.MobileNumberOnLogin().sendKeys(MobileNumber);
//Click on Next button
objLogin.NextButtonOnLogin().click();
}
My another Package has below code
package utilities;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.URL;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import utilities.Constant;
import utilities.ExcelUtils;
public class AndroidAppSetup {
public AndroidDriver<MobileElement> driver;
public AndroidDriver<MobileElement> setupCapabilities() throws Exception{
ExcelUtils.setExcelFileSheet(Constant.Path_TestData+Constant.File_TestData, "Sheet1");
//Get data from excelsheet
String DeviceName = ExcelUtils.getCellData(1,6);
String DeviceId = ExcelUtils.getCellData(1,7);
String AndroidVersion = ExcelUtils.getCellData(1,8);
String AppPackage = Constant.AppPackage;
String AppActivity = Constant.AppActivity;
String AppiumURL = Constant.AppiumURL;
//Set the Desired Capabilities
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", DeviceName);
caps.setCapability("udid", DeviceId); //Give Device ID of your mobile phone
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", AndroidVersion);
caps.setCapability("appPackage", AppPackage);
caps.setCapability("appActivity", AppActivity);
caps.setCapability(CapabilityType.TAKES_SCREENSHOT, "true");
driver = new AndroidDriver<MobileElement>( new URL(AppiumURL), caps);
System.out.println("driver1= " + driver);
return driver;
}
}
pageObjects.DearoLoginPage is defined in below class
package pageObjects;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class DearoLoginPage {
private static MobileElement element = null;
//private AppiumDriver<MobileElement> driver;
private AndroidDriver<MobileElement> driver;
public DearoLoginPage(AndroidDriver<MobileElement> driver) {
// private AndroidDriver<MobileElement> driver;
this.driver = driver;
}
public MobileElement MobileNumberOnLogin(){
System.out.println("driver4 = " + driver);
element = driver.findElement(By.id("com.carworkz.debug:id/et_login_mobile_no"));
return element;
}
When I execute my script I am getting Null pointer exception as below,
INFO: Detected dialect: OSS
driver1= Android: null
driver2 =Android: null
driver=Android: null
driver4 = null
FAILED: loginRegisteredMobile
java.lang.NullPointerException
at pageObjects.DearoLoginPage.MobileNumberOnLogin(DearoLoginPage.java:26)
at testCases.Main_TestCase.loginRegisteredMobile(Main_TestCase.java:148)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:669)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:776)
at org.testng.TestRunner.run(TestRunner.java:634)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:425)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385)
at org.testng.SuiteRunner.run(SuiteRunner.java:334)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
at org.testng.TestNG.runSuites(TestNG.java:1161)
at org.testng.TestNG.run(TestNG.java:1129)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Please help me to resolve this issue.
DearoLoginPage class instance object is created and initialized before the driver initialization . Please change the initialization as below and then check.
Modified Main_TestCase Class:
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.Assert;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import utilities.Constant;
import utilities.ExcelUtils;
import pageObjects.DearoLoginPage;
import pageObjects.JobCardsPage;
import TestData.MongoDBConnector;
import utilities.AndroidAppSetup;
public class Main_TestCase {
AndroidDriver<MobileElement> driver;
//removed the initialization part and initialization will happen in BeforeClass Method
DearoLoginPage objLogin;
MongoDBConnector objDB = new MongoDBConnector();
AndroidAppSetup objAndroidAppSetup = new AndroidAppSetup();
#BeforeClass
public void setUp() throws Exception{
//This is to open the Excel file. Excel path, file name and the sheet name are parameters to this method
ExcelUtils.setExcelFileSheet(Constant.Path_TestData+Constant.File_TestData, "Sheet1");
////Get the Desired Capabilities
driver = (AndroidDriver<MobileElement>) objAndroidAppSetup.setupCapabilities();
//DearoLoginPage object initialization is added
objLogin= new DearoLoginPage(driver);
System.out.println("driver2 =" + driver);
Thread.sleep(2000);
}
#Test
public void loginRegisteredMobile() throws Exception {
//Get the data from excel datasheet
String MobileNumber = ExcelUtils.getCellData(1,0);
System.out.println("driver3= " + driver);
objLogin.MobileNumberOnLogin().clear();
//Enter Mobile number
objLogin.MobileNumberOnLogin().sendKeys(MobileNumber);
//Click on Next button
objLogin.NextButtonOnLogin().click();
}

i am unable findthe app_package and app_activity in MobileCompatibityType while coding in eclipse for appium.This is for testing the native app

import java.net.URL;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
public class LaunchAppium {
AndroidDriver driver;
// driver = new AndroidDriver("http://127.0.0.1:4723/wd/hub", test);
#BeforeClass
public void setup() throws MalformedURLException
{
DesiredCapabilities test=new DesiredCapabilities();
test.setCapability(MobileCapabilityType.DEVICE_NAME, "Androidemulator");
test.setCapability(MobileCapabilityType.APP, "Ebutor 15_dec_2016.apk");
//test.setCapability(MobileCapabilityType., value);
driver = new AndroidDr`enter code here`iver(new URL("http://127.0.0.1:4723/wd/hub"), test);
}
In the above pice of code i could not find the APP_PACKAGE and APP_ACTIVITY for the mobilecapabilitytpe instance which is essential for the android settings.
PLEASE HELP!!!
From 4.0, this is moved to specific to android package:
public interface AndroidMobileCapabilityType extends CapabilityType {
/**
* Activity name for the Android activity you want to launch from your package.
* This often needs to be preceded by a . (e.g., .MainActivity instead of MainActivity).
*/
String APP_ACTIVITY = "appActivity";
/**
* Java package of the Android app you want to run.
*/
String APP_PACKAGE = "appPackage";
/**
* Activity name for the Android activity you want to wait for.
*/
String APP_WAIT_ACTIVITY = "appWaitActivity";
}
So you have to use
AndroidMobileCapabilityType.APP_ACTIVITY
Please check the above it should work or you can see the see # link
package com.appium.android;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
public class LaunchAppium {
static AndroidDriver driver;
// driver = new AndroidDriver("http://127.0.0.1:4723/wd/hub", test);
#SuppressWarnings("deprecation")
#BeforeClass
public void setup() throws MalformedURLException
{
DesiredCapabilities test=new DesiredCapabilities();
//test.setCapability(MobileCapabilityType.APP_PACKAGE, "com.veronicaapps.veronica.simplecalculator");
//test.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.veronicaapps.veronica.simplecalculator.MainActivity");
test.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator");
test.setCapability(MobileCapabilityType.VERSION, "4.2.2");
test.setCapability(MobileCapabilityType.PLATFORM_NAME, "android");
//test.setCapability("appPackage",
"com.veronicaapps.veronica.simplecalculator");
//test.setCapability("appActivity", ".MainActivity");
//driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
test);
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), test);
}
public interface AndroidMobileCapabilityType extends CapabilityType {
String APP_ACTIVITY = "com.veronicaapps.veronica.simplecalculator.MainActivity";
String APP_PACKAGE = "com.veronicaapps.veronica.simplecalculator";
}
#Test
public void atest() throws Exception
{
WebDriver two=(WebDriver) driver.findElement(By.linkText("2"));
((WebElement) two).click();
WebDriver plus= (WebDriver) driver.findElement(By.linkText("+"));
((WebElement) plus).click();
WebDriver five= (WebDriver) driver.findElement(By.linkText("5"));
((WebElement) five).click();
WebDriver equal= (WebDriver) driver.findElement(By.linkText("="));
((WebElement) equal).click();
}
}

phonegap custom plugin for android advice needed

Hi I am in mobile app.
I have developed an app in phonegap (html5, JQuery, JS) and I want to develop a plugin to print to a BT printer.
I download printer manufacturer's SDK and I imported the appropriate .jar file to my project with the following way:
To include this library into your project:
Drag the appropriate library file into the Project Explorer from the SDK package
Right click the project folder and choose Properties
Click Java Build Path
Click Libraries and the Add JARs button
At the top of your main code add:
import com.starmicronics.stario.StarIOPort;
import com.starmicronics.stario.StarIOPortException;
import com.starmicronics.stario.StarPrinterStatus;
Now you can access all of StarIO’s methods!
I create the following plugin
js
var HelloPlugin = {
callNativeFunction: function (success, fail, resultType) {
return cordova.exec(success, fail, "com.tricedesigns.HelloPlugin", "nativeAction", [resultType]);
}
};
java
package com.tricedesigns;
import com.starmicronics.stario.StarIOPort;
import com.starmicronics.stario.StarIOPortException;
import com.starmicronics.stario.StarPrinterStatus;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.util.Log;
public class HelloPlugin extends Plugin {
public static final String NATIVE_ACTION_STRING="nativeAction";
public static final String SUCCESS_PARAMETER="success";
public static final String portName = "BT:";
public static final String portSettings = "mini";
#Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
Log.d("HelloPlugin", "Hello, this is a native function called from PhoneGap/Cordova!");
//only perform the action if it is the one that should be invoked
if (NATIVE_ACTION_STRING.equals(action)) {
String resultType = null;
try {
resultType = data.getString(0);
}
catch (Exception ex) {
Log.d("HelloPlugin", ex.toString());
}
byte[] texttoprint = resultType.toString().getBytes();
if (resultType.equals(SUCCESS_PARAMETER)) {
StarIOPort port = null;
return new PluginResult(PluginResult.Status.OK, "Yay, Success!!!");
}
else {
return new PluginResult(PluginResult.Status.ERROR, "Oops, Error :(");
}
}
return null;
}
}
which is working with no promblems.
When i try to include the below call to printer .jar method
port = StarIOPort.getPort(portName, portSettings, 10000, context);
I get Error: Status=2 Message=Class not found.
Where am i wrong????

Categories

Resources