Hello here is the code:
URL uri = new URL(photoUrl);
URLConnection connection = uri.openConnection();
Log.i(TAG, "connecting...");
connection.connect();
Log.i(TAG, "connected");
Log.i(TAG, "building Bitmap...");
InputStream is = connection.getInputStream();
//BufferedInputStream bis = new BufferedInputStream(is, 8 * 1024);
File myfile = new File(getApplicationContext().getCacheDir(), "wallpaper.tmp");
myfile.createNewFile();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(myfile));
byte buf[]=new byte[1024];
int len;
while((len=is.read(buf))>0)
out.write(buf,0,len);
out.close();
is.close();
Bitmap bmp = BitmapFactory.decodeFile(myfile.getPath());
//Bitmap bmp = BitmapFactory.decodeStream(bis);
Log.i(TAG, "builded Bitmap");
Log.i(TAG, "showing bitmap...");
//int scale;
Matrix matrix = new Matrix();
matrix.setScale(0.1F, 0.1F);
//if (bmp.getWidth() < bmp.getHeight()){
// scale = canvas.getWidth()/bmp.getWidth();
//}else{
// scale = canvas.getHeight()/bmp.getHeight();
//}
//matrix.postScale(scale, scale, bmp.getWidth(), bmp.getHeight());
//matrix.postScale(0.5F, canvas.getWidth()/bmp.getWidth());
//Bitmap bmp2 = Bitmap.createScaledBitmap(bmp, canvas.getWidth(), canvas.getHeight(), true);
//Paint p = new Paint();
//p.setFilterBitmap(true);
//try{
canvas.drawBitmap(bmp, matrix, null);
//}catch(NullPointerException exc){
// //why do we get this??
// Log.d(TAG, "NullPointerException drawing canvas. why?");
// return;
//}
Now what happens is that drawBitmap is blocking since 5 minutes...
Any ideas?
You might get better performance by creating a scaled bitmap in the first place.
Bitmap bmp = BitmapFactory.decodeFile(myfile.getPath());
bmp = bmp.createScaledBitmap(bmp, width, height, true);
Then you won't have to use a matrix when drawing it out to the screen either.
Related
I'm getting an image from gallery into a layout , then I'm getting the bitmap of that layout by using getBitmap(), after getting bitmap I'm saving the image into device storage by using saveImage().
After getting bitmap and saving the bitmap, the quality and pixel of that bitmap reduces too much as shown in the pictureSaved image AND Orignal image
Here is the code for getting and saving bitmap
private Bitmap getBitmap(View v) {
v.clearFocus();
v.setPressed(false);
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
Toast.makeText(this, "Something Wrong", Toast.LENGTH_SHORT).show();
return null;
}
Bitmap lastimage = Bitmap.createBitmap(cacheBitmap);
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
//2048x2048 resolution
int newWidth = 2048;
int newHeight = 2048;
int width = lastimage.getWidth();
int height = lastimage.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(lastimage, 0, 0, width, height, matrix, true);
lastimage.recycle();
saveImage(resizedBitmap);
return resizedBitmap;
}
For Saving:
private void saveImage (Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/NewFolder");
myDir.mkdirs();
String fname = "Image-.png";
File file = new File(myDir, fname);
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Thanks for anyone's help.
I'm getting preview frames using OnImageAvailableListener:
#Override
public void onImageAvailable(ImageReader reader) {
Image image = null;
try {
image = reader.acquireLatestImage();
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
byte[] data = new byte[buffer.capacity()];
buffer.get(data);
//data.length=332803; width=3264; height=2448
Log.e(TAG, "data.length=" + data.length + "; width=" + image.getWidth() + "; height=" + image.getHeight());
//TODO data processing
} catch (Exception e) {
e.printStackTrace();
}
if (image != null) {
image.close();
}
}
Each time length of data is different but image width and height are the same.
Main problem: data.length is too small for such resolution as 3264x2448.
Size of data array should be 3264*2448=7,990,272, not 300,000 - 600,000.
What is wrong?
imageReader = ImageReader.newInstance(3264, 2448, ImageFormat.JPEG, 5);
I solved this problem by using YUV_420_888 image format and converting it to JPEG image format manually.
imageReader = ImageReader.newInstance(MAX_PREVIEW_WIDTH, MAX_PREVIEW_HEIGHT,
ImageFormat.YUV_420_888, 5);
imageReader.setOnImageAvailableListener(this, null);
Surface imageSurface = imageReader.getSurface();
List<Surface> surfaceList = new ArrayList<>();
//...add other surfaces
previewRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
previewRequestBuilder.addTarget(imageSurface);
surfaceList.add(imageSurface);
cameraDevice.createCaptureSession(surfaceList,
new CameraCaptureSession.StateCallback() {
//...implement onConfigured, onConfigureFailed for StateCallback
}, null);
#Override
public void onImageAvailable(ImageReader reader) {
Image image = reader.acquireLatestImage();
if (image != null) {
//converting to JPEG
byte[] jpegData = ImageUtils.imageToByteArray(image);
//write to file (for example ..some_path/frame.jpg)
FileManager.writeFrame(FILE_NAME, jpegData);
image.close();
}
}
public final class ImageUtil {
public static byte[] imageToByteArray(Image image) {
byte[] data = null;
if (image.getFormat() == ImageFormat.JPEG) {
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
data = new byte[buffer.capacity()];
buffer.get(data);
return data;
} else if (image.getFormat() == ImageFormat.YUV_420_888) {
data = NV21toJPEG(
YUV_420_888toNV21(image),
image.getWidth(), image.getHeight());
}
return data;
}
private static byte[] YUV_420_888toNV21(Image image) {
byte[] nv21;
ByteBuffer yBuffer = image.getPlanes()[0].getBuffer();
ByteBuffer vuBuffer = image.getPlanes()[2].getBuffer();
int ySize = yBuffer.remaining();
int vuSize = vuBuffer.remaining();
nv21 = new byte[ySize + vuSize];
yBuffer.get(nv21, 0, ySize);
vuBuffer.get(nv21, ySize, vuSize);
return nv21;
}
private static byte[] NV21toJPEG(byte[] nv21, int width, int height) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
YuvImage yuv = new YuvImage(nv21, ImageFormat.NV21, width, height, null);
yuv.compressToJpeg(new Rect(0, 0, width, height), 100, out);
return out.toByteArray();
}
}
public final class FileManager {
public static void writeFrame(String fileName, byte[] data) {
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
bos.write(data);
bos.flush();
bos.close();
// Log.e(TAG, "" + data.length + " bytes have been written to " + filesDir + fileName + ".jpg");
} catch (IOException e) {
e.printStackTrace();
}
}
}
I am not sure, but I think you are taking only one of the plane of the YUV_420_888 format (luminance part).
In my case, I usually transform my image to byte[] in this way.
Image m_img;
Log.v(LOG_TAG,"Format -> "+m_img.getFormat());
Image.Plane Y = m_img.getPlanes()[0];
Image.Plane U = m_img.getPlanes()[1];
Image.Plane V = m_img.getPlanes()[2];
int Yb = Y.getBuffer().remaining();
int Ub = U.getBuffer().remaining();
int Vb = V.getBuffer().remaining();
data = new byte[Yb + Ub + Vb];
//your data length should be this byte array length.
Y.getBuffer().get(data, 0, Yb);
U.getBuffer().get(data, Yb, Ub);
V.getBuffer().get(data, Yb+ Ub, Vb);
final int width = m_img.getWidth();
final int height = m_img.getHeight();
And I use this byte buffer to transform to rgb.
Hope this helps.
Cheers.
Unai.
Your code is requesting JPEG-format images, which are compressed. They'll change in size for every frame, and they'll be much smaller than the uncompressed image. If you want to do nothing besides save JPEG images, you can just save what you have in the byte[] data to disk and you're done.
If you want to actually do something with the JPEG, you can use BitmapFactory.decodeByteArray() to convert it to a Bitmap, for example, though that's pretty inefficient.
Or you can switch to YUV, which is more efficient, but you need to do more work to get a Bitmap out of it.
I have this code where i get an InputStream and create an image:
Part file;
// more code
try {
InputStream is = file.getInputStream();
File f = new File("C:\\ImagenesAlmacen\\QR\\olaKeAse.jpg");
OutputStream os = new FileOutputStream(f);
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
is.close();
} catch (IOException e) {
System.out.println("Error");
}
The problem is that I have to resize that image before i create if from the InputStream
So how to resize what I get from the InputStream and then create that resized image. I want to set the largest side of the image to 180px and resize the other side with that proportion.
Example:
Image = 289px * 206px
Resized image = 180px* 128px
I did this:
try {
InputStream is = file.getInputStream();
Image image = ImageIO.read(is);
BufferedImage bi = this.createResizedCopy(image, 180, 180, true);
ImageIO.write(bi, "jpg", new File("C:\\ImagenesAlmacen\\QR\\olaKeAse.jpg"));
} catch (IOException e) {
System.out.println("Error");
}
BufferedImage createResizedCopy(Image originalImage, int scaledWidth, int scaledHeight, boolean preserveAlpha) {
int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
Graphics2D g = scaledBI.createGraphics();
if (preserveAlpha) {
g.setComposite(AlphaComposite.Src);
}
g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
g.dispose();
return scaledBI;
}
And I did not use the other code.
Hope helps someone!
I've integrated ZXing 3.1.1 in my app to enable it to take pictures of barcodes and act on the input data.
I'm able to scan two dimensional codes (like QR Codes and Data Matrices) with it, but all one dimensional barcodes are failing to scan.
My code is as follows:
public void onPictureTaken(byte[] data, Camera camera){
BinaryBitmap bbm = BinaryBitmapFromJpegData(data); // Impl. below
MultiFormatReader reader = new MultiFormatReader();
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
Collection<BarcodeFormat> possible_types = new ArrayList<BarcodeFormat>();
possible_types.add(BarcodeFormat.UPC_A);
possible_types.add(BarcodeFormat.UPC_E);
possible_types.add(BarcodeFormat.EAN_8);
possible_types.add(BarcodeFormat.EAN_13);
possible_types.add(BarcodeFormat.QR_CODE);
possible_types.add(BarcodeFormat.DATA_MATRIX);
hints.put(DecodeHintType.POSSIBLE_FORMAT, possible_types);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
try {
Result result = reader.decode(bbm, hints);
Toast.makeText(context, "Found barcode: " + result.getText(), Toast.LENGTH_LONG).show();
} catch (NotFoundException e) {
Toast.makeText(context, "No barcode found", Toast.LENGTH_LONG).show();
Log.d("myapp", "Not found! (" + e.getMessage() + ")", e);
}
}
private BinaryBitmap BinaryBitmapFromJpegData(byte[] data){
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray);
BinaryBitmap bbm = new BinaryBitmap(new HybridBinarizer(source));
return bbm;
}
The stack trace that isn't very helpful either:
Does anyone have any ideas? I'm testing this on a Samsung Galaxy Note 2.
The solution was that the camera was shooting at a sideways orientation, and ZXing doesn't appear to check for 1D barcodes at an angle.
Rotating the bitmap in 90 degree intervals fixed the issue for me.
The new functions are as follows:
public void onPictureTaken(byte[] data, Camera camera){
MultiFormatReader reader = new MultiFormatReader();
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
Collection<BarcodeFormat> possible_types = new ArrayList<BarcodeFormat>();
possible_types.add(BarcodeFormat.UPC_A);
possible_types.add(BarcodeFormat.UPC_E);
possible_types.add(BarcodeFormat.EAN_8);
possible_types.add(BarcodeFormat.EAN_13);
possible_types.add(BarcodeFormat.QR_CODE);
possible_types.add(BarcodeFormat.DATA_MATRIX);
hints.put(DecodeHintType.POSSIBLE_FORMAT, possible_types);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
Result result = null;
for(int i = 0; i < 4; i++){
BinaryBitmap bitmap = BinaryBitmapFromJpegData(data, i * 90);
try {
result = reader.decode(bitmap, hints);
break;
} catch (NotFoundException e) {}
}
if(result == null){
Toast.makeText(context, "No Barcode Found", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Found barcode: " + result.getText(), Toast.LENGTH_LONG).show();
}
}
private BinaryBitmap BinaryBitmapFromJpegData(byte[] data, int rotation){
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
if(rotation != 0){
bitmap = RotateBitmap(bitmap, rotation);
}
int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray);
BinaryBitmap bbm = new BinaryBitmap(new HybridBinarizer(source));
return bbm;
}
public static Bitmap RotateBitmap(Bitmap source, float angle){
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
I trying capture image and set water mark from onActivityResult method is fragment from my code.
Private void savingCapturedImage() {
long date = System.currentTimeMillis();
Date data = new Date(date);
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera", "mobiliskaita.JPG");
Uri imagePath = Uri.fromFile(file);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imagePath);
System.out.println("bitmap: " + bitmap.getWidth() + " " + bitmap.getHeight());
file.delete();
bitmap = mark(bitmap, String.valueOf(data), 100, 200, 100, false);
bitmap = mark(bitmap, TheGlobals.partneriaiValue, 100, 310, 100, false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
bitmap.recycle();
File fileOutput = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera", photoName());
fileOutput.createNewFile();
FileOutputStream fo = new FileOutputStream(fileOutput);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
private Bitmap mark(Bitmap src, String watermark, int x, int y, int size, boolean underline) {
int w = src.getWidth();
int h = src.getHeight();
Point _p = new Point();
_p.x = x;
_p.y = y;
final float scale = getResources().getDisplayMetrics().density;
int p = (int) (900 * scale + 0.5f);
Bitmap result = Bitmap.createScaledBitmap(src, p, p, true);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(R.dimen.default_text_size);
paint.setAntiAlias(true);
paint.setUnderlineText(underline);
canvas.drawText(watermark, _p.x, _p.y, paint);
return result;
}
It's working but then i have camer with 8 or more megapixels, i get out of memory expection. Maybe someone can help my solve this problem?
The bitmap from the camera is going to be big, if you're only going to save it as 900x900, then you may have to use another method of reading it (not MediaStore.Images.Media.getBitmap()), one where you can set the inSampleSize: http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeFile(java.lang.String, android.graphics.BitmapFactory.Options)
You could also recycle src in mark() after you've drawn it to the canvas. At the moment you are creating 2 900x900dp bitmaps, so merging the 2 calls to mark might also help.