How to get an unique id from a drawable image !!. - android

I'm on working on a process for adding icons to the home menu from a icon selection window. if the user selected some icons ,in the next time user click on add icon button , the window should be appear with ticked icon .
here is my code
private void getAlreadyTicked() {
Log.d("getAlreadyTicked", "getAlreadyTicked");
//getting already ticked values for selection page
String icon_name_val = null, icon_link_val = null, icon_flag_val = null;
List<HomeIconset> icondetails = dbh.geticondetails();
for (HomeIconset sp : icondetails) {
icon_name_val = sp.getIcon_name();
icon_link_val = sp.getIcon_link();
icon_flag_val = sp.getIcon_flag();
intSelected = null;
intSelected = new Integer[19];
String[] stringIconName = {"paymoneyimg", "recieve",
"load", "buysell",
"gas", "electricity",
"mobileimg", "movie",
"buyforex", "sellforex",
"travelcard", "send",
"flight", "bus",
"hotel", "holiday",
"gold", "vehicleloan",
"personalloan", "insuranceimg"};
for (int i = 0; i < 19; i++) {
String imgName;
Drawable drawableImgName = null;
Bitmap result;
Drawable[] drawableImgNameArray = new Drawable[19];
if (icon_name_val.equals(stringIconName[i])) {
int resID = getResources().getIdentifier(icon_name_val, "drawable", getActivity().getPackageName());
Bitmap bitmapbus = BitmapFactory.decodeResource(getResources(), R.drawable.bus);
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), resID);
Bitmap resized = Bitmap.createScaledBitmap(bitmap1, bitmapbus.getWidth(), bitmapbus.getHeight(), true);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.tick);
// Bitmap resultBitmap = Bitmap.createBitmap(bitmap2.getWidth(), bitmap2.getHeight(), Bitmap.Config.ARGB_8888);
Bitmap resultBitmap = Bitmap.createBitmap(bitmapbus.getWidth(), bitmapbus.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(resultBitmap);
c.drawBitmap(resized, 0, 0, null);
c.drawBitmap(bitmap2, 0, 0, null);
drawableImgName = new BitmapDrawable(getResources(), resultBitmap);
result = resultBitmap;
ImageView im = new ImageView(getActivity());
//im.setTag(icon_name_val+i);
im.setImageDrawable(drawableImgName);
// String name = getActivity().getResources().getResourceEntryName(drawableImgName);
Log.d("tickedimage", String.valueOf(String.valueOf(drawableImgName)) + " ..." + im.getResources());
} else {
int resID = getResources().getIdentifier(icon_name_val, "drawable", getActivity().getPackageName());
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), resID);
drawableImgName = getResources().getDrawable(getResources().getIdentifier(icon_name_val, "drawable", getActivity().getPackageName()));
result = bitmap1;
}
// drawableImgNameArray[i]=drawableImgName;
// Bitmap bitmap = BitmapFactory.decodeResource(getResources(), drawableImgNameArray[19]);
intSelected[i] = getResources().getIdentifier(drawableImgName, "drawable", myContext.getPackageName());
}
for (int j = 0; j < intSelected.length; j++) {
Log.d("imgarraysf", String.valueOf(intSelected[j]));
}
}
}

If I correctly understood your code and the question you have to use icon_name_val.
That is,
intSelected[i] = getResources().getIdentifier(icon_name_val, "drawable", myContext.getPackageName());

Related

Temple image detection not working correctly

