比较2位图的Android问题

时间:2016-06-16 11:02:05

标签: android bitmap

我有一个两位图,我想比较这两个图像,但我不能这样做。请告诉我怎么做/?我有一个无线基地应用程序,用户连接到wifi时,他设置壁纸保存在共享优惠中,并再次得到它现在当用户再次连接到这个wifi他的壁纸自动更改为此他已保存在sharedprefrence.now问题是这不是比较位图来检查壁纸是否已经有相同的图像,然后它什么都不做,如果位图不相等,那么它将sharedfrefences保存图像设置为壁纸。 这是我的代码。

final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
//
                ConnectivityManager connectivityManager = (ConnectivityManager)
                        context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

                if (mWifi.isConnected()) {
                        final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                        final WifiInfo conn = wifiManager.getConnectionInfo();
                    //Toast.makeText(MainActivity.this, con.getSSID()+"",Toast.LENGTH_LONG).show();

                    if (conn.getSSID().toString().equalsIgnoreCase("\"" + homewifi + "\"")) {

                        final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
                        final Drawable wal = wallpaperManager.getDrawable();
                        final Bitmap bitmap = ((BitmapDrawable)wal).getBitmap();

                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                        final byte[][] b = {baos.toByteArray()};
                        String encodedImage = Base64.encodeToString(b[0], Base64.DEFAULT);
                        // textEncode.setText(encodedImage);

                        SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(context);
                        SharedPreferences.Editor edit=shre.edit();
                        edit.putString("image_data",encodedImage);
                        edit.commit();

                        final String previouslyEncodedImage = shre.getString("image_data", "");

                                    b[0] = Base64.decode(previouslyEncodedImage, Base64.DEFAULT);
                                    bit = BitmapFactory.decodeByteArray(b[0], 0, b[0].length);

                                        if (!bitmap.equals(bit)) {
                                            try {
                                                context.setWallpaper(bit);
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                            Toast.makeText(Profile1Activity.this,"wallpaper set", Toast.LENGTH_SHORT).show();
                                        } else {
                                                Toast.makeText(Profile1Activity.this,"wallpaper already set", Toast.LENGTH_LONG).show();
                                        }      
                    }
                }
                    else {

                    }

                handler.postDelayed(this, 5000);
            }
        }, 5000);

    }

1 个答案:

答案 0 :(得分:0)

private static String mergeTwoImg(String botImgPath, String topImgPath, float leftPadding, float topPadding) {
        Bitmap bottomImage = BitmapFactory.decodeFile(botImgPath);
        Bitmap topImage = BitmapFactory.decodeFile(topImgPath);

        Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
        Bitmap bmp = Bitmap.createBitmap(bottomImage.getWidth(), bottomImage.getHeight(), conf); // this creates a MUTABLE bitmap

        Canvas comboImage = new Canvas(bmp);

        comboImage.drawBitmap(bottomImage, 0f, 0f, null);
        comboImage.drawBitmap(topImage, leftPadding, topPadding, null);

        File file = new File(outPath + "/mergingImg.png");
        if (file.exists()) {
            file.delete();
        }

        OutputStream os = null;
        try {
            os = new FileOutputStream(outPath + "/mergingImg.png");
            bmp.compress(Bitmap.CompressFormat.PNG, 50, os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outPath + "/mergingImg.png";
    }