Created
September 1, 2018 19:58
-
-
Save Binary-Finery/63cff66240c45844bf72971dd43a443f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package spencerstudios.com.colourwiz; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.graphics.Bitmap; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.graphics.drawable.BitmapDrawable; | |
import android.media.MediaScannerConnection; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.provider.MediaStore; | |
import android.support.annotation.NonNull; | |
import android.support.v7.app.AlertDialog; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.graphics.Palette; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.ImageView; | |
import android.widget.ListAdapter; | |
import android.widget.ListView; | |
import android.widget.Toast; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.List; | |
import spencerstudios.com.bungeelib.Bungee; | |
import spencerstudios.com.colourwiz.Adapters.ColorAdapter; | |
public class MainActivity extends AppCompatActivity { | |
private ImageView imageView; | |
private ListView listView; | |
private ColorAdapter adapter; | |
private List<PixData> pixData; | |
private static final int REQ_CODE_CHOOSE_PHOTO = 100; | |
public static final int REQ_CODE_TAKE_PHOTO = 200; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Intent intent = getIntent(); | |
int methodSelected = intent.getIntExtra("image_select_method", 1); | |
imageView = findViewById(R.id.image_view); | |
listView = findViewById(R.id.lv); | |
pixData = new ArrayList<>(); | |
adapter = new ColorAdapter(MainActivity.this, pixData); | |
imageView.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
shareSaveDialog(); | |
} | |
}); | |
switch (methodSelected) { | |
case 1: | |
Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); | |
startActivityForResult(pickPhoto, REQ_CODE_CHOOSE_PHOTO); | |
break; | |
case 2: | |
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
startActivityForResult(takePicture, REQ_CODE_TAKE_PHOTO); | |
break; | |
} | |
} | |
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { | |
super.onActivityResult(requestCode, resultCode, imageReturnedIntent); | |
if (resultCode==RESULT_CANCELED){ | |
finish(); | |
Bungee.card(MainActivity.this); | |
return; | |
} | |
if (requestCode == REQ_CODE_TAKE_PHOTO) { | |
if (resultCode == RESULT_OK) { | |
Bundle extras = imageReturnedIntent.getExtras(); | |
Bitmap imageBitmap = null; | |
if (extras != null) { | |
imageBitmap = (Bitmap) extras.get("data"); | |
} | |
imageView.setImageBitmap(imageBitmap); | |
generatePalette(); | |
} | |
} | |
if (requestCode == REQ_CODE_CHOOSE_PHOTO) { | |
if (resultCode == RESULT_OK) { | |
Uri imageUri = imageReturnedIntent.getData(); | |
imageView.setImageURI(imageUri); | |
generatePalette(); | |
} | |
} | |
} | |
private void generatePalette() { | |
pixData.clear(); | |
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); | |
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() { | |
@Override | |
public void onGenerated(@NonNull Palette palette) { | |
List<Palette.Swatch> swatches = palette.getSwatches(); | |
for (int i = 0; i < swatches.size(); i++) { | |
int bodyColor = swatches.get(i).getRgb(); | |
int qty = swatches.get(i).getPopulation(); | |
int textColor = swatches.get(i).getTitleTextColor(); | |
pixData.add(new PixData(bodyColor, textColor, qty)); | |
} | |
Collections.sort(pixData, new Comparator<PixData>() { | |
@Override | |
public int compare(PixData ob1, PixData ob2) { | |
return ob2.getQty() - ob1.getQty(); | |
} | |
}); | |
listView.setAdapter(adapter); | |
} | |
}); | |
} | |
private void shareSaveDialog() { | |
final AlertDialog dialog = new AlertDialog.Builder(this).create(); | |
View v = LayoutInflater.from(this).inflate(R.layout.image_dialog, null); | |
ImageView iv = v.findViewById(R.id.iv); | |
final Bitmap bm = getWholeListViewItemsToBitmap(); | |
iv.setImageBitmap(bm); | |
dialog.setView(v); | |
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "share", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i) { | |
sharePalette(bm); | |
} | |
}); | |
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "save", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i) { | |
savePalette(bm); | |
} | |
}); | |
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "cancel", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i) { | |
dialog.dismiss(); | |
} | |
}); | |
dialog.show(); | |
} | |
public Bitmap getWholeListViewItemsToBitmap() { | |
ListAdapter adapter = listView.getAdapter(); | |
int itemsCount = adapter.getCount(); | |
int allItemsHeight = 0; | |
List<Bitmap> bitmaps = new ArrayList<Bitmap>(); | |
for (int i = 0; i < itemsCount; i++) { | |
View childView = adapter.getView(i, null, listView); | |
childView.measure(View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); | |
childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight()); | |
childView.setDrawingCacheEnabled(true); | |
childView.buildDrawingCache(); | |
bitmaps.add(childView.getDrawingCache()); | |
allItemsHeight += childView.getMeasuredHeight(); | |
} | |
Bitmap bitmap = Bitmap.createBitmap(listView.getMeasuredWidth(), allItemsHeight, Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(bitmap); | |
Paint paint = new Paint(); | |
int iHeight = 0; | |
for (int i = 0; i < bitmaps.size(); i++) { | |
Bitmap bmp = bitmaps.get(i); | |
canvas.drawBitmap(bmp, 0, iHeight, paint); | |
iHeight += bmp.getHeight(); | |
bmp.recycle(); | |
} | |
return bitmap; | |
} | |
private void savePalette(Bitmap bitmap){ | |
String root = Environment.getExternalStorageDirectory().toString(); | |
File myDir = new File(root + "/palette.io"); | |
//myDir.mkdirs(); | |
String fileName = "palette-io-generated-" + System.currentTimeMillis()+ ".jpg"; | |
File file = new File(myDir, fileName); | |
//if (file.exists()) | |
// file.delete(); | |
try { | |
FileOutputStream out = new FileOutputStream(file); | |
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); | |
out.flush(); | |
out.close(); | |
MediaScannerConnection.scanFile(this, | |
new String[] { file.toString() }, null, | |
new MediaScannerConnection.OnScanCompletedListener() { | |
public void onScanCompleted(String path, Uri uri) { | |
Log.i("ExternalStorage", "Scanned " + path + ":"); | |
Log.i("ExternalStorage", "-> uri=" + uri); | |
} | |
}); | |
Toast.makeText(getApplicationContext(), "palette saved", Toast.LENGTH_SHORT).show(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private void sharePalette(Bitmap bitmap){ | |
String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap,"palette", "share palette"); | |
Uri bitmapUri = Uri.parse(bitmapPath); | |
Intent intent = new Intent(Intent.ACTION_SEND); | |
intent.setType("image/png"); | |
intent.putExtra(Intent.EXTRA_STREAM, bitmapUri); | |
startActivity(Intent.createChooser(intent, "Share")); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment