Home > Mobile >  Imageview display bitmap screen jammed problems, is to draw on camera, will obtain the bitmap by tra
Imageview display bitmap screen jammed problems, is to draw on camera, will obtain the bitmap by tra

Time:02-15

Import the Java. IO. The File;
import java.io.FileNotFoundException;
import java.io.IOException;
Import the Java. Util. ArrayList;
Import the Java. Util. HashMap;
Import the Java. Util. Iterator;
import java.util.Map;

The import android. App. The Activity;
The import android. Content. Intent;
The import android. Graphics. Bitmap;
The import android. Graphics. BitmapFactory;
The import android. Graphics. Color;
The import android.net.Uri;
The import android. OS. Build;
The import android. OS. Bundle;
The import android. The provider. MediaStore;
Import android. Support. The v4. Content. FileProvider;
The import android. Util. Log;
The import android. View. The view;
The import android. Widget. The Button;
The import android. Widget. ImageView;

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);
The setContentView (R.l ayout. 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 () {
@ Overrid
ePublic 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));

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);
ArrayList RGB=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);
}
}

HashMap Color2=new HashMap (a);
For (Integer color: RGB) {
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);
System. The gc ();
} the catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
Default:
break;
}
}
}
This part blue is the color of pick out the most frequently, get rid of this period of treatment, photos can be a plus, but normal displayed in the imageview real machine will be black screen jammed, new to the android is not very experienced, hope everybody to help me
  • Related