I have a picture that contains 2 icons at the bottom of the picture i'm already croping the picture to get only the buttom and comparing it with the icon picture opencv temple image detecter it works perfectly when the icon is there but the problem is when i delete the icon still the rectangle appears and it appears in wrong place all i want if there is a match show the rectangle if there is not don't show it
here is my code
public class Test123 extends AppCompatActivity {
ImageView a,b,c;
String resultImgPath,baseDir,lol;
Button x;
private static final String TAG = "Test123";
String aa,bb;
static {
if(!OpenCVLoader.initDebug()){
Log.e(TAG, "OpenCV not loaded");
} else {
Log.e(TAG, "OpenCV loaded");
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test123);
System.loadLibrary("opencv_java3");
a = (ImageView)findViewById(R.id.imageView);
b = (ImageView)findViewById(R.id.imageView2);
c = (ImageView)findViewById(R.id.imageView3);
x = (Button) findViewById(R.id.button);
baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
resultImgPath = baseDir+"/Test/result.jpg";
aa = baseDir+"/Test/d.jpg";
bb = baseDir+"/Test/dd1.jpg";
lol = baseDir+"/Test/c.jpg";
x.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bitmap bmImg = BitmapFactory.decodeFile(aa);
int fromHere = (int) (bmImg.getHeight() * 0.06);
final Bitmap croppedBitmap = Bitmap.createBitmap(bmImg, (int) (bmImg.getWidth() * 0.3), (int) (bmImg.getHeight() * 0.94), (int) (bmImg.getWidth() * 0.6), fromHere);
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Test");
myDir.mkdirs();
String fname = "c.jpg";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
croppedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
a.setImageBitmap(croppedBitmap);
Bitmap bmImg2 = BitmapFactory.decodeFile(bb);
b.setImageBitmap(bmImg2);
matchingDemo(lol, bb, resultImgPath, Imgproc.TM_SQDIFF);
}
});
}
public void matchingDemo(String imgPath,String templatePath,String resPath, int matchType){
// to read the entered image from its path and make a mat object
Mat img = Imgcodecs.imread(imgPath);
Mat templ = Imgcodecs.imread(templatePath);
// Create the result matrix
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_8UC3);
// performing matching and do normalization
Imgproc.matchTemplate(img, templ, result, matchType);
int type = Imgproc.THRESH_TOZERO;
Imgproc.threshold(result, result, 0.8, 1., type);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
// / finding the best match from minMaxLoc
Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
double bb = mmr.maxVal;
Log.e("hey",bb+"");
Point matchLoc;
if (matchType == Imgproc.TM_SQDIFF || matchType == Imgproc.TM_SQDIFF_NORMED) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}
// draw a rectangle on searched object
Imgproc.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
matchLoc.y + templ.rows()), new Scalar(0, 255, 0));
// store the result image here
Imgcodecs.imwrite(resPath, img);
Mat image = new Mat();
image =Imgcodecs.imread(resPath);
Bitmap bm = Bitmap.createBitmap(image.cols(),image.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(image, bm);
c.setImageBitmap(bm);
image.release();
}
}
EDIT
i know that i need something here
if(......................)
Imgproc.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
matchLoc.y + templ.rows()), new Scalar(0, 255, 0));
}
I tried to put minVal inside the if block but every picture gives me different numbers i tried with normalize and without it i can put the right number on 1 picture but other picture gives me different numbers so it is not detecting at all if the icon is visible or if it is not it givin the same issue drawing in wrong place i just need 1 number or something to draw if it is there to not draw if there is no match i don't want to have value if there is no match

Programatically add linearlayouts on top of each other

I am trying to create multiple layers of linearlayouts one underneath the other but the result shows only the first linearlayout and I think that the other linearlayouts are to the right of each other rather than stacked on top of each other. Can you help?
My code is as follows:
public void actionBrowse(){
LinearLayout HolderLL = new LinearLayout(browseActivity);
LinearLayout picLL1 = new LinearLayout(browseActivity);
LinearLayout picLL2 = new LinearLayout(browseActivity);
LinearLayout picLL3 = new LinearLayout(browseActivity);
LinearLayout picLL4 = new LinearLayout(browseActivity);
LinearLayout picLL5 = new LinearLayout(browseActivity);
HolderLL.addView(picLL1);
HolderLL.addView(picLL2);
HolderLL.addView(picLL3);
HolderLL.addView(picLL4);
HolderLL.addView(picLL5);
LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
for(int i=0; i<State.getInstance().getDesignUnit().getAllSavedImages().size(); i++){
if(i<4) {
ArrayList<DesignUnitViewer> savedImages = State.getInstance().getDesignUnit().getAllSavedImages();
DesignUnitViewer dv = savedImages.get(i);
Bitmap bmp = dv.getBacking_store();
ImageView newImage = new ImageView(browseActivity);
newImage.setImageBitmap(bmp);
picLL1.addView(newImage, imageParams);
}
else if (i>= 4 && i <8){
ArrayList<DesignUnitViewer> savedImages = State.getInstance().getDesignUnit().getAllSavedImages();
DesignUnitViewer dv = savedImages.get(i);
Bitmap bmp = dv.getBacking_store();
ImageView newImage = new ImageView(browseActivity);
newImage.setImageBitmap(bmp);
picLL2.addView(newImage, imageParams);
}
else if( i >=8 && i<12){
ArrayList<DesignUnitViewer> savedImages = State.getInstance().getDesignUnit().getAllSavedImages();
DesignUnitViewer dv = savedImages.get(i);
Bitmap bmp = dv.getBacking_store();
ImageView newImage = new ImageView(browseActivity);
newImage.setImageBitmap(bmp);
picLL3.addView(newImage, imageParams);
}else if (i >= 12 && i<16) {
ArrayList<DesignUnitViewer> savedImages = State.getInstance().getDesignUnit().getAllSavedImages();
DesignUnitViewer dv = savedImages.get(i);
Bitmap bmp = dv.getBacking_store();
ImageView newImage = new ImageView(browseActivity);
newImage.setImageBitmap(bmp);
picLL4.addView(newImage, imageParams);
}else if (i >=16 && i<20){
ArrayList<DesignUnitViewer> savedImages = State.getInstance().getDesignUnit().getAllSavedImages();
DesignUnitViewer dv = savedImages.get(i);
Bitmap bmp = dv.getBacking_store();
ImageView newImage = new ImageView(browseActivity);
newImage.setImageBitmap(bmp);
picLL5.addView(newImage, imageParams);
}
}
browseActivity.setContentView(HolderLL);
}

Android - fast algorithm to compare bitmaps

I'm making that find duplicate images.
I'm using this code for to do this
private static boolean compare(Bitmap b1, Bitmap b2) {
if (b1.getWidth() == b2.getWidth() && b1.getHeight() == b2.getHeight()) {
int[] pixels1 = new int[b1.getWidth() * b1.getHeight()];
int[] pixels2 = new int[b2.getWidth() * b2.getHeight()];
b1.getPixels(pixels1, 0, b1.getWidth(), 0, 0, b1.getWidth(), b1.getHeight());
b2.getPixels(pixels2, 0, b2.getWidth(), 0, 0, b2.getWidth(), b2.getHeight());
if (Arrays.equals(pixels1, pixels2)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
The code works fine, but it's very slow when i put a List of Images.
public static Bitmap getBitmapFromPath(String path){
File sd = Environment.getExternalStorageDirectory();
File image = new File(path);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inPreferredConfig = Bitmap.Config.RGB_565;
bmOptions.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
return bitmap;
}
public static ArrayList<String> compareListOfImages(Activity activity){
ArrayList<String> pathList = FileUtils.getImagesPath(activity);
ArrayList<String> pathListResult = new ArrayList<>();
if(pathList!=null) {
for (int i = 0; i < pathList.size(); i++){
Log.d("I", i + " ITEM");
Bitmap bitmap1 = FileUtils.getBitmapFromPath(pathList.get(i));
for (int k =0; k <pathList.size(); k++){
Log.d("PATH", pathList.get(k));
Bitmap bitmap2 = FileUtils.getBitmapFromPath(pathList.get(k));
if(!pathList.get(i).equals(pathList.get(k))) {
if (FileUtils.compare(bitmap1, bitmap2)) {
pathListResult.add(pathList.get(k));
}
}
}
}
}
return pathListResult;
}
Someone know a othe way to get result more fast?

how to add the "int" (Array of drawables) in an array of scene

Mat last;
//Mat last2;
ArrayList<Scene> scenes = new ArrayList<Scene>();
//ArrayList<Scene> scenes23 = new ArrayList<Scene>();
ArrayList<Bitmap> myImageList2 = new ArrayList<Bitmap>();
int[] myImageList = new int[]{R.drawable.baldglassy,
R.drawable.baldglassy2,
R.drawable.bandedarcherfish,
R.drawable.bandedarcherfish2,
R.drawable.bluegill,
R.drawable.bluegill2,
R.drawable.bluespotmullet};
/*Bitmap[] images2 = { BitmapFactory.decodeResource(getResources(),R.drawable.baldglassy),
BitmapFactory.decodeResource(getResources(),R.drawable.baldglassy2),
BitmapFactory.decodeResource(getResources(),R.drawable.bandedarcherfish),
BitmapFactory.decodeResource(getResources(),R.drawable.bluegill),
BitmapFactory.decodeResource(getResources(),R.drawable.bluegill2),
BitmapFactory.decodeResource(getResources(),R.drawable.bluespotmullet),
};*/
Scene refScene;
ProgressDialog progress;
//Mat imgMAT;
public void takePic1(View w) {
//Bitmap bmp32 = images[](Bitmap.Config.ARGB_8888, true);
/*for(int i=0;i<=5;i++){
Bitmap bmp32 = images[i].copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmp32, imgMAT);
}
Scene scene2 = new Scene(imgMAT);
scenes.add(scene2);*/
Scene scene = new Scene(last);
scenes.add(scene);
addBtn.setText("Add (" + scenes.size() + ")");
}
public void takePic2(View w) {
Mat im = last.clone();
// Imgproc.cvtColor(im, im, Imgproc.COLOR_BGR2RGB);
Bitmap bmp = Bitmap.createBitmap(im.cols(), im.rows(),
Bitmap.Config.ARGB_8888);
Utils.matToBitmap(im, bmp);
matchDrawArea.setImageBitmap(bmp);
refScene = new Scene(last);
}
when i use a bitmap array, the app crashes
it says that change the type of my int array to Scene. But it is impossible. Thanks in advance to those who will help.
here is the scene class
public class Scene {
final Mat image;
final Mat descriptors = new Mat();
final MatOfKeyPoint keypoints = new MatOfKeyPoint();
boolean firstTime = true;
public Scene(Mat image) {
this.image = image.clone();
// DetectUtility.analyze(image, keypoints, descriptors);
}
public void preCompute() {
if (firstTime) {
DetectUtility.analyze(image, keypoints, descriptors);
firstTime = false;
}
}
public SceneDetectData compare(Scene frame, boolean isHomogrpahy, boolean imageOnly) {
// Info to store analysis stats
SceneDetectData s = new SceneDetectData();
// Detect key points and compute descriptors for inputFrame
MatOfKeyPoint f_keypoints = frame.keypoints;
Mat f_descriptors = frame.descriptors;
this.preCompute();
frame.preCompute();
// Compute matches
MatOfDMatch matches = DetectUtility.match(descriptors, f_descriptors);
// Filter matches by distance
MatOfDMatch filtered = DetectUtility.filterMatchesByDistance(matches);
// If count of matches is OK, apply homography check
s.original_key1 = (int) descriptors.size().height;
s.original_key2 = (int) f_descriptors.size().height;
s.original_matches = (int) matches.size().height;
s.dist_matches = (int) filtered.size().height;
if (isHomogrpahy) {
MatOfDMatch homo = DetectUtility.filterMatchesByHomography(
keypoints, f_keypoints, filtered);
Bitmap bmp = DetectUtility.drawMatches(image, keypoints,
frame.image, f_keypoints, homo, imageOnly);
s.bmp = bmp;
s.homo_matches = (int) homo.size().height;
return s;
} else {
Bitmap bmp = DetectUtility.drawMatches(image, keypoints,
frame.image, f_keypoints, filtered, imageOnly);
s.bmp = bmp;
s.homo_matches = -1;
return s;
}
}
}

why imageview take space when load multiple image in android

image take space when i load image i don;t understand what is the problem why is take space after first iamge other image is display normallly u can see my activity java class which have imagedisplay method i am alos load image here in this image is take space before and after first image
myactivity.java
public void imagedisplay()
{
itable = (TableLayout)findViewById(R.id.itable);
String chekplace=null;
Log.e("MyList Size", "Counted--->"+mylist.size());
int size=mylist.size();
s:
for(int j=0;j<mylist.size();j++)
{
trstate = new TableRow(this);
trstate.setGravity(Gravity.CENTER_HORIZONTAL);
tstate = new TextView(this);
tstate.setGravity(Gravity.CENTER);
String tempstate = mylist.get(j).get(KEY_PLACENAME).intern().toString();
tstate.setText(tempstate);
trstate.addView(tstate);
itable.addView(trstate);
chekplacename=mylist.get(j).get(KEY_PLACENAME).intern().toString();
int counter=1;
String nextplace=chekplacename;
chekplace=nextplace;
m:
while(nextplace==chekplace)
{
trimage = new TableRow(this);
trimage.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
for (int m = 1; m <= 4; m++)
{
chekplace=mylist.get(j).get(KEY_PLACENAME).intern().toString();
if(nextplace==chekplace)
{
iimage = new ImageView(this);
iimage.setPadding(2,2,2,2);
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 4;
String loadimageurl=mylist.get(j).get(KEY_PHOTOURL).intern().toString();
counter++
bm = LoadImage(loadimageurl, bmOptions);
int height=bm.getWidth();
int width=bm.getHeight();
Log.e("newheight","Before--->" +height);
Log.e("new width","Before--->"+width);
bm=resize(bm, bm.getHeight(), bm.getWidth());
height=bm.getWidth();
width=bm.getHeight();
iimage.setLayoutParams(new TableRow.LayoutParams( height, width));
iimage.setImageBitmap(bm);
trimage.addView(iimage);
}
}
itable.addView(trimage);
}
}

Categories

Resources