my api is 2.2,so i used camera.setDisplayOrientation(90) to make the preview is Portrait,this work very well,but when i save the pic to sd,the pic is horizontal not Portrait.when i used :
#Override
protected Bitmap doInBackground(String... params) {
int w = bmp.getWidth();
int h = bmp.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(90);
// Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
bmp.recycle();
return rotatedBMP;
}
#Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
ivNewPhoto.setImageBitmap(result);
resultPath = ConstValue.MY_ALBUM_DIR + "/" + System.currentTimeMillis() + ".jpg";
ImageFile.writePhotoJpg(result), resultPath);
previewView.setVisibility(View.VISIBLE);
}
bmp is the Snapshot pic but i have mistakes:
Activity com.android.SuperPictureSearch.photo.PhotoActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#43abb200 that was originally added here
can you tell me how to save the pic is Portrait,thank you
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap mutableBitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bm= BitmapFactory.decodeByteArray(data, 0, data.length, options);
mutableBitmap =Bitmap.createScaledBitmap( bm , h, w,true);
bm.recycle();
Matrix matri = new Matrix();
matri.postRotate(90);
Bitmap mBitmap = Bitmap.createBitmap(mutableBitmap, 0, 0, mutableBitmap.getWidth(), mutableBitmap.getHeight(), matri, true);
mutableBitmap.recycle();
because the pic is too big form camera,so i size it
Related
I am tying to make a custom camera and after taking picture I am setting it in image view in the same activity as in which I am setting camera. I have been successful in taking the photos but before setting the image in image view I have to scale it which reduces the image quality. Is there any way to show the real image instead of scaling it?
My images are as below First one is real view of camera which is surface view:
After Taking photo it becomes:
The code I am using is:
Camera.PictureCallback picture = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
mCamera.stopPreview();
surface_view.setVisibility(View.INVISIBLE);
setupImageDisplay(data);
}
};
private void setupImageDisplay(byte[] data) {
photo = BitmapFactory.decodeByteArray(data, 0, data.length);
photo = scaleDown(photo, true);//scaling down bitmap
imageview_photo.setImageBitmap(photo); //setting bitmap in imageview
}
public Bitmap scaleDown(Bitmap realImage, boolean filter) {
int screenWidth = width;
int screenHeight = height;
Bitmap scaled;
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// Notice that width and height are reversed
scaled = Bitmap.createScaledBitmap(realImage, screenHeight, screenWidth, filter);
int w = scaled.getWidth();
int h = scaled.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
if (camera_id == Camera.CameraInfo.CAMERA_FACING_FRONT) {
float[] mirrorY = {-1, 0, 0, 0, 1, 0, 0, 0, 1};
Matrix matrixMirrorY = new Matrix();
matrixMirrorY.setValues(mirrorY);
mtx.postConcat(matrixMirrorY);
}
mtx.postRotate(90);
// Rotating Bitmap
realImage = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, filter);
} else {// LANDSCAPE MODE
//No need to reverse width and height
scaled = Bitmap.createScaledBitmap(realImage, screenHeight, screenWidth, filter);
int w = scaled.getWidth();
int h = scaled.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
if (camera_id == Camera.CameraInfo.CAMERA_FACING_FRONT) {
float[] mirrorY = {-1, 0, 0, 0, 1, 0, 0, 0, 1};
Matrix matrixMirrorY = new Matrix();
matrixMirrorY.setValues(mirrorY);
mtx.postConcat(matrixMirrorY);
}
mtx.postRotate(180);
// Rotating Bitmap
realImage = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, filter);
}
return realImage;
}
After taking photo the image is like squeezed is there any way that image remains the same after scaling?
You can create a separate file which is temporary file and stores the thumbnail size of the image. You can make a POJO like this to store both images. You can display the smaller one and use the original file to keep high quality.
public class Image {
File fullSize;
File Thumbnail;
public Image(File fullSize, File thumbnail) {
this.fullSize = fullSize;
Thumbnail = thumbnail;
}
public File getFullSize() {
return fullSize;
}
public void setFullSize(File fullSize) {
this.fullSize = fullSize;
}
public File getThumbnail() {
return Thumbnail;
}
public void setThumbnail(File thumbnail) {
Thumbnail = thumbnail;
}
}
I wanted to rotate the images as much as the user clicks on rotate button.The code below makes the image rotated correctly but my image is becoming blur.
I've been using the code below:
backLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<selectedImageList.size();i++) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap scaledBitmap = BitmapFactory.decodeFile(selectedImageList.get(i), bmOptions);
Matrix matrix=new Matrix();
matrix.preRotate(0);
Bitmap rotatedBitmap = Bitmap.
createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
File file=Utility.createFileFromBitmap(rotatedBitmap);
File oldFile=new File(selectedImageList.get(i));
LogUtil.error("Deleted Name",oldFile.getAbsolutePath()+"-->"+file.getAbsolutePath());
oldFile.delete();
itemList.set(selectedImages.get(i),file.getAbsolutePath());
selectedImageList.set(i,file.getAbsolutePath());
mSnapRecyclerAdapter.notifyDataSetChanged();
}
Matrix matrix = new Matrix();
imageView.setScaleType(ImageView.ScaleType.MATRIX); //required
matrix.postRotate((float) angle, pivotX, pivotY);
imageView.setImageMatrix(matrix);
/
#Override
public void onClick(View v)
{
imageView.setRotation(imageView.getRotation() + 90);
}
Im taking a screenshot of my app and try to post it on facebook using the facebook SDK. But as the ShareDialog appears with the Image, it´s upside down.. So I need to re-rotate it.
This is how I create the image:
private void saveScreenshot() {
try{
FileHandle fh;
do{
fh = new FileHandle(Gdx.files.getLocalStoragePath() + "stoneIMG" + counter++ + ".png");
}while(fh.exists());
Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
PixmapIO.writePNG(fh, pixmap);
pixmap.dispose();
System.out.println(fh.toString());
}catch(Exception e) {
}
}
And here I fetch it:
private Pixmap getScreenshot(int x, int y, int w, int h, boolean yDown){
final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h);
if(yDown) {
ByteBuffer pixels = pixmap.getPixels();
int numBytes = w * h * 4;
byte[] lines = new byte[numBytes];
int numBytesPerLine = w * 4;
for (int i = 0; i < h; i++) {
pixels.position((h - i - 1) * numBytesPerLine);
pixels.get(lines, i * numBytesPerLine, numBytesPerLine);
}
pixels.clear();
pixels.put(lines);
}
return pixmap;
}
Then I try to share the photo:
public void sharePhoto() {
String filePath = Gdx.files.getLocalStoragePath() + "stoneIMG" + counter + ".png";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
If you want to rotate a bitmap by 180 degrees you can use this code:
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
Matrix matrix = new Matrix();
matrix.postRotate(180);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
I'm trying to save as an image the contents of a WebView with a canvas drawing on top of it. I've tried two methods:
Picture picture = drawView.capturePicture();
Bitmap bmp = pictureDrawable2Bitmap(new PictureDrawable(picture));
MediaStore.Images.Media.insertImage(
getContentResolver(), bitmap,
"image" + ".png", "drawing");
private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable){
Bitmap bitmap = Bitmap.createBitmap(
pictureDrawable.getIntrinsicWidth() ,pictureDrawable.getIntrinsicHeight(),Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
return bitmap;
}
This gets me the entire WebView page but without the Canvas drawing.
Method 2:
Bitmap bitmap = Bitmap.createBitmap(drawView.getWidth(), drawView.getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawView.draw(canvas);
MediaStore.Images.Media.insertImage(
getContentResolver(), bitmap,
"image" + ".png", "drawing");
drawView.destroyDrawingCache();
This way I get both the canvas and WebView but it only captures it at the current zoom level. Which means if I save it while zoomed in it will only save the current view state, not the entire image. It's also lower quality.
Any suggestion on how to get them together?
Some of the code:
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
canvasBitmap = Bitmap.createBitmap(w + 1500, h + 1500, Bitmap.Config.ARGB_8888);
drawCanvas = new Canvas(canvasBitmap);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
clipBounds = canvas.getClipBounds();
canvas.save();
//canvas.translate(clipBounds.left, clipBounds.top);
drawPaint.setStrokeWidth(8/mScaleFactor);
canvas.scale(mScaleFactor, mScaleFactor, 0, 0);
if(!pathsDrawn) {
canvas.drawPath(drawPath, drawPaint);
pathsDrawn = true;
}
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.restore();
}
WebView settings:
final DrawingView drawView;
drawView = (DrawingView) findViewById(R.id.pdf);
drawView.setDrawingCacheEnabled(true);
WebSettings webSettings = drawView.getSettings();
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDefaultZoom(ZoomDensity.FAR);
webSettings.setSupportZoom(true);
webSettings.setDisplayZoomControls(false);
String data = "someurl.jpg"
drawView.loadUrl(data);
drawView.setDrawingCacheEnabled(true);
Picture picture = drawView.capturePicture();
Bitmap bmp = pictureDrawable2Bitmap(new PictureDrawable(picture));
MediaStore.Images.Media.insertImage(getContentResolver(), overlayMark(bmp, DrawingView.canvasBitmap),
"dfdsf" + ".png", "drawing");
EDIT*
Got it working for the most part. I basically drew two bitmaps with canvas. However, the web image bitmap and the drawing bitmap do not scale correctly. I had to offset this by scaling the drawing using a Matrix and redrawing the bitmap before overlaying it on the web bitmap. For some reason, scale it to 1.5f makes it line up perfectly, at least on the 10in tablet I'm testing it on. If anyone has any more insight on this or how to make it better let me know.
Matrix matrix = new Matrix();
matrix.postScale(1.5f, 1.5f);
Picture picture = drawView.capturePicture();
Bitmap bmp = pictureDrawable2Bitmap(new PictureDrawable(picture));
Bitmap resizedBitmap = Bitmap.createBitmap(DrawingView.canvasBitmap, 0, 0, DrawingView.canvasBitmap.getWidth(), DrawingView.canvasBitmap.getHeight(), matrix, false);
MediaStore.Images.Media.insertImage(getContentResolver(), overlayMark(bmp, resizedBitmap),
"dfdsf" + ".png", "drawing");
private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable){
Bitmap bitmap = Bitmap.createBitmap(
pictureDrawable.getIntrinsicWidth() ,pictureDrawable.getIntrinsicHeight(),Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
return bitmap;
}
private Bitmap overlayMark(Bitmap bmp1, Bitmap bmp2) {
int bh = bmp1.getHeight();
int bw = bmp1.getWidth();
Bitmap bmOverlay = Bitmap.createBitmap(bw,bh,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, 0, 0, null);
canvas.drawBitmap(bmp2, 0, 0, null);
return bmOverlay;
}
Is it possible to save a photo taken with a camera in color mode as black and white or must the camera paramaters be changed so the viewfinder sees black and white when the picture is taken? Here is the code I am using to save an image in color, I want to save it as black and white to save space.
PictureCallback jpegCallback = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.e("Pic Taken","Callback");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
options.inDither = false; // Disable Dithering mode
options.inPurgeable = true; // Tell to gc that whether it needs free
// memory, the Bitmap can be cleared
options.inInputShareable = true; // Which kind of reference will be
// used to recover the Bitmap
// data after being clear, when
// it will be used in the future
options.inTempStorage = new byte[32 * 1024];
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
int orientation;
// others devices
if(bMap.getHeight() < bMap.getWidth()){
orientation = 90;
} else {
orientation = 0;
}
Bitmap bMapRotate;
if (orientation != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(),
bMap.getHeight(), matrix, true);
} else
bMapRotate = Bitmap.createScaledBitmap(bMap, bMap.getWidth(),
bMap.getHeight(), true);
FileOutputStream out;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
try {
Log.e("Saving","Pic");
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "/" + System.currentTimeMillis() + ".jpg";
out = new FileOutputStream(baseDir + fileName);
bMapRotate.compress(Bitmap.CompressFormat.JPEG, 25, out);
if (bMapRotate != null) {
bMapRotate.recycle();
bMapRotate = null;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
You can convert Bitmap:
public Bitmap convertToGrayScale(Bitmap original) {
Bitmap result = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(matrix);
paint.setColorFilter(f);
canvas.drawBitmap(original, 0, 0, paint);
return result;
}
You can convert an existing photo into Black and White. After:
else
bMapRotate = Bitmap.createScaledBitmap(bMap, bMap.getWidth(),
bMap.getHeight(), true);
Add this:
Bitmap newBitmap = Bitmap.createBitmap(bMapRotate.getWidth(), bMapRotate.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(newBitmap);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter cmFilter = new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(cmFilter);
canvas.drawBitmap(bMapRotate, 0, 0, paint);
And don't forget to call .recycle() on bMapRotate.
It should be available in the Camera.parameters.
Depending on if the device's camera supports it, you could use it.
Use this code to retrieve them:
Camera.Parameters params = cam.getParameters();
try {
for (String e : params.getSupportedColorEffects()) {
Log.d(TAG, "Effect: " + e);
}
}
catch (NullPointerException npe) {
Log.d(TAG, "No color effects supported by this camera.");
}
And read the documentation:
http://developer.android.com/reference/android/hardware/Camera.Parameters.html