public class MainActivity extends the Activity {
Public static final ints TAKE_PHOTO=1;
Private ImageView picture;
Private Uri ImageUri;
Private Button takePhoto;
@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Picture=(ImageView) the findViewById (R.i d.p icture);
TakePhoto=(Button) the findViewById (R.i which ake_photo);
TakePhoto. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View v) {
//create the File object, used to store the pictures after the picture
The File outputImage=new File (getExternalCacheDir (), "outputImage. JPG");
Try {
{if (outputImage. The exists ())
OutputImage. Delete ();
}
OutputImage. CreateNewFile ();
} the catch (IOException e) {
e.printStackTrace();
}
If (Build) VERSION) SDK_INT & gt; 24)={
ImageUri=FileProvider. GetUriForFile (MainActivity. This,
"Com. Example. Camerralbumtest. Fileprovider", outputImage);
} else {
ImageUri=Uri. FromFile (outputImage);
}
//start the camera application
Intent Intent=new Intent (" android. Media. Action. IMAGE_CAPTURE ");
Intent. PutExtra (MediaStore. EXTRA_OUTPUT ImageUri);
StartActivityForResult (intent, TAKE_PHOTO);
}
});
}
@ Override
Protected void onActivityResult (int requestCode, int the resultCode, Intent data) {
The switch (requestCode) {
Case TAKE_PHOTO:
If (the resultCode==RESULT_OK) {
Try {
//to display images
Bitmap Bitmap=BitmapFactory. DecodeStream (getContentResolver () openInputStream (ImageUri));
Try {
ArrayListPicturePixel=BitmapUtil. GetPicturePixel (bitmap);
HashMapColor2=new HashMap (a);
For (Integer color: picturePixel) {
If (color2. Either containsKey (color)) {
Integer to Integer=color2. Get (color);
Integer++;
System. The out. Println (integer++);
Color2. Remove (color);
Color2. Put (color, integer);
} else {
Color2. Put (color, 1);
}
}
//select the largest number of colors
The Iterator iter=color2. EntrySet (). The Iterator ();
int count=0;
Int color=0;
While (iter) hasNext ()) {
The Map. The Entry Entry=(Map. Entry) iter. Next ();
The int value=https://bbs.csdn.net/topics/(Integer) entry. GetValue ();
If (countThe count=value;
Color=(Integer) entry. GetKey ();
}
}
Bitmap. EraseColor (color);
Picture. SetImageBitmap (bitmap);
}
The catch (Exception e) {
Toast. MakeText (getApplication (), e. oString (), Toast. LENGTH_LONG), show ();
}
System. The gc ();
} the catch (FileNotFoundException e) {
e.printStackTrace();
Toast. MakeText (getApplication (), e. oString (), Toast. LENGTH_LONG), show ();
}
}
break;
Default:
break;
}
}
Static class BitmapUtil {
Public static ArrayListGetPicturePixel (Bitmap Bitmap)
{
Int width=bitmap. GetWidth ();
Int height=bitmap. GetHeight ();
//save all the pixel array, the image width x height
Int [] pixels=new int (width * height);
Bitmap. GetPixels (pixels, 0, width, 0, 0, width, height);
ArrayListRGB=new ArrayList (a);
for (int i=0; I & lt; Pixels. Length; I++) {
Int the CLR=pixels [I];
Int red=(CLR & amp; 0 x00ff0000) & gt;> 16.//take high two
Int green=(CLR & amp; 0 x0000ff00) & gt;> 8;//take two
Int blue=CLR & amp; 0 x000000ff;//take low two
The d (" tag ", "r=" + red + ", g="+ green +", b="+ blue);
Int color=color RGB (red, green, blue);//remove the white and black
If (color!=Color. WHITE & amp; & Color!=Color. BLACK) {
RGB. The add (color);
}
}
Return RGB.
}
}
}
CodePudding user response:
See your system version, different version is not the same method to obtain the